This section provides an overview of what selenium-webdriver is, and why a developer might want to use it.
It should also mention any large subjects within selenium-webdriver, and link out to the related topics. Since the Documentation for selenium-webdriver is new, you may need to create initial versions of those related topics.
Items are found in Selenium through the use of locators and the By
class. In order to make a robust automation project with Selenium, one should use locators for Web Elements smartly. The locators should be descriptive, unique, and unlikely to change so you won't get false positives in tests for example. The priority is to use:
div::parent
.The rest of the locators are prone to changes or rendering, and preferebly be avoided.
Rule of thumb: if your code cannot locate a particular element, one reason could be that your code hasn't waited for all the DOM elements to download. Consider telling your program to "wait" for a short period of time (try 3-5 seconds, and then slowly increase as needed) before searching for said element. Here is an example in Python, taken from this question:
from selenium import webdriver
import time
browser = webdriver.Firefox()
browser.get("https://app.website.com")
reports_element = browser.find_element_by_xpath("//button[contains(text(), 'Reports')]")
# Element not found! Try giving time for the browser to download all DOM elements:
time.sleep(10)
reports_element = browser.find_element_by_xpath("//button[contains(text(), 'Reports')]")
# This returns correct value!
This section contains information of Actions class of Selenium WebDriver. The Actions class provides you convenient methods to perform complex user gestures like drag and drop, hold and click etc.
Page object model is a pattern where we write object oriented classes that serve as an interface to a particular view of web page. We use the methods of that page class to perform the required action. Few years back, we were manipulating the HTML code of webpage in test classes directly which was very difficult to maintain along with brittle to changes in UI.
However, having your code organized in a way of Page Object Pattern provides an application specific API, allowing you to manipulate the page elements without digging around the HTML. The basic Rue of thumb says, your page object should have everything which a human can do on that webpage. For example, to access the text field on a webpage you should a method there to get the text and return string after doing all the modifications.
Few important points you should keep in mind while designing the page objects:
Page object usually should not build only for pages, but you should prefer to build it for significant elements of page. For example, a page having multiple tabs to show different charts of your academics should have same number of pages as the count of tabs.
Navigating from one view to other should return the instance of page classes.
The utility methods which are required to be there only for a specific view or webpage should belong to that page class only.
The assertion methods shouldn't be taken care by page classes, you can have methods to return boolean but don't verify them there. For example, to verify user full name you can have method to get boolean value:
public boolean hasDisplayedUserFullName (String userFullName) {
return driver.findElement(By.xpath("xpathExpressionUsingFullName")).isDisplayed();
}
If your webpage is based on iframe, prefer to have page classes for iframes too.
Advantages of Page Object Pattern:
This section contains details about implementation of Robot API with Selenium Webdriver. The Robot class is used to generate native system input when selenium is not capable to do that for example pressing right key of mouse, pressing F1 key etc.
Select
class of Selenium WebDriver provides useful methods for interacting with select
options. User can perform operations on a select dropdown and also de-select operation using the below methods.
In C# the Select class is actually SelectElement
Note that there are two ways of using the annotation. Examples:
@FindBy(id = "id")
and
@FindBy(how = How.ID, using ="id")
are equal and both look for element by its ID. In case of ID_OR_NAME
you can only use
@FindBy(how = How.ID_OR_NAME, using ="idOrName")
PageFactory.initElements()
must be used after page object instantiation to find elements marked with @FindBy
annotation.