Basic button detection question

Ask general questions here.
Jordy
Posts: 4
Joined: Fri Oct 21, 2016 10:08 am

Basic button detection question

Post by Jordy » Fri Oct 21, 2016 10:51 am

Hello all.

I was just trying some browser testing (in IE) when I found some buttons that I could not manage to detect with user code. Using XPath I can find all buttons perfectly fine.
buttons.png
Buttons
buttons.png (21.75 KiB) Viewed 471 times
The code I am using to try to detect them:
public void removeLocations()
        {
        	IList<Button> allButtons = Host.Local.Find<Button>("/dom[@domain='main url']//ul[#'listLocaties']//button");
        	
        	foreach(Button button in allButtons)
        	{
        		Report.Info("Button found: " + button.Text);
        		if(button.Text.Contains("Verwijder"))
        		{
        			Mouse.Click(button);
        		   	Report.Info("Delete button was clicked.");
        		} else {
        		   	Report.Info("Button was found but did not contain delete option.");
        		}
        	}
        	Report.Info("No buttons found"); //to make sure code executed the loop in case it returns nothing
        }
I typed 'main URL' because I can't post links due to being registered recently.

This will always return "No buttons found". I have also tried the div element that's the parent of the list I'm using but it is also not working.

Using just "//button" will return all buttons so I assume either my path is built wrong or I am using the wrong method to search in my browser?

Thanks in advance for your time :)
Jordy
Attachments
RouteplannerSnapshot.rxsnp
Snapshot
(445.48 KiB) Downloaded 22 times
Last edited by Jordy on Fri Oct 21, 2016 12:15 pm, edited 3 times in total.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3916
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Basic button detection question

Post by odklizec » Fri Oct 21, 2016 12:10 pm

Hi,

Please post a Ranorex snapshot (not screenshot) of the problematic elements. Snapshot is critical piece of information to find a possible problem and determine best approach how to solve it. Thanks.

PS: the screenshot you tried to post is not visible anyway ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Jordy
Posts: 4
Joined: Fri Oct 21, 2016 10:08 am

Re: Basic button detection question

Post by Jordy » Fri Oct 21, 2016 12:16 pm

Hello,

I'm sorry. Post was waiting to be verified so I could not edit it until it was actually posted.
Added a snapshot and fixed the screenshot! The screenshot is too big though, to see the area I marked you will have to scroll to the right.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3916
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Basic button detection question

Post by odklizec » Fri Oct 21, 2016 12:44 pm

Hi,

OK, as for the reason why the buttons were not found in code, there are two problems. You need to declare the button as "ButtonTag" instead of "Button". Then you should validate InnerText attribute, instead of just Text. Text attribute is not available for given button (in the snapshot you posted). So the code should look like this:

Code: Select all

                IList<ButtonTag> allButtons = Host.Local.Find<ButtonTag>("/dom[@domain='main url']//ul[#'listLocaties']//button");  
                  
                foreach(ButtonTag button in allButtons)  
                {  
                    Report.Info("Button found: " + button.InnerText);  
                    if(button.InnerText.Contains("Verwijder"))  
                    {  
                        Mouse.Click(button);  
                        Report.Info("Delete button was clicked.");  
                    } else {  
                        Report.Info("Button was found but did not contain delete option.");  
                    }  
                }  
                Report.Info("No buttons found"); //to make sure code executed the loop in case it returns nothing  
            }  		
Also, I would suggest to make the searched xpath more detailed, so
/dom[@domain='www.delijn.be']//ul[#'listLocaties']//button[@innertext~'Verwijder']
So then you can reduce the amount of required code to bare minimum, because the list will now contain only the "Verwijder" buttons:

Code: Select all

    public void removeLocations()  
            {  
                IList<ButtonTag> allButtons = Host.Local.Find<ButtonTag>("/dom[@domain='main url']//ul[#'listLocaties']//button[@innertext~'Verwijder']");  
                
                if (allButtons!=null)
                {
	                foreach(ButtonTag button in allButtons)  
	                {  
	                    Report.Info("Button found: " + button.InnerText);  
	                    Mouse.Click(button);  
	                    Report.Info("Delete button was clicked.");  
	                }                 	
                }
                else
                	Report.Info("No buttons found");
            }
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Jordy
Posts: 4
Joined: Fri Oct 21, 2016 10:08 am

Re: Basic button detection question

Post by Jordy » Fri Oct 21, 2016 1:02 pm

Thanks Pavel :)

First attempt I had too many locations and it was failing because it wanted to click the first button which was not visible. This was easily fixed with some visibility checks though, so all is good now! Have a nice day.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3916
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Basic button detection question

Post by odklizec » Fri Oct 21, 2016 1:05 pm

Hi,

You are welcome ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration