Trial message and using WebUI.waitForElementVisible

I am new to Katalon and I am doing a trial of the product.

Here is my flow:

  1. I click a button, which then starts a countdown of 60s
  2. After 60s a message pops up on the page. The html definition is as follows:
Brief Message Shown here
This message appears for about 1s and then it disappears.

Now, when I try to detect that message using the groovy statement:
def isVisible = WebUI.waitForElementVisible(locator, 62)
where locator is defined to be something like:
def pullDeviceConfigLocator = findTestObject(‘Object Repository/path/to/message object’)

One of two things will happen:
a) the waitForElementVisible will timeout and isVisible will return false
or
b) an exception is thrown:
Caused by: org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found

I think what is happening is that this UI element is so short lived that either the method waitForElementVisible simply misses that UI element or it does find it but because it is so short-lived, that when it tries to reference / verfies its existence, the exception is thrown.

I have tried using waitForElementPresent, but that returns true right away because that element is present, but just hidden.

I have also tried using waitForElementClickable, but it behaved about the same as the waitForElementVisible (in that it timed out).

Any ideas on how to solve this issue?
No, I do not believe it is a Katalon specific issue. Perhaps something to do with Selenium & related to performance ( ?? )

Overall, my fallback plan is to simply insert:
CucumberKW.delay(60)
It is not an ideal solution, but workable (in that I am not really interested in the transient message, but I am using it as an evergreen solution such that if the timer ever changed from 60 seconds to some other value, then the waiting for the message to appear is always there, at least I am assuming).

I am just wondering if others have encountered similar situations and are there some best practices / guidelines on handling this type of situation.

Thanks!

1 Like

In order to anser to your question, I need to reproduce your problem on my side on my PC. Only when the problem is reproduced in hand, I would be able to look into the detail to see what’s happening inside the system.

I need to see your test case script fully. I would need to know the URL as the test target, which need to be publicly accessible from the Internet.

Unless you provide enough information disclosed, it is impossible for the guys in the Katlaon Forum to answer to your question.

I think you’re right – it’s a synchronization problem. And since Katalon (webdriver, actually) is only “in-sync” as much as you are able to force it so, what you end up with is a flaky test (but you knew that).

Okay. Strap in.

The only thing best suited to handle this scenario is the browser itself. From Katalon, in Groovy, write some code that waits for the browser to say (figuratively speaking) “Okay, I’m done”.

How? Do all the test steps and waiting in JavaScript using async waits and checks – setInterval, something like this…

function doMyChecks() { ... }

function getSentinel() {
  return window.robertlee;
}

function checkWeirdThing() {
  var completed = false, interrupt;
  window.robertlee = ""; // sentinel/signal

  var interrupt = setInterval( function() {
   if(!completed) {
    // do your checks/tests
    completed = doMyChecks();
    if(completed) {
      clearInterval(interrupt);
      window.robertlee = "whatever-you-want";
    }
   }
  }, 250); // reduce this as you please
}

checkWeirdThing();

You obviously need to fill out doMyChecks().

In Groovy, use WebUI.executeJavaScript() to pull that code in, then write a while loop…

def sentinel
boolean done = false

while(!done) {
  String robertlee = WebUI.executeJavaScript("getSentinel()", null)

  //deal with robertlee, set done to true when you're happy.
}

Caveats.

  1. I’ve set NO timeouts here. Right now, that code could run forever. You should add a reasonable timeout in the groovy. Simple loop counter will likely do the job (perhaps using sleep).

  2. This is a barebones solution. It’s a long time since I first wrote code like this, but I use it every day. I can’t promise that I’ve remembered “everything” and my version would be overkill for your needs.

Good luck.

1 Like

@robert.lee I meant to add…

The nice/useful thing about working with sentinels is…

1 – They create a “state” without changing the page in any visual sense.

2 – You can check them by hand in the browser console – just type

window.robertlee

and hit return.

3 – I use a sentinel to pause my test cases for upto 3 minutes giving me time to investigate the DOM etc. A button appears in the browser, top right, that when clicked, cancels the pause(i.e. changes the sentinel value) and the test carries on. Hugely beneficial while developing complex tests. (@mike.verinder KS should provide something like this OOTB).