3

I know there are types of wait in java

Implicit wait-

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS)

Explicit wait-

WebDriverWait wait = new WebDriverWait(driver, explicitWaitSec);
        wait.until(ExpectedConditions.elementToBeClickable(element));

Fluent wait-

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)                            
        .withTimeout(30, TimeUnit.SECONDS)          
        .pollingEvery(5, TimeUnit.SECONDS)          
        .ignoring(NoSuchElementException.class);

, but I am confused which of the waits and expected conditions will serve me closest to Wait Until Page Contains keyword in Robot Framework?

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
Dev
  • 2,739
  • 2
  • 21
  • 34

3 Answers3

3

There is no direct analog, that's a capability developed explicitly in in Robot Framework.
In the same time, you can achieve it with ExpectedCondition's presenceOfElementLocated() with explicit/fluent wait (the latter is just more customizable version of the first, btw).

For a locator, use this xpath:

//*[contains(., "Your Text Here")]

This is what Robotf Framework actually does, quite clever I must admit.

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
1

Wait Until Page Contains

Wait Until Page Contains is the implementation to wait for the text to appear within the HTML DOM. It is documented as:

Keyword                     Arguments                           Documentation
-------                     ---------                           -------------
Wait Until Page Contains    text, timeout=None, error=None      Waits until text appears on current page.
                                                                Fails if timeout expires before the text appears. See the Timeouts section for more information about using timeouts and their default value.
                                                                error can be used to override the default error message.

Source Code:

@keyword
def wait_until_page_contains(self, text, timeout=None, error=None):
    """Waits until ``text`` appears on current page.
    Fails if ``timeout`` expires before the text appears. See
    the `Timeouts` section for more information about using timeouts
    and their default value.
    ``error`` can be used to override the default error message.
    """
    self._wait_until(lambda: self.is_text_present(text),
                     "Text '%s' did not appear in <TIMEOUT>." % text,
                     timeout, error)

So the equivalent ExpectedConditions can be either of the following:

Note: One significant difference is, while wait_until_page_contains is relative to the current page i.e. the current DOM Tree, the ExpectedConditions are based on WebElements on the current page which makes your Tests much more granular.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Apart from repeating what is in the other answers - e.g. not bringing anything new to the table, the answer is incomplete/incorrect - the keyword `Wait Until Page Contains` does **not** use any locators, it looks in the full DOM for a string. All these keywords expect a specific locator, to first match an element and then check its text. – Todor Minakov Feb 14 '19 at 08:54
  • @DebanjanB thanks, For showing possible more options, which can lined up after `presenceOfElementLocated()` in list of possible solutions, +1 – Dev Feb 14 '19 at 10:47
  • Hmm, the _usecase_ is still not _super clear_ to me. However my answer is based on the condition _Wait Until Page Contains_ which is exclusively based on the **text** and IMO _presenceOfElementLocated()_ isn't meant for this validation. Finally, everything boils down to what worked for you. Good luck :) – undetected Selenium Feb 14 '19 at 10:54
0

There is like more functions like but this three use mostly..

Wait Until Element Is Enabled assumes that the element exists on the page, and will wait until the element is enabled (not readonly, and not disabled). If the element does not exist, it will fail immediately (or after a timeout if you have an implicit wait)

Wait Until Element is Visible assumes that the element exists on the page, and will wait until the element is visible. If the element does not exist, it will fail immediately (or after a timeout if you have an implicit wait)

Wait Until Page Contains Element makes no assumptions about the element. It waits until the element is actually on the page, regardless if it is visible, invisible, enabled, or disabled. It does not require an implicit wait, since this keyword is an explicit wait.

The most complete solution is to wait for it to be on the page, wait for it to be visible, and then wait for it to be enabled.

If the element will always be on the page, you can skip the first check (ie: if there's no javascript that can create or delete the element).

If the element will always be enabled, you don't need to wait for it to become enabled (ie: if there's no javascript to disable or enable the element)

For simple static pages, you really only need to check that an element is visible. Even that isn't usually necessary since selenium doesn't return from opening a page until the page is loaded. The problem comes when the page is dynamic. That is, when there is javascript that can change what is on the page and whether it is visible or enabled, after the html has loaded.

No, because "is loaded" can mean different things in different applications. The browser will set the variable document.readyState to "complete" when it's done loading the html. You can check for that in robot with something like Wait for condition return window.document.readyState === 'complete'. Again, if you have javascript that runs on the page, this may not be sufficient since the page might change after the initial HTML is loaded.

akshay patil
  • 670
  • 7
  • 20
  • The question is how to do `Wait Until Page Contains` in Java. This answer provides only Robot Framework alternatives and as such does not line up with what is asked. – A. Kootstra Feb 14 '19 at 06:50
  • The explanation is not entirely correct - `Wait Until Element Is *` will **not** fail immediately if the element is not present; on the contrary, they will first wait for the element to appear in the DOM, and then check on their specific condition. – Todor Minakov Feb 14 '19 at 07:19
  • 1
    @akshaypatil I was referring to these statements - "... assumes that the element exists on the page...". That's not so - if the element is not in the page, the keywords will not fail directly, they **will** wait for it to appear and then do the other verifications. Hopefully this makes it clearer. – Todor Minakov Feb 14 '19 at 08:56