0

I have a Angular SPA application which I would like to automate. When this progress bar is shown:

<div _ngcontent-cln-c1="" class="ngx-loading-text center-center" style="top: calc(50% + 60px + 5px); color: white;">Loading...</div>

I would like to pause globally the execution of Java code. Is it possible to pause Selenium somehow if this div is visible?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • might want to try with WebDriverEventListeners: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/events/WebDriverEventListener.html – pcalkins Jan 22 '21 at 23:35
  • Would you please show me how to configure it globally to wait for the `Loading...` text and pause the execution? – Peter Penzov Jan 23 '21 at 01:00
  • configure one of the options/answers below to run with a webdrivereventlistener (or 2?) Depends on when this might appear. They are fairly new so I don't really know how supported they are and whether they might meet your needs. You might also build them into a sort of "driveractions" class to run before any driver action. Build the architecture to suit your needs. Be sure to consider the time it takes for the loading... element to load/appear. – pcalkins Jan 23 '21 at 18:24

2 Answers2

2

you can use webdriver wait for this :

WebDriverWait wait = new WebDriverWait(driver,30);


wait.until(ExpectedConditions.stalenessOf(loadingelement)));

or

WebDriverWait wait = new WebDriverWait(driver,30);


wait.until(ExpectedConditions.invisibilityOf​(loadingelement)));

The above will wait till the loading elment is invisibile or stale ( means modified or removed)

You can also use:

List<WebElement> elementName = driver.findElements(By.xpath("//div[@class=\"ngx-loading-text center-center\"]"));

while(elementlist.size()){
  Thread.sleep(1000)
  elementName = driver.findElements(By.xpath("//div[@class=\"ngx-loading-text center-center\"]"));
}

The above code will check findelements list is empty else wai 1 second and then try to find again and the loop continues till size is 0

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

invisibilityOf()

invisibilityOf(WebElement element) is defined as:

public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)

An expectation for checking the element to be invisible

Here the expectation is that, the element must be present as well as visible as a pre-condition and the method will wait for the element to be invisible. At this point it is worth to mention that as the argument is of type WebElement, findElement(By by) have to successfully locate the element as a pre-condition. Hence NoSuchElementException can't be ignored.


invisibilityOfElementLocated()

invisibilityOfElementLocated(By locator) is defined as:

public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator)

An expectation for checking that an element is either invisible or not present on the DOM.

Here the expectation is clearly either the element is already invisible or not present in the HTML DOM. In this case the primary mission is the absence of the element which may occur even before the ExpectedCondition is invoked or during the timespan while the ExpectedCondition is active. So here we need to ignore NoSuchElementException as a mandatory measure.


This usecase

To pause the execution of your program you need to induce WebDriverWait and you can use either of the following Locator Strategies:

  • Using invisibilityOf():

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(driver.findElement(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]"))));
    
  • Using invisibilityOfElementLocated():

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352