0

I use Cucumber-JVM with Serenity (Reporting library). The step implementation uses selenium for browser automation.

I use methods like

waitForRenderedElementsToBePresent(By.cssSelector(<css>));
waitFor(ExpectedConditions.visibilityOf(getDriver().findElement(By.cssSelector(<css>))));

but still my tests suffer from flakiness sometimes. I do not want to use explicit wait.

Is there any way I can make my tests more reliable.?

Manisha Awasthi
  • 449
  • 1
  • 5
  • 16
  • Why don't you want to use explicit wait? It's there for exactly this purpose. – JeffC Nov 25 '15 at 19:52
  • I am already using explicit waits but it is not working very fine. Sometimes it passes and sometimes it just fails randomly – Manisha Awasthi Nov 25 '15 at 19:55
  • When does it fail and why? – JeffC Nov 25 '15 at 22:07
  • example failure: It is trying to assert on an element which is not loaded yet and hence it failed. Though I have added an explicit wait before assertion so that it waits until element is visible and this failure is very random. Sometime it is passed, sometime it fails. – Manisha Awasthi Nov 25 '15 at 22:13

2 Answers2

2

I've struggled a lot with the same issue, I did not really like using ExpectedConditions neither using explicit waits. Eventually I started using the Awaitility framework.

It basically allows you to write code like this:

await("Element did not show foo.").atMost(60,TimeUnit.SECONDS) .until(() -> driver.findElement(By.id("some-element").getText().contains("foo"));

I found it to work really well with Selenium WebDriver plus I think it makes your code more readable.

thebobblob
  • 138
  • 1
  • 1
  • 7
  • Thanks much for the reply. Let me try this in my code – Manisha Awasthi Nov 25 '15 at 19:28
  • Just a question, does Awaitilty also handles the rendering if the element in UI. Because some time it happens that DOM has the element but not visible because rendering is still in progress? – Manisha Awasthi Nov 25 '15 at 19:45
  • I think it's using the DOM and not watching the GUI(because it's still using Selenium methods). If it's not in the DOM, findElement will return null and thus it will not comply to the criteria of your await statement. It will keep polling for duration of the timeout you've provided. – thebobblob Nov 25 '15 at 21:01
0

Have you tried using selenide? https://selenide.org/

$(element).shouldBe(visible).shouldHave(text("foo"));
tripleee
  • 175,061
  • 34
  • 275
  • 318
Amuthan
  • 11
  • 1