Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Selenium2/WebDriver is optimized for positive testing paths. My web application hides elements based on user permissions. I am looking for an element and throwing-catching the exception when it does not find it and the command fails (I'm using Java). This verifies the element is not present. I've seen this question: Quicker way to assert that an element does not exist but I am wondering if there is an alternative way besides if/else statements.

Edited: The code currently being used is shown below. This currently takes ~60+ seconds to run.

@User246: I guess my main question is there a parameter or something like that to pass to Selenium that tells it to only look for the WebElement for X seconds before throwing the exception. Optimally I would set it to 1 second since I expect it to not be found and just want to catch the exception. I hope this is more clear.

protected void assertAddButtonIsHidden() throws Exception
{
    boolean found = true;

    try
    {
        WebElement addButton = findElementById("add");
        addButton.click();
    }
    catch (StaleElementReferenceException x)
    {
        found = false;
    }

    assertTrue(!found);
}
share|improve this question
    
I do not understand the question. You seem to object to using Exceptions. That leaves calling a function that returns a value, but of course something will need to behave differently depending on that value. In Java, we use if-statements for that. You could use a switch statement instead, but surely an if-statement would be easier. Perhaps you could add a code fragment that describes what you are trying to do. –  user246 Sep 20 '12 at 19:37
    
I added the code and updated the question. Hopefully it is a little clearer. I am a little long-winded at times. –  squeemish Sep 20 '12 at 19:47
    
When you say the code takes 60 seconds to run, do you mean calling assertAddButtonIsHidden just once takes 60 seconds? –  user246 Sep 20 '12 at 20:44
    
That is correct. The entire snippet up there runs for ~60+ seconds, hanging while trying to find the element that is not present –  squeemish Sep 20 '12 at 20:48
add comment

1 Answer 1

up vote 1 down vote accepted

I have not used the java implimentation but can you utilize the ImplicityWait method to configure the timeout?

This is a c# example that gets set in the Setup stage of the unit test.

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0,30));

This is the API doc for the method.

share|improve this answer
    
Surprisingly, this worked great. I was under the impression from looking at the API doc previously that it could only be used for EXTENDING the time it looked, but I set it to 150 milliseconds and now a test that took 60 seconds takes 7. Thanks a lot! –  squeemish Sep 21 '12 at 14:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.