Saturday, October 22, 2016

Handling unexpected poupus and alerts while automation with selenium webdriver



Hi All,
Today I just thought it’s been a long time I wrote my last blog and I should post another one. Meantime on what’s app group one of my friend asked question on how to deal with the unexpected popup message while automation was going on. I am sure most of the automation testers are still facing the same issue and trying to figure out a solution to this problem. 

QTP gives a very nice feature to handle such a conditions with the help of “recovery scenario manager” features which works on different triggers like popup dialogue. But when it comes to WebDriver automation on one hand we get freedom to choose whichever framework / methodology but on other we don’t get any built in features like “Recovery Manager Scenario” instead we have to code it.

So here I am going to post a solution with the code example on handling unexpected poupus / dialog boxes or alert messages encountered during webdriver automation.
  
The first thing first “Unexpected popups / dialogue boxes or alert messages are not really UNEXPECTED”. The popup messages like survey or feedback are expected to get displayed in application but one cannot predict when it will be displayed in application. It doesn’t make sense to have a if….else.. condition after every webdriver action.

I propose 2 solutions to this problem

Approach 1: Using a very simple approach
This solution is only applicable for framework which is not built on Page object model. If your framework is having Page Object model pattern please go with approach #2.

Do not allow the driver to freely flowing all over the project, Use encapsulation and abstraction to keep driver in one class and write dependent methods in same class. As this framework approach do not allow driver to get freely accessible anywhere in the project except this class.

Simple example is as ->





Use the Object of this WebDrivreFace class to perform all operations on driver. Don’t allow driver access outside of this class and always use getElement() & isDisplayed () methods before doing any direct operations on element.

Sample code to use this type of framework example –

WebDriverFace face = new WebDriverFace(); // initialize driver in constructor
face.openUrl(“http://samplewebsite.com”);
face.click( By.linkText(“sign in”) );
face.type( By.id(“userid”), “username@domain.com”);
face.type( By.id(“password”), “myAW3somep@55word”);

If there is any popup / dialog displayed matching with our text or locator the it will closed. J

Approach 2: using Page Object model
                If your framework is using Page Object model to initialize page Objects then use below technique.

                Have a common generic method to initialize all page class objects from your class. Make sure after every time you create a object you call a method which will check and handle popups.

High level code will look like this ->

Class Page is base class for all other pages class with below method ->

 class Page{
                public void handlePopupsAndAlerts(){
                                // use loop and check if any popup locator is displayed
                                // if displayed then write a code close it here.
                }
public static T getPageObject(WebDriver drver, Class clazz) {
                               
                                T pageObject = (T)PageFactory.initElements(driver, clazz);
                                pageObject. handlePopupsAndAlerts();
                                return pageObject;
                }
}
 
How to use this code to create page object?

LoginPage loginPage = Page. getPageObject(driver,LoginPage.class);
loginPage.performLogin();
 
So in above example as soon as the new page is loaded in browser we should create object as mentioned above. While initializing every page object it will execute handlePopupsAndAlerts() method to find and handle all types of popups and alerts.

3 comments: