2

How can I catch an error when I don't know when it happens?

I'm using Selenium + Java. Creating tests for a JS web page.

When an error occurs, the test continues clicking on the elements. The error is visible in that moment, so it is not a messagebox or something like that. An errorbox just becomes visible. Then some time passes, and the test crashes saying that it cannot click some element.

How can I listen for any random element to be visible (or clickable) while my test is running? That would allow me to catch the error and the test would not fail.

Should I put my test into two threads, one listening for errors, and another running my test case?

Alex Wittig
  • 2,800
  • 1
  • 33
  • 42

2 Answers2

0

I would suggest to use the following

List<WebElement> Elements = getDriver().findElements(method);

First, when you entry to the page you just prepare the list of the page elements. Then you include this procedure several time in your code. You compare your list with the new list and see if the error element is appears, means you will have one more element in the new list. Then you catch it.

Eugene
  • 1,865
  • 3
  • 21
  • 24
  • Can you be more specific? I get the idea how to add page elements to List. But what do i have to compare? How can it be look like in code? – Alexander Krivorotko Mar 19 '15 at 15:00
  • I supposed that your error box is the new element appears in the page. Then if you have list elements you may compare the new list with the old one. The new list in case error will include your error box. Then the code will know that the new element is added. Please see this [post](http://stackoverflow.com/questions/2762093/java-compare-two-lists) for the compare help – Eugene Mar 20 '15 at 10:34
  • Unfortunately error box in not a new element. This element is present but it just changes his class name in DOM. – Alexander Krivorotko Mar 23 '15 at 13:02
  • Well, why not compare the class names with the same procedure? – Eugene Mar 23 '15 at 19:02
0

The best option is to understand and resolve the underlying error condition.

If the error is only something that occurs exclusively in testing, or otherwise cannot be resolved I would suggest looking at your driver configuration and insure that the dialog you're seeing wouldn't be covered by the Driver setting for Unexpected_Alert_Behavior. If you've got that turned off, I'd try turning it on and see how it impacts your behavior.

I'm not entirely certain that will solve the listed problem bc the dialog you mention is part of the dom and the 'unexpected' alert behavior is typically not visible in that context from what I've seen. I believe this is also very specific to the IE implementation.

Finally, I think I would resort to using a method in my class to perform all findbys for me. In that method, I would use an explicit wait to check for the random addition and have it fall-through to resolve the actual requested object if the wait fails to resolve the 'error reference'.

  /** By identifier for the error dialog addition.*/
    private static final By ERR_DLG = By.className("error");

    /**
     * Delegate method that will attempt to resolve the occasional page error condition prior to performing the delegate lookup on the WebDriver.
     * <p/>
     * This method will fail on Assert behavior if the error dialog is present on the page.
     * 
     * @param driver WebDriver referenced for the test instance.
     * @param lookup By reference to resolve the desired DOM reference.
     * @return WebElement of the specified DOM.
     */
    private final WebElement safeResolve(WebDriver driver, By lookup) {
        WebDriverWait wait = new WebDriverWait(driver, 1);
        WebElement errDlgRef = null;
        try {
            errDlgRef = wait.until(ExpectedConditions.visibilityOfElementLocated(ERR_DLG));
        } catch (TimeoutException te) {
            //This is actually OK, in that in the majority of cases this is the expected behavior.
            //Granted this is bad form for Unit-Testing, but Selenium at an Integration-Test level changes the rules.
        }

        Assert.assertNull("Unexpected Error Dialog exists in DOM", errDlgRef);

        return driver.findElement(lookup);
    }

This solution is kind of a hammer, but I think it would work. All lookups would need to go through this method. You'd probably also need an analogous method if you use the List WebDriver.findElements(By) function in your test, or a way to abstract it from this one.

Community
  • 1
  • 1
Jeremiah
  • 1,145
  • 6
  • 8