2

I currently use TestNG's assertFalse to make sure a particular element is Not shown when not logged in on a site. eg

 assertFalse(isElementPresent(PageElements.PDFLinkText),"PDF link present when not expected");

Although this is functional, Selenium will wait for the full timeout to ensure it doesn't appear latter during load. Is there a way to make it check for the element Without the implicit wait?

0

1 Answer 1

8
+50
List<WebElement> cheeses = driver.findElements(By.className("cheese"));

findElements() does not wait, but returns list of elements (located by locator). List is empty if nothing is found (reference).

Also, Selenium elders very strongly advise to use only explicit waits (no implicit). And especially warn about mixing implicit and explicit waits.

In my work experience (in Python), converting to pure explicit waits substantially increased the stability of the test (decreased flakiness).

In response to @dzieciou comment about style vs performance:

I always give priority to programmer's productivity, even if generated code will be 10% slower (compilers are getting ever better). Remember, the most important speed in programming is speed to the market.

If 90% of your computing time is spend in communication between your script and the browser, outside of your control and independent of the language you use, then it makes strong sense to use the language in which you are most productive (can write, debug and maintain most code per day).

If you use language in which you are twice as productive, even if such code will be half as slow as say C# code (and it would not be: OOP languages with dynamic dispatch are inherently slower than statically compiled, like C), it still does not matter: Something would take 110 sec instead of 100 sec (your code taking 20 sec instead of 10 sec, rest is spent in libraries and communication outside your control).

So you can deliver twice the value, and server running the test will spend extra 10% time running the tests. Nobody cares, and if: just buy faster server, they are cheap, compared to programmer's salary. If you can buy 10% faster server for 1 month salary, it pays for itself in 1 month. And after that all is just gravy, double your productivity for free.

7
  • Thanks for this. Has a slight overhead in that it needs to parse the whole page to create the list, but its reduced the time from about 20 seconds (the implicit wait) for the element to 5.
    – ECiurleo
    May 15, 2015 at 15:37
  • What kind of locators are you using? 5 seconds seems quite slow to me, unless your page is huge, and even then. Do you use XPATH locators? If so, can you use something faster? In order of preference: ID > Name > CSS? May 15, 2015 at 16:30
  • Using unique class names. The page itself isn't massively large so investigating where the delay is coming from
    – ECiurleo
    May 16, 2015 at 17:11
  • Why not just try to find a first matching element with findElement ()? That should go faster than finding all matches.
    – dzieciou
    Jan 19, 2016 at 15:28
  • findElement throws exception on failure. findElements returns empty list. I admit I did not timed either. Especially in Python, empty list is just a falsy value and less disruptive to my mind flow :-) At least for me, my own productivity as programmer is more important and I am willing to let computer to work hard to earn the electricity it uses. Jan 19, 2016 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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