Tell me more ×
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.

The goal of my test is to assert that a popup does not appear after certain actions. Previously to test if the popup exist, i have used exception handling.

try:

    self.driver.find_element_by_id("fancybox-close").click()

except Exception ('ElementNotVisibleException'):

    print "No popup"

This works fine for the test : to assert if popup exists.

But as soon as i change the goal to : assert if popup does not exist

the exception handling solution becomes very expensive (exception handling takes a lot of time in python) and a test that would execute in 3 secs now takes up to a minute.

Is there a way around this? A quicker way to check if an element does not exist using webdriver python binding ?

share|improve this question
How many times would you estimate that code fragment is executed over the course of your one-minute test? – user246 Jul 17 '12 at 14:20
It is just executed once, but it just stalls on the "except" part for 90% of the 1 minute. – user1411110 Jul 17 '12 at 19:42
That sounds suspicious. On modern hardware, I would expect "slow" exception handling to mean additional microseconds, or perhaps even additional milliseconds, but certainly not 54 seconds. – user246 Jul 17 '12 at 22:57
I should probably use a profiler to get the exact time spent in that try/catch block . But from past test results, the test where no exception was raised ran within 4 - 8 secs while the one where it was raised ran for about a minute or just over. There were no other changes in the code. – user1411110 Jul 17 '12 at 23:23

1 Answer

up vote 1 down vote accepted

I haven't used the Python bindings, but as far as I know they should be equivalent to the Java ones.

If I were you I would try to find the Python equivalents of findElements() and isDisplayed() that are available in the Java bindings.

For example, I would do something similar to this:

// ...
myElementList = driver.findElements(By.Id("fancybox-close"))
if (myElementList.isEmpty()) {
    // The element doesn't exist. findElements, in plural, returns a list of the matching elements, or an empty list if no one is found
else {
    // We know it exists, now we need to know if it's displayed (visible) or not
    if (myElementList[0].isDisplayed()) {
        // This means the element is visible
    else {
        // ...
    }

}

Hope it helps

share|improve this answer
Ignacio, this is a great idea. python bindings have a find_elements() and is_displayed(). I am however having trouble with checking if the list is empty or not. I get an error message saying "built in function id is not JSON serializable. Will have to look in it. But otherwise this is a great idea. Thanks! – user1411110 Jul 17 '12 at 19:39
isEmpty() is a Java method for lists. In Python, I think you should check the length of the list (len(driver.find_elements(...)) == 0)? I cannot check this for myself right now. – Ignacio Contreras Jul 17 '12 at 19:43
In python to check if the list is empty you just have to do if not a : where a =[] is a list. That is what i am doing. if not self.driver.find_elements(By.id("fancybox-close")) : – user1411110 Jul 17 '12 at 19:53
And it doesn't work? – Ignacio Contreras Jul 17 '12 at 19:56
1  
Cool. Now you should look for an equivalent of isDisplayed() (if an element is not displayed it will throw the exception you were using when clicked) for Python as I said in the answer :) – Ignacio Contreras Jul 17 '12 at 20:18
show 6 more comments

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.