Some selenium scenarios - Part 1

Share
selenium-web-driver


Selenium web driver ( Java ) has revolutionized how GUI testing is done. It's free, easy to customize and takes little time to learn.

It is a best alternative to both QTP and TestComplete, where you need to know hardly used languages like VB (except you are into excel automation).

In this post we learn automating some scenarios in Selenium.



Lets see simple tasks that can be done by Selenium.

We will take the example of our previous tutorial as explained here in detail.
We will use selenium for :

1. How to delete all cookies using Selenium
driver.manage().deleteAllCookies();


2.  Working with page source in Selenium

driver.getPageSource();

If you want to know if a text is present in the page source, you can use the below code as explained here at StackOverflow.
boolean b = driver.getPageSource().contains("your text");
assertTrue(b);
 
 
It is wise to write the page source so that it can be used or viewed later to a file as below:

try {
  
 String pagesource=driver.getPageSource();

 File file = new File("files/pagesource.txt");

 // if file doesnt exists, then create it
 if (!file.exists()) {
  file.createNewFile();
 }

 FileWriter fw = new FileWriter(file.getAbsoluteFile());
 BufferedWriter bw = new BufferedWriter(fw);
 bw.write(pagesource);
 bw.close();

 System.out.println("Done");

} catch (IOException e) {
 e.printStackTrace();
}

3. Difference between isPresent and isDisplayed

isElementPresent() - This method basically tests if the element we are looking for is present somewhere on the page.
isVisible() - looks for display: none style tag - this might throw a null pointer if we aren't careful...thus to see if an element is visible first check if the element is present using isElementPresent() method. Then try checking if the element is visible!
Observe that isElementPresent() won't mind even if our element is not visible.
For ex: lets say the below is the html code for a component on my test application:
now if you test the above component with
selenium.isElementPresent("testinput") - returns true!
selenium.isVisible("testinput") - returns false!
 
http://stackoverflow.com/a/10223301/1606579


4. Difference between Implicit wait and explicit wait in selenium
 Explicit Waits
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

 This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.
This example is also functionally equivalent to the first Implicit Waits example.

Expected Conditions

There are some common conditions that are frequently come across when automating web browsers. Listed below are Implementations of each. Java happens to have convienence methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.
  • Element is Clickable - it is Displayed and Enabled.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
 The ExpectedConditions package contains a set of predefined conditions to use with WebDriverWait.

Implicit Waits

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. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

Reference:  http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp


0 comments :

Post a Comment

Written by Sundeep Machado © 2007 - 2018 Sundeep Machado

You can get in touch with him on and Twitter