Louise
Louise

Reputation: 31

Popup freezes Selenium Webdriver until the popup is manually closed

I'm having problems automating tests on an internal website. In some cases, a popup will freeze the test until I manually close the popup. After the popup is opened, no code is run, not even System.out.println's.

driver.findElement(By.id("top_toolbarSALTkA7_Aras_Tbi_promote")).click();
System.out.println("test");

I have tried multiple ways of handling the popup, but no code at all is run after the click(), and it seems it never times out.

Tried:

1.

((JavascriptExecutor) driver).executeScript("return document.getElementById('top_toolbarSALTkA7_Aras_Tbi_promote', 'onClick')");

2.

Set<String> windowHandles = driver.getWindowHandles();
    for(String handle : windowHandles)
    {  
        driver.switchTo().window(handle);
        if (driver.getTitle().contains(title)) 
        {
            System.out.println("- (Page title is: " + driver.getTitle() + ")");
            break;  
        }  
    }

3.

driver.switchTo().alert();

4.

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

If I close the popup, the test will continue with the System.out.println and then continue until finshed.

I'm using Selenium Webdriver 2.48.2 with FireFox 31.0, programming is Java. Any ideas what can be done? (It's not possible to change the website)

Upvotes: 1

Views: 2816

Answers (2)

storenth
storenth

Reputation: 1225

Just disable popup. I mean you need to call the next script inside the current browser context (executeScript - I guess in your, Java binding case):

document.alert = window.alert = alert = () => {};

Upvotes: 0

Louise
Louise

Reputation: 31

Finally found the solution!!! Found it in the Selenium official user group: https://groups.google.com/forum/#!searchin/selenium-users/popup%7Csort:relevance/selenium-users/eDqPiYoJ9-Q/kRI67cCVe5wJ

Solution is to start a new thread that waits a couple of seconds, and then presses enter (or in my case first tabs to the "OK" button). Just call the function before the popup is opened.

    public static final void prepareToPressEnterKey(int seconds, int tabs) {
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
    ScheduledFuture scheduledFuture = scheduledExecutorService.schedule(new Runnable() {

        public void run() {

            try { 
                Robot robot = new Robot();

                for (int i=0; i<tabs; i++)
                {
                    robot.keyPress(KeyEvent.VK_TAB);
                    TimeUnit.SECONDS.sleep(1);
                    robot.keyRelease(KeyEvent.VK_TAB);
                }

                robot.keyPress(KeyEvent.VK_ENTER);
                TimeUnit.SECONDS.sleep(1); //press for 1 sec
                robot.keyRelease(KeyEvent.VK_ENTER);
            } catch (AWTException | InterruptedException e) {
                System.out.println("Prepare to Press Enter Exception");
            }
        }
    },
            seconds,
            TimeUnit.SECONDS);
    scheduledExecutorService.shutdown();
}

Still, if there are any better solutions I'd very much like to know. Thanks!

Upvotes: 2

Related Questions