Synchronization in Automation Testing with selenium.

When you ask selenium tester what is the most common issues he is facing during his automation testing experience, the first thing he would say is Sync problem.

Let’s take a bird eye view on this and will take deep dive on the synchronization issues and how to address them.

Selenium framework has several methods to address the synchronization issues. Mainly they are segregated into two different varieties.

  • Implicit waits
  • Explicit waits

Implicit wait:

This is the most common wait type we use; it is also called as default wait. The page load time out, and the timeouts.implicitwait is the two types of implicit wait. The implicit wait meaning that the driver has to wait until the web element is existed on the dom. It will come out of the waiting loop if element exists beforehand. Example you have given 10 seconds on element x, and it has loaded in 2 seconds web driver knows it and goes to next step without waiting for 10 seconds. It is called as implicit wait.

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit wait:

Explicit waits are defined on the particular web element to wait until the property value becomes true.

It will pole every 500 milliseconds to get the property value and verifies it with defined value.

Examples:

//webdriver wait class
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(menu))));

//for multiple elements
WebDriverWait wait = new WebDriverWait(driver, 30);
List options = driver.findElements(By.xpath(option_to_select));
wait.until(ExpectedConditions.visibilityOfAllElements(options));

//Another example
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementWithText(By.className("jqx-fill-state-normal"), "Loading..."));

Fluent wait:

Fluent waits also called as explicit waits, but only difference being we can define interval to pole and can tell to ignore any exceptions. It will be useful to save resources not to check every 500 milliseconds if element load takes long time.

Wait<WebDriver> fluentwait = new FluentWait<WebDriver>(driver)
                        .withTimeout(20, TimeUnit.SECONDS)
                        .pollingEvery(1, TimeUnit.SECONDS)
                        .ignoring(java.util.NoSuchElementException.class);
fluentwait.until(ExpectedConditions.invisibilityOfElementWithText(By.className("jqx-fill-state-normal"), "Loading..."));

The another type of wait is normally not recommended is static wait or pause the program.

Thread.sleep(500);

So, the gist is the know when to use what. Fluent wait is used when application slower and running in a slow paced machine where we don’t want driver to hit every 500 milliseconds, and you know it will not come till 2 sec or 3 seconds, we can define the polling interval. I am guessing this as it is synonymous to implicit wait where the default polling is 500 ms.

We can use web driver wait almost in all cases where we know element loading is taking place, but the properties are not loaded until some other action is completed, example, state dropdown is enabled only when country drop down is selected, in that case we can use expected conditions on the enabled of the state dropdown after country select.

Default waits or implicit waits are used when we know dom loading is taking some time and not to give wait on existence of each element. Normally this would serve the purpose, but angular applications waits plays key role in test execution failures.

If we use waits wisely and where ever we need depending the application behavior with limited use of static waits will improve the script quality drastically.

Thank you.

Advertisement

Selenium – Overview

Introduction

Hello,

Recently, I met one of my colleague  who is preparing for Automation Test interviews.

As I do take interviews in my organization, I thought why not I throw my two cents on this topic. As the thought goes into action you see me here. I hope it helps people who are preparing for interviews and automation learners.

Every interviewer’s perception and thought process for selecting a candidate will be different. In my view one should know below three areas of the automation testing for that matter in any technology or the software we are learning or planned to learn.

  • Conceptual details :
    • This would cover the concepts of the tool/technology what we are planning to use where does this tool exactly fit in place.
  • Technical details:
    • This will give us complete information of the tool/technology implementation details.
  • Managerial details
    • This is the last area but the most important area, this will give us picture of what are the efforts and risks involving this tool and this implementation.
    • At the end this is the most deciding factor for choosing the technology and tool that we are planned to use.

In this blog I tried to put focus on above three mentioned areas and come up with general questions that an interviewer might ask.

and the details

Conceptual

