1

I am currently writing some automated tests using Selenium in Java to test a search engine and its results in a webpage, but I have noticed that the findElement calls tend to run pretty slow. I use this call a lot as I iterate through the displayed results to make sure that they are displaying properly (As well as a few other tests).

I want to know if there is a faster method I could use, or if there are other recommendations out there for speeding up this process.

Example calls:

   driver.findElement(By.xpath("//section[@id='results']/ul/li[1]/a/h1")).click(); // choose first result
   driver.findElement(By.className("total"));

I am wondering if perhaps the By.xpath function is slower than the other functions such as the By.className function. I'm relatively new to automated testing, but I feel like the tests I am running shouldn't be as slow as they are. Any help is appreciated. Thanks!

Vicky
  • 2,999
  • 2
  • 21
  • 36
Tyler
  • 197
  • 3
  • 13
  • 1
    Yeah, I think this topic is related to your problem: http://stackoverflow.com/questions/29221463/how-can-selenium-batch-many-iselementdisplayed-calls. – alecxe Jul 13 '15 at 17:30
  • Are they equally slow in different browsers? – automatictester Jul 13 '15 at 17:46
  • @automatictester Yes, it runs (essentially) equally slow in different browsers. Chrome tends to be slightly faster than Firefox and IE in my tests. – Tyler Jul 13 '15 at 18:56
  • @alecxe Though related, it isn't quite what I'm looking for. Ultimately, the response loops back to using the Xpath to pull the display, which is something I have been implementing. I have noticed that when I step through the test in debug mode, the statements execute fairly quickly individually. Could the lag be due to multiple calls in succession? – Tyler Jul 13 '15 at 19:03
  • @TylerAshcroft yeah, this is the bottleneck - both `findElement` and `isDisplayed` trigger HTTP interaction between java selenium bindings and a webdriver. – alecxe Jul 13 '15 at 19:07
  • @alexce Perhaps my follow up questions to that should be put in a separate question post, but is it possible to save the current webpage state in an object for use? (With the intent of it staying in memory rather than being fetched all over again) I guess I'm just confused as to why those commands execute so quickly when I step through the code as opposed to when I run the code. – Tyler Jul 13 '15 at 19:24

1 Answers1

0

I ended up using the findElements() function and iterating through the generated list. A lot of the repetitive tasks I needed to perform was the check the formatting of search results that were being kept in lists in the HTML. The findElements() was useful because it only needed to go to the webpage itself one (with its initial call) and then all the iterations and operations I performed on it could be executed in memory.

The findElements() was also useful for checking for a lack of elements, per the Selenium documentation at https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html. After making the changes to my tests, I saw an average speedup of about %30 (though for some of the smaller tests, the changes were almost negligible...)

Tyler
  • 197
  • 3
  • 13