Search code examples
javaselenium-webdriverjunithtmlunitremotewebdriver

Selenium remotewebdriver timeout should (in this case) return true


I am using Selenium remotewebdriver with htmlunit and have a small collection of target URLs that I need to fetch through an IPS - some of the URLs should trigger IPS to blackhole the traffic, in which case, the correct "pass" scenario for that particular test is that the request should time out. These are in Java, btw. So, something like this:

@Test
public void TESTING_IPS_THIS_SHOULD_TRIGGER_ALERT_AND_BLACKHOLE() {
    driver.get("http://testmyids.com");
    //I would like to do a simple assert here that timeout is true;

}

So my question is - what is the most simplistic way to achieve this in a manner more akin to assertTrue that driver timeout occurred?


Solution

  • In case of timeout WebDriver throws a TimeoutException. To check if this actually happens, simply declare to expect this exception in the annotation of your test method like this:

    @Test(expected = org.openqa.selenium.TimeoutException.class)
    public void TESTING_IPS_THIS_SHOULD_TRIGGER_ALERT_AND_BLACKHOLE() {
        driver.get("http://testmyids.com");
        //I would like to do a simple assert here that timeout is true;
    
    }
    

    If the exception is thrown, the test will pass. If not, the error message is:

    java.lang.AssertionError: Expected exception: org.openqa.selenium.TimeoutException