What are the factors to choose the Selenium vs other tools ? why not UFT or other tools ?

  • Selenium is free library and driver owners are constantly updating the driver exes
  • Selenium is best suited for multiple browser testing with Java/.Net/python etc, if we have skilled resources available we can always utilize them for building the automated tests
  • Selenium can be integrated easily with BDD with Cucumber for business driven development for modern development environment
  • It can be easily integrated with CICD process with Jenkins or with TFS
  • It is not suited for the applications involving interactions with windows UI objects.
  • Auto IT is the free tool combined with Selenium is widely used for limited windows operations and more html DOM operations.
  • UFT tool is very much suited for HP tool set environment like ALM. It is also most widely used tool in the automated testing space, as it is licensed tool and license is costlier Its slowly loosing the market share. It only supports vb where as Java resources much widely found. Distributed testing can be easily done with this combination as test management tool like ALM, it has capabilities to run scripts on multiple host machines.
  • Selenium Grid can be used for distributed testing, if Jenkins is used, we can configure multiple Slave nodes containing different browsers and operating systems.
  • If regression suite size is large it would be good to use frame works like testNG or cucumber for better maintainability.

 What is Jenkins and CICD process how does Selenium fit in this ?

  • CICD is continuous integration and continuous development
  • It is the integrated part of modern fast paced development methodologies like Agile Scrum.
  • CICD , CI part of it is being when build gets generated there will be set of automated tests will run to verify the build stability.
  • Jenkins is the tool which is widely used to build the code in distributed (there are lot of other tools which supports CICD environment) we can add build steps in this tool
    • Post build operation being triggering the automated build
    • Jenkins will receive feedback from the automated tests and it will mark build pass/fail/warning based on the test results.
    • CD part being development of new code/ enhancements based on the failures or new features.

Technical:

What is selenium ?

  • Selenium is code library that has methods developed to interact with modern browsers.
  • The actions on the browsers are performed by driver exes. Selenium methods can talk to browsers through these driver exes.
  • We can send or receive information from driver exes through the selenium methods.

What is xpath ?

  • Xpath is the general location of an web element in the HTLM dom (document object model)
  • This will tell us the location or generalized location of element or elements in the DOM tree
  • This is most widely used and modern browsers supports the xpath search.
  • This is fast and not mostly reliable as the dom structure will dynamically change in the modern web technologies.

What is css selectors ?

  • Css selector is the why selenium identifies the object similar to Xpath
  • Selenium can read the css files and it can identify the objects based on css classes.
  • Css selectors and xpaths are most commonly used methods to identify the object

In web application driver.findement(by.xpath) not recognizing te objects. Application is html based we can see element in the dom, what might be the issue ?

  • The most common reason for this is being that element is in iFrame.
  • We need to switch the driver to that iFrame
    • Before that we need to find all elemnts with iFrame tag get either ids or names of the frames and switch to that frame
    • With Driver.switchto
    • Then if we use find elements we can find the element.

What is TestNG xml ?

  • TestNG xml is the source or driving the test suites build on TestNG framework
  • We can give test classes and test groups information to run the tests.

How can we identify a link in the application ?

  • Link can be identified with by.linktext or by.partiallinktext

What are the different ways to identify elements ?

  • By.TagName
  • BY.Xpath
  • By.cssseector
  • By.classname

How do handle elements with in the elements say for example data in table ?

  • We can iterate the findelements with in the elements like below
  • First find Elements with tag name tr
List <WebElement>rows = Driver.FindElements(By.tagnme(“tr”))
for (WebElement row :rows)
 {
   List<WebElement> dataincolumns = Row.findelements(By.tagname(“td”))
 }
  • This is just illustration that we can  do following
