How can I tell selenide to refesh a page in between retries without raising an error?

Viewed 22

I want to test a feature that requires a pageload to be updated. Since selenide only checks if the element has appeared without reloading the page, it will not pick up on the updated state.

I am currently doing it like this:

element(byAttribute("type", "submit")).click()
try {
    element(byText("some specific text")).shouldBe(visible)
} catch (e: com.codeborne.selenide.ex.ElementNotFound){
    refresh()
    element(byText("some specific text")).shouldBe(visible)
}

This is bad in multiple ways:

  • it catches an error, which is not intended to be caught (otherwise it would have been an exception)
  • it triggers side effects, like creating error-screenshots
  • it only retries exactly once
  • it waits the full first timeout, which may not even be needed. e.g. maybe it could have refreshed immediately then be successful

in case you are wondering: the feature in question is related to a cache being warm or cold. Forcing the cache to be cold at the beginning of the test would probably be possible, but it would also add a lot of complexity (e.g. interacting directly with the database), which I would like to avoid.

1 Answers

I'm not familiar with selenide, but I think you can fix at least several problems you listed with the following changes:

  1. Put the try-catch block inside a while true loop so as if the element found visible break is applied, otherwise continue looping.
    In case you want to avoid infinite loop add some counter there so that in case after X iterations the element was not found visible - leave it.
  2. Decrease the timeout for waiting for the expected condition. You did not share here where you define if, so I don't know how to change that.
    The first paragraph changes could be something like the following:
element(byAttribute("type", "submit")).click()
for(int i=0;i<100;i++){
    try {
        element(byText("some specific text")).shouldBe(visible)
        break
    } catch (e: com.codeborne.selenide.ex.ElementNotFound){
        refresh()
        element(byText("some specific text")).shouldBe(visible)
    }
}
Related