3

My company wants me to develop a "Visual" GUI style BDD function using JBehave and Selenium, which uses javascript alert/confirm popup window to prompt user what is the exact step the running test reaches, eg:

  • Given I goto "www.google.com"
  • When I login

So we want to add Javascript alert window to popup during the automation test, the popped up window has the "OK" button, so when user click the OK button, the test will continue to the next step, and so on...

My issue is: I wrote a javascript func using Selenium's executeScript API which invoke the pop up alert window:

public void stepText(String step) {
  executeScript("alert('"+step+"');");
}

So I expect when I click the OK button, the popped up window will disappear and test will continue to next step... But what shocked me is that when I click it, the test throw exception and crashed...

The exception is: selenium.WebDriverException

But I found if I add the following code to make the test automatically detect the alert window and accept it by using the following usual selenium alert handle function:

Alert alert=switchTo().alert();
alert.accept();

This can make the test runs well, so it looks I can NOT manually click the alert (after I manually click, the selenium still can NOT go back to the browser...lost connection to browser?), but the automation alert handle code works...

Of course, we want to let user to manually to click alert window to control the test execution, not the automation handle alert.

I really got stuck here for a while, and did a lot googling to search, but can not find similar example online, I hope you can shed me light on it, since you are much more guru than me on JBehave and Selenium.

I will be much grateful if you can help me out.

Don Cruickshank
  • 5,641
  • 6
  • 48
  • 48
Brian
  • 31
  • 1
  • 2

1 Answers1

3

Selenium is a browser automation tool, it does not anticipate user's interactions.

Therefore, I'd use a simple Java GUI window to present the user with messages/options. Afterall, you are testing a web application in a browser, but the program itself is Java and has nothing to do with the browser. A usual Swing option dialog should be enough.

  • JOptionPane.showMessageDialog(null, "Login successful.");
  • String loginAs = JOptionPane.showInputDialog("Login as:", "admin");
  • int choice = JOptionPane.showConfirmDialog(null, "Use production data?");

(note that you don't want to invoke this in the EventQueue.invokeLater() block, because you want the dialogs to be blocking)

This way, you won't interact with Selenium or the browser in any way, you won't confuse it and you'll get the user input cleanly.


That said, if you insist on using alerts, I think it's definitely doable, but as of now (June 2013, Selenium 2.33.0), I don't know how:

  1. The issue is not reproducible on IE8. After the executeScript("alert('Something.')"); call, Selenium waits for the call to return something and then proceeds normally. So you're good on IE.

  2. However, with FF21, Selenium fails immediatelly with UnhandledAlertException just as you said.

    I tried two obvious solutions:

    js.executeScript("alert('something')");
    new WebDriverWait(driver, 10)
        .pollingEvery(100, TimeUnit.MILLISECONDS)
        .ignoring(UnhandledAlertException.class)
        .until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()))
        .wait();
    

    and

    js.executeScript("alert('something')");
    boolean alertVisible = true;
    while (alertVisible) {
        try {
            driver.switchTo().alert();
        } catch (NoAlertPresentException ignored) {
            alertVisible = false;
        }
    }
    

    Both make FF fail horribly with an internal JavaScript exception. Possibly a bug that might get fixed (please test it, check whether it had been reported and report it if you're interested in it), so I'll leave the solutions here for future generations.

    But as I said before, it's possible that it won't get fixed, since Selenium doesn't count on manual user interactions.

  3. Not sure how this behaves in other browsers.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • Thank you so much @Slanec ! You helped me resolved this issue by using Swing. Well, I have another question maybe you can help me out, my company want me to use JMeter with JBehave and Selenium to do the load automation test, but I found JMeter seems only/usually support for Manual test by using its IDE/GUI, so how I can write code of JMeter to automate the test, I did lots of googling about it, but can not find useful/practical example of programmatically using JMeter... – Brian Jun 12 '13 at 16:07
  • Eg: How can I write code to simulate "I'm one of 10 users who navigate to www.google.com" by using JMeter's API? If you can help, I will be very grateful to you... – Brian Jun 12 '13 at 16:12
  • @Brian I'm sorry, I have never used JMeter. That said, from [this](http://stackoverflow.com/questions/16141927/how-can-i-use-my-junit-test-selenium-webdriver-with-jmeter), it seems to be possible to set JMeter as a proxy on your browser and run your WebDriver tests as usual. Not sure how to do this, though. Good luck! – Petr Janeček Jun 12 '13 at 16:22