Driver.FindElement(By.tagname(<locator>)).FindElement(By.xpath(<locator>).FindElement….. etc

How can we get the text in the textbox

  • We use element.getAttribute to get the attribute of elements such as value and color
  • Element.getAttribute(value) will give us the text of the element.

What are actions ?

  • Actions is class for mouse interactions
  • Some of the web elements need to be hovered to get the list in the common web page designs.
  • We need actions class to perform those operations.
  • Examples:
    • Hovering
    • Mouse move

what is test framework ?

  • test framework is well defined process for creating, execution reporting the automated tests.
  • We see lot of customized framworks in each organizations and some free framework tools also available
    • Junit
    • TestNG
    • Cucumber
  • The main concepts for designing framework should be
    • Process to easily create tests
    • Process to pass data to tests
    • Process to easily maintain the tests
    • Process to generate required reports

Managerial

How can we report with selenium

  • Reporting is bit of crude in selenium, if we use standard frameworks we can get html report
  • Generally we create expected result and actual result in array lists and at the end of the tests we flush them to excel.

How can we manage the code repository if many people are working

  • Selenium development of tests is same as any development project.
  • We use SVN or TFS code management tools to make regular check ins and check outs.
  • SVN is free , and most widely used code management tool, for Microsoft technologies we use TFS as code management.

What is Maven ?

  • Maven is the repository for Jar files. Jar files are nothing but java libraries which has functions or methods readily available for usage.
  • When building a project with selenium we require to use standard java libraries for different purposes
  • Examples:
    • testNG jar file
    • Apache poi jar file
  • If multiple people are using the project, sharing of these libraries will become challenge.
  • Maven project will remove this dependency by adding all dependent jar files directly from Maven repository.
  • Maven will create local repository and down loads all required jar files in the local system. We need to make sure we add all dependencies in the pom.xml.

These are some of the commonly asked High level overview questions in the Selenium interviews. I see we did not cover everything in very detail as it is just overview.

I will try to cover each area with important aspects in my next blogs. Thank you.

Page Object Model – Approach

In selenium, most frequently we hear page object model.

I have asked people why did they call it as page object model ? Is there any specific reason behind this ?

I got general answer saying “we store all locators in a page as static final strings in a page class”

Is this correct explanation ? are we doing page object model as is ? below is another view point.

In general we call page object model because every method in a page returns the page object instead of string/float/int or any standard return types.  This will provide flexibility in writing test cases easily.

Step 1 : Define Page class

package test.pom;
public class page1 extends pageRep
{  
public page1() 
{
super(); 
} 
public page1 testM1() 
{ 
System.out.println("Method 1 Page 1");  
return this; 
} 
public page1 testM2() 
{ 
System.out.println("Method 2 Page 1");  
return this; 
} 

}

Create another page to illustrate navigation between pages.

package test.pom;
public class page2 extends pageRep 
{
 public page2() 
{ 
super(); 
} 
public page2 test_M1() 
{ 
System.out.println("Method 1 Page 2");  
return this; 
} 
public page2 test_M2() 
{ 
System.out.println("Method 2 Page 2");  
return this; 
} 
}

If you observe clearly, the above classes are returning the page class as object and extending the class page repository. The class page repository is the class where we use to navigate between the pages.

Step 2: Create page repository for navigation between pages

package test.pom;
public class pageRep { 
public pageRep() 
{
 System.out.println("Calling the super class"); 
}
public page1 navigateToPage1() 
{ 
return new page1();
} 
public page2 navigateTopage2() 
{ 
return new page2(); 
} 
}

Step 4 : While creating the test case you can see it will be simple as we can keep “.” to see the methods in the class as each method returns the class object again, you will have visibility of all methods.

Page repository helps us to navigate between pages.

This is the best illustration of the Java extends concept which we can use to write our tests in selenium.

package test.testcases;
import test.pom.pageRep;
public class tests {
public static void main(String[] args) 
{ 
 pageRep start = new pageRep();
 start     
.navigateToPage1().testM1().testM2()
.navigateTopage2().test_M1().test_M2()     
.navigateToPage1().testM1(); 
 }
}

output:

Calling the super class
Calling the super class
Method 1 Page 1
Method 2 Page 1
Calling the super class
Method 1 Page 2
Method 2 Page 2
Calling the super class
Method 1 Page 1