It has two methods,
These classes are not meant to be instantiated directly by users, users are required to use the correspondent static factory method to create them, for example, By.id(String id) will return a By.ById
In my opinion, The By class in these methods is the best design strategy in Selenium WebDriver framework, it allows users to only one method to achieve many variations of the searching method. Without this By parameter, it would need The following methods,
Luckily, they didn't take this route.
Joshua Block advised the same principle in his book, Effective Java, Avoid Strings when other types are more appropriate, please refer to Page 224 of Effective Java, Second Edition for more information.
-
Method Summary
Methods Modifier and Type Method and Description static By
className(java.lang.String className)
Finds elements based on the value of the "class" attribute.static By
cssSelector(java.lang.String selector)
Finds elements via the driver's underlying W3 Selector engine.static By
id(java.lang.String id)
static By
linkText(java.lang.String linkText)
static By
name(java.lang.String name)
static By
partialLinkText(java.lang.String linkText)
static static By
tagName(java.lang.String name)
static By
xpath(java.lang.String xpathExpression)
WebElement
findElement(SearchContext context)
Find a single element.abstract java.util.List<WebElement>
findElements(SearchContext context)
Find many elements.
If there are too many choices, people will be overwhelmed with the options. A lot of people often wonder which By class they should use.
There are some rule of thumb,
ByName
ById
ByClassName
ByCssSelector and ByXpath
Let us practise on this field.
<label for="shipping-zip" id="labelshipping-zip" class="text"><strong>Zip Code:&tl;/strong> <input type="text" class="text" name="shippingAddressDS.shipping_ROW0_zip" id="shipping-zip" size="10" maxlength="99" value="" /> </label>
ByName
// ☹ this is a bad example, please don't follow the style. webDriver.findElement(By.name("shippingAddressDS.shipping_ROW0_zip"));
ById
// ☹ this is a bad example, please don't follow the style. webDriver.findElement(By.id("shipping-zip"));
ByCssSelector
// ☹ this is a bad example, please don't follow the style. webDriver.findElement(By.cssSelector("input[id='shipping-zip'"));
ByXpath
// ☹ this is a bad example, please don't follow the style. webDriver.findElement(By.xpath("//input[@id='shipping-zip'"));You can see there are many different ways to locate the same element.
These are the ways to locate the element, but they are not the ways that they should be used in the test automation projects, for example, when filling out a form and click a button, it usually need to locate multiple elements and call the sendKeys method on each of them, such as,
// ☹ this is a bad example, please don't follow the style. try { WelElement webElement = webDriver.findElement(By.name("shippingAddressDS.shipping_ROW0_zip")); webElement.clear(); webElement.sendKeys(zipCode); } catch(NoSuchElementException e) { handle(e); }This is just the code for one input field, if you are doing this way for all input fields on the form, your tests will be full of code.
There is a better way than using Selenium directly, here is an example,
So just call,
// ☺ This is a good example. put(BILLING_ZIP_______, address.zipcode);
All Roads Lead to Rome, but there exists a shortest one.
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has nothing to do with my post, I will delete it soon. Thank you.
ReplyDelete