Tuesday, September 10, 2019

Post#16.Locators Part-2 (ByChained,ByIdOrName,ByAll)


ByChained:

This locator performs a sequence of matches, drilling down into the DOM to find the element. It is probably best explained using an example of finding the email input within the registration form, but without using any major attribute of the email input:


<form role="form" id="registration-form">
<div class="form-group">
<label> Email
<input type="email" name="email" class="form-control" placeholder="E.g. john.doe@swb.com"/>
</label>
</div>
<!--- ... --->
</form>

1. Find form by ID "registration-form"
2. Find a label with the text "Email"
3. find an input
LocatorComposition.java
driver.findElement(new ByChained(By.id("registration-form"),
                                                          By.xpath("//label[contains(.,'Email')]"),
                                                          By.tagName("input"))
                                                          );
ByIdOrName :

This locator finds by the value of either ID or name attribute. This is useful for situations where you expect a page to change, and you want to be robust to that change. This is especially useful if the automation code doesn’t live with the production code, and you can’t guarantee they will get updated in step with one another. It is an example of backward/forward compatibility. It means you can update your test before the change is made, and it will still pass both before and after the change has occurred.

Consider this password input:
<input type="password" name="password" class="form-control"/>
It might be that the developers need to change the name to j_password . You can negotiate with them to add an ID to it:
<input type="password" name="password" id="password" class="form-control"/>
Then you can use the following locator:

driver.findElement(new ByIdOrName("password"));

This will work with before and after the change has been made. One thing to note is that if you use ByIdOrName with findElements (plural) you will get the super-set of elements that match: all elements that have the ID and name.
This method tries to find the element using id first, it wait till the max wait time for element if it is not able to find the element with id then only ByIdOrName locator tries with name.

ByAll:

ByAll locator in selenium helps to find the element based on the given locators, locator could be any other types like id, name, classname, link text, xpath, css, partial link text.

ByAll locator tries to find the element using first locator if the element is not present then it waits for given implicit wait time, once it reaches the maximum wait time and if there is no element, ByAll method tries to find the element using second locator and goes on.

Finding element continues till it finds the element or there is no more locators
driver.findElement
(new ByAll (By.className("not-exist"),  By.id("inner"))).sendKeys("help");


No comments:

Post a Comment