? QA Design Gurus: Tip: How to use XPath in Web UI Automation(Telerik Test Studio)

Oct 31, 2015

Tip: How to use XPath in Web UI Automation(Telerik Test Studio)



The biggest challenge in Web UI Automation is finding an element in a dynamic web page where we have more than one similar elements. In these days, web pages in most of the cloud applications are auto generated. Even though we provide a unique id for each element we cannot rely on these ids as they are auto generated. Enterprise automation tools provide element repository and we can add some conditions to identify the element. This solution may not work sometimes. In this situation, using XPath we can easily identify the element. Most of the open source and enterprise automation tools supports XPath.

Ref: https://goo.gl/YCRnQX


Frequently used XPath elements examples as follows. I used Telerik Test Studio enterprise automation tool APIs in below examples

1) following-sibling

 For example, let's assume you have many check boxes in a web page. You want to find 3rd check box. You need to find a unique text near to that text box. Using that we can find that element using XPath. Assume text "three" is unique text nearby that text box. You may write XPath such as the one given below

Element e = Manager.ActiveBrowser.Find.ByXPath("//td[text()='three']/following-sibling::td[1]/input");
Manager.ActiveBrowser.Actions.Click(e);

2) preceding-sibling

//a[text()='three']/parent::td[1]/preceding-sibling::td[2]

3) parent

//a[text()='three']/parent::td[1]/preceding-sibling::td[2]


Example: 

If you want to click on the check box of particular email then you use the following XPath.

(//span[text()="SearchString"])[1]/parent::div[1]/parent::td[1]/preceding-sibling::td[3]/div

Here "SearchString" can be an email sender name. The above XPath returns the first occurrence.

You can use the following XPath to get the value of check box.

(//span[text()="SearchString"])[1]/parent::div[1]/parent::td[1]/preceding-sibling::td[3]/div/@aria-checked


If you find at least one unique string in the web page then you can traverse the page to find your desired element using XPath

No comments:

Post a Comment