5. LOCATORS

5. LOCATORS

1. Static methods which are used to identify the elements which are present the webpage.

2. All these locators are present in a class called By which is an Abstract class.

3. There are 8 types of locators and all the locators takes argument of type string. They are,

1. Id(String)

2. name(String)

3. className(String)

4. tagName(String)

5. linkText(String)

6. partialLinkText(String)

7. cssSelector(String)

8. xpath(String)

Note:

1 .Id, name, className are available as attributes of an element.

2. In order to Handle the single element we use findElement().

3. Return type of findElement() is WebElement.

4. In findElement(), if the specified locator is not matching with any element it will throw NoSuchElementException

5 In findElement(), if the specified locator is matching with multiple element it will return the address of 1st matching element

6 If the specified element is link, then we can identify that element by using linkText

7 If the specified element is link and if it partially dynamic, then we can identify that element by using partialLinkText

Reference URL - ("https://adactin.com/HotelApp/index.php")

A. cssSelector

1. If we can not identify the elements by using any of the above locators, then we can identify that element by using cssSelector.

2. Syntax:

-tagnName[attributeName=’attributeValue’]

-ex: input[type='password']

3. In order to verify cssSelector expression in firefox browser, click on TX-select queryselectorAll option? specify the expression in expression field and click on enter

Note:

- In cssSelector,

- Id can be represented by using #,

Ex:- input#email

- Class can be represented by using .

Ex:- tagName.classValue- WebElement x = driver.findElement(By.id("username"));

B. LinkText:

<a href="ForgotPassword.php">Forgot Password?</a>

Text: >Forgot Password?<

linkText: Text which is available in a tag called linktext.

WebElement x2 = driver.findElement(By.linkText("Forgot Password?"))

C. Xpath:

Path of an element present in the webpage.

--Absolute

--Relative

Sample webpage:

<div>

A:<input type="text" value="A">

B:<input type="text" value="B">

</div><br/>

<div>

C:<input type="text" value="C">

D:<input type="text" value="D">

</div>

Absolute:

-Complete path of an element from root of the webpage(html)

- Represented by using /--> immediate child

Ex:

/html/body/div[1]/input[2]

Relative xpath:

- Path of any element which is present on web page.

- It is represented by using // which means any child/element

Syntax:

1. //tagName? all the matching elements

2. //tagName[1]? all the 1st matching elements

3. //tagName[last()]? all the last matching elements

4. //*? all the elements

5. //*[@attribute=’value’]

6. //tagName[@attribute=’value’]

Ex:

//div[1]/input[2]

//div[1]/input

//div[2]/input

//input[1]

//div[1]/input[2]| //div[2]/input

//input

Xpath by attribute:

- To identify the specified elements, if we use index it may not work properly when we use the index values because whenever the position of an element changes its index value will also changes.

- To overcome the above problem in place of index we can include attributes which is called as xpath by attributes.

- It is applicable for both Absolute and Relative xpath.

-Syntax:

-- tagName[@attributeName=’attributeValue’]

-Example:

-- Absolute? /html/body/div/input[@value='B']

-- Relative? //input[@value='B']

- In an xpath we can pass multiple attributes by using or operator.

- Example:

-- //input[@value='B' or @value='C']

Xpath by text():

- If the specified element does not contain any attributes and if it contains text then we can identify that element by using xpath by text()

- It is applicable for both absolute and relative xpath

- Syntax:

-- tagName[text()=’textValue’]

- Example:

-- //td[text()='Java']

- text() can be represented by using dot(.)

- Example:

-- //td[.='Java']

- Attribute values and the text values are case and space sensitive.

- Example:

-- //div[text()='Login '

Xpath by contains():

- It is used to handle the partial dynamic elements

- It is applicable for both absolute and relative xpath.

- Syntax 1: if text value is partially dynamic,

-- tagName[contains(text(),'textValue')]

- Example:

-- //nobr[contains(text(),'actiTIME')]

- Syntax 2: if attribute value is partially dynamic,

-- tagName[contains(@attributeName,'attributeValue')]

- Example:

-- //img[contains(@src,'/img/default/login/timer.png?hash')]

Traversing:

- Navigating from one element to another element using xpath is called traversing.

- To navigate from one element to another element xpath uses axis.

- The different types of axis are,

1. child

2. parent

3. descendant

4. ancestor

5. following-sibling

6. preceding-sibling

- Syntax:

/axis::tagNam

-Navigate from one element to any of it’s child present on the webpage.

Ex: /html/descendant::option[1]

Ancestor:

- Navigate from one element to any of it’s parent present on the webpage.

Ex: //select[@option=’j’]/ancestor::html

Following-sibilng:

- The elements which are present below the specified element, under same parent

are called as following-sibling.

Ex:

//select[@option=’m’]/following-sibling::option? A,M

//select[@option=’m’]/following-sibling::option[1]? A

//select[@option=’m’]/following-sibling::option[2]? M

Preceding-sibling:

- The elements which are present above the specified element, under same parent

are called as following-sibling.

Ex:

//select[@option=’m’]/preceding-sibling::option? J,F

//select[@option=’m’]/preceding-sibling::option[1]? F

//select[@option=’m’]/preceding-sibling::option[2]? J

Comment for better reach:

To join with testing group on WhatsApp click here The Testing Gurukul


要查看或添加评论,请登录

Abhishek Chauhan的更多文章

社区洞察

其他会员也浏览了