Wednesday 10 October 2018

How to handle unexpected popup / alert in Selenium

How to handle unexpected popup / alert in Selenium?



Here, what I mean by unexpected popup / alert is you will see in some applications popups come unexpectedly and you can't expect the sequence of step when it will come in Application under test. So this is the challenge we have to handle that comes any time in the screen. 

We can handle unexpected popup / alerts using Selenium Listeners. Below steps can be followed. 

1. Create a class with any name for eg: WebListen.java implementing WebDriverEventListener (Interface)
2. Create method related to popupHandler / alertHandler in the same class that is created in (1 - WebListen.java ). 

public void checkAlert() {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 2);
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
    } catch (Exception e) {
        //exception handling
    e.printStackTrace();
    System.out.println("In alert hanlder");
    }
}

OR
public void checkPopup(WebDriver driver) {
    try {
        WebDriverWait wait = new WebDriverWait(driver,5);
        WebElement skip_link= driver.findElement(By.linkText("Skip"));
        wait.until(ExpectedConditions.elementToBeClickable(skip_link));
        skip_link.click();
    } catch (Exception e) {
        //exception handling
    System.out.println(e.getMessage());
    System.out.println("In popup hanlder");
    }
}

Depending on your project requirement / scenario based on the alert / popup object properties you have. 

Now call these methods in overridden methods wherever you think is applicable. 

For eg: afterChangeValueOf(), beforeChangeValueOf ()

as below: 

@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub
System.out.println("afterChangeValueOf");
checkPopup(driver);
}

@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub
System.out.println("beforeChangeValueOf");
checkPopup(driver);
}
or any other method like beforeClick, beforeAlert and so on. 

What this means is beforeClick / beforeAlert or any other event is going to be occurred first you want to check whether alert or popup is there. If yes, you want to handle it and then move ahead. 


Now we need to use this listener in our test class. 

3. Use EventFiringWebDriver which is a class to register listener events. 

EventFiringWebDriver event1= new EventFiringWebDriver(driver);

// where driver is your driver instance. 

WebListen listen= new WebListen (event1);

// where WebListen is the class implementing WebDriverEventListener

event1.register(listen);

// use event1 ref variable going forward. 

event1.get("http://www.sample.demo.com");
event1.manage().window().maximize();
event1.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
event1.findElement(By.cssSelector("input[id = 'LoginForm_username']")).sendKeys("admin");
// here internally check for popup / alert will happen
event1.findElement(By.cssSelector("input[id = 'LoginForm_password']")).sendKeys("admin");
// here internally check for popup / alert will happen
event1.findElement(By.cssSelector("#Login > span.z-label")).click();

event1.unregister(listen);

This way your code will handle any popup / alert that is coming in AUT. 

if you have any questions about this do write in comment below. 

Hope this blog has been useful to you. If yes do follow my blogs by entering your email by 
clicking on "Follow" (blue) button. 

You can as well explore my 

Youtube Channel: https://www.youtube.com/user/srinivaskinik

And 

facebook page: https://www.facebook.com/srinivaskinikalmady/


Thank you. 

No comments:

Post a Comment

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...