Selenium WD
Selenium WD
Method Name: To access any method of any class, we need to create an object
of class and then all the public methods will appear for the object.
Parameter: It is an argument which is passed to a method as a parameter to
perform some operation. Every argument must passed with the same data type.
For e.g. get(String arg0) : void. This is asking for a String type argument.
Return Type: Method can returns a value or returning nothing (void). If
the void is mentioned after the method, it means the method is returning no
value. And if it is returning any value, then it must display the type of the value for
e.g. getTitle() : String.
Get Commands
Command Syntax Sample code Notes
WebElement element =
driver.findElement(By.id("SubmitButton"));
Dimension dimensions = element.getSize();
System.out.println(“Height :” +
dimensions.height + ”Width : "+
dimensions.width);
WebElement element = This method fetch the width and height
driver.findElement(By.id("SubmitButton")); of the rendered element. This accepts
Dimension dimensions = element.getSize(); nothing as a parameter but returns the
System.out.println(“Height :” + Dimension object.
dimensions.height + ”Width : "+ This returns the size of the element on
element.getSize(); getSize( ) : Dimension dimensions.width); the page.
WebElement element =
driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed(); This method determines if an element is
//Or can be written as currently being displayed or not. This accepts
boolean staus = nothing as a parameter but returns boolean
driver.findElement(By.id("UserName")).isDisplaye value(true/false).
element.isDisplayed(); isDisplayed( ) : boolean d();
WebElement element =
driver.findElement(By.id("UserName"));
boolean status = element.isEnabled();
// Check that if the Text field is enabled, if yes
enter value This determines if the element currently is
if(status){ Enabled or not? This accepts nothing as a
element.sendKeys("ToolsQA"); parameter but returns boolean
element.isEnabled(); isEnabled( ) : boolean } value(true/false).
WebElement radioBtn =
driver.findElement(By.id("toolsqa")); If ID is given for the Radio Button/CheckBox
and you just want to click on it irrespective of
By ID radioBtn.click(); it’s value, then the command will be like this:
List oRadioButton =
driver.findElements(By.name("toolsqa"));
boolean bValue = false;
bValue =
oRadioButton.get(0).isSelected();
if(bValue = true){ If your choice is based on the pre-selection of
oRadioButton.get(1).click(); the Radio Button/Check Box and you just
}else{ need to select the deselected Radio
oRadioButton.get(0).click(); Button/Check Box. With IsSelected
} statement, you can get to know that the
IsSelected element is selected or not.
DropDown & Multiple Select Operations
deselectByIndex(int arg0) : void oSelect.deselectByIndex; Deselect the option at the given index.
Partial xpath with contains By using 'contains' function in XPath, we can extract all
//input[contains(@id,’username’)] keyword the elements which matches a particular text value.
//input[@id=’’]/following::input[1]
//input[@id='email']/following::tr It will identify the immediate node which start after
Using Following node the current node.
• Conditional:
We specify a condition along with timeout value, so that tool waits to
check for the condition and then come out if nothing happens.
Conditional wait is further classified into implicit and explicit wait.
Implicit wait
• An implicit wait is to tell WebDriver to poll the DOM for a certain
amount of time when trying to find an element or elements if they
are not immediately available.
• It is a mechanism which will be written once and applied for entire
session automatically. It should be applied immediately once we
initiate the Webdriver.
• It will work only for "FindElement" and "FindElements" statements.
• We need to include the below package :
import java.util.concurrent.TimeUnit;
• Syntax:
driver.manage.TimeOuts.implicitwait(6,Timeunit.SECONDS);
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“www.gmail.com”);
Explicit Wait:
• We need to define a wait statement for certain condition to be
satisfied until the specified timeout period. If the Webdriver
finds the element within the timeout period the code will get
executed.
• Explicit wait is mostly used when we need to Wait for a specific
content/attribute change after performing any action, like when
application gives AJAX call to system and get dynamic data and
render on UI.
• The two packages need to be included:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
•
/*Explicit wait for state dropdown field*/
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("st
atedropdown")));
Explicit Wait - variations
Command Sample code Notes
WebDriverWait wait = new
WebDriverWait(driver, waitTime); ExpectedConditions will return true
wait.until(ExpectedConditions.presenceOfEle once the element is found in the
isElementPresent: mentLocated(locator)); DOM.
Below is the syntax to check if the
element is present on the DOM of a
page and visible. Visibility means
WebDriverWait wait = new that the element is not just
WebDriverWait(driver, waitTime); displayed but also should also has a
wait.until(ExpectedConditions.elementToBeC height and width that is greater than
isElementClickable lickable(locator)); 0.
WebDriverWait wait = new
WebDriverWait(driver, waitTime);
List<WebElement> linkElements =
driver.findelements(By.cssSelector('#linkhell
o'));
visibilityOfAllElemen
WebDriverWait wait = new
ts
WebDriverWait(driver, waitTime);
check all elements present on the
wait..until(ExpectedConditions.visibilityOfAll web page are visible. We need to
Elements(linkElements)); pass list of WebElements.
WebElement element =
driver.findElement(By.id("")); check if the element is enabled or
isElementEnabled element.isEnabled(); not.
WebElement element = check if the element is displayed or
driver.findElement(By.id("")); not. It returns false when the
isElementDisplayed element.isDisplayed(); element is not present in DOM.
Fluent Waits:
• Fluent waits are also called smart waits also. They are called smart primarily because
they don’t wait for the max time out, specified in the .withTimeout(5000,
TimeUnit.MILLISECONDS). Instead it waits for the time till the condition specified
in .until(YourCondition) method becomes true.
When the until method is called, following things happen in strictly this sequence
Step 1: In this step fluent wait captures the wait start time.
Step 2: Fluent wait checks the condition that is mentioned in the .until() method
Step 3: If the condition is not met, a thread sleep is applied with time out of the
value mentioned in the .pollingEvery(250, TimeUnit.MILLISECONDS) method call.
In the example above it is of 250 milliseconds.
Step 4: Once the thread sleep of step 3 expires, a check of start time is made
with the current time. If the difference between wait start time, as captured in
step 1, and the current time is less than time specified in .withTimeout(5000,
TimeUnit.MILLISECONDS) then step 2 is repeated.
This process keeps on happening till the time either the time out expires or the
condition comes out to be true.
Fluent Wait:
//Declare and initialise a fluent wait
FluentWait wait = new FluentWait(driver);
//Specify the timout of the wait
wait.withTimeout(5000, TimeUnit.MILLISECONDS);
//Sepcify polling time
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
//Specify what exceptions to ignore
wait.ignoring(NoSuchElementException.class)