Page 1 of 1

Basic button detection question

Posted: Fri Oct 21, 2016 10:51 am
by Jordy
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
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

Re: Basic button detection question

Posted: Fri Oct 21, 2016 12:10 pm
by odklizec
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 ;)

Re: Basic button detection question

Posted: Fri Oct 21, 2016 12:16 pm
by Jordy
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.

Re: Basic button detection question

Posted: Fri Oct 21, 2016 12:44 pm
by odklizec
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");
            }

Re: Basic button detection question

Posted: Fri Oct 21, 2016 1:02 pm
by Jordy
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.

Re: Basic button detection question

Posted: Fri Oct 21, 2016 1:05 pm
by odklizec
Hi,

You are welcome ;)