0

I am trying to achieve the following:

  • If a specific element exists, click it and go back to the home page.
  • Otherwise, just go back to home page so the test continues on without failing

I have come up with the code below, this but it is really slow. I am not sure there is any better way to implement this? Any comments will be appreciated!!

boolean exists = driver.findElements( By.id("xxx")).size() != 0;

if (exists)
  {
     driver.findElement(By.id("xxx")).click();
     driver.findElement(By.cssSelector("xxx")).click();
  }
  else
  {
     driver.findElement(By.cssSelector("xxx")).click();
   }
Leigh
  • 28,765
  • 10
  • 55
  • 103
user1282634
  • 131
  • 1
  • 6

2 Answers2

4

I worked out what's slowing down the performance. It is this line:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Because of that statement, it will wait for the element to be verified for 30 seconds.

After changing it to:

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

.. it now works like a charm...:)

Leigh
  • 28,765
  • 10
  • 55
  • 103
user1282634
  • 131
  • 1
  • 6
0

What are you using for CSS selectors? You might be able to improve the performance just by tweaking those. Another thing that will slow it down is when the page has too many DOM elements.

It would be helpful to see the CSS selectors and an example of what DOM elements you are scanning over.

For example, if your page is full of 1000 DIV elements, with a class like this:

<div class="smallItem">...</div>
<div class="largeItem">...</div>
<div class="smallItem">...</div>

and you use a css selector like this:

".smallItem"

to select all of the DIV elements, it has to scan over each DOM element and compute on the class attribute.

Community
  • 1
  • 1
Jay Prall
  • 5,295
  • 5
  • 49
  • 79