Robotframework: Selenium2Lib: Wait Until (…) Keywo

2019-01-29 13:40发布

问题:

I am using Selenium2 w RF to test GUI of rather complex web application. Since I do get some fails with elements load, would like to know what are differences between keywords:

Wait Until Element Is Enabled locator Wait Until Element Is Visible locator Wait Until Page Contains Element locator

What is the scoope of each one and which keyword is most reliable in case, when I wanna check if element of the page is fully loaded and ready to use? Is there a keyword that checks if a full page is loaded?

回答1:

I don't know what you mean by "scope". They all work in the same scope.

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.

which keyword is most reliable in case, when I wanna check if element of the page is fully loaded and ready to use?

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.

Is there a keyword that checks if a full page is 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.

There is no single solution that works for all apps. It's up to you to understand what the app is doing at startup, and to make the appropriate checks.

For a nice discussion of how to know when a page is fully loaded using raw python and selenium, see the blog post How to get Selenium to wait for page load after a click



回答2:

Wait Until Element Is Enabled

Wait Until Element Is Enabled : This is the property to check if the element is enabled or not within the given timeframe.

  • Waits until element locator is enabled where enabled implies it is not disabled nor read-only.
  • Fails if timeout expires before the element is enabled.

Wait Until Element Is Visible

Wait Until Element Is Visible : This is the expectation for checking that an element, known to be present on the DOM of a page, is visible or not within the given timeframe.

  • Waits until element locator is visible.
  • Fails if timeout expires before the element is visible.

Wait Until Page Contains Element

Wait Until Page Contains Element : This is the expectation for checking that an element is present on the DOM of a page or not within the given timeframe.

  • Waits until element locator appears on current page.
  • Fails if timeout expires before the element appears.

Now, each keyword is used as per the requirement (usecase) and are effective, proven and reliable as listed in Selenium2Library

Note : Selenium2Library has been renamed to SeleniumLibrary since version 3.0. Currently Selenium2Library is just a thin wrapper to SeleniumLibrary that eases with transitioning to the new project. Here you can find SeleniumLibrary and Selenium2Library project pages for more documentation.

Finally, as you look out for a keyword that checks if a full page is loaded or not , there is a practice to check for document.readyState to be equal to complete but it is worth to mention that the Web Client (i.e. the Web Browser) returns back the control to the WebDriver instance only when 'document.readyState' equal to "complete" is achieved though it doesn't garuntees that all the WebElements on the new HTML DOM are visible, interactable and clickable

Here you can find a detailed discussion