Selenium- "Waiting for xxxx"- Chrome - selenium

Website I am testing needs to load libraries from xxxx server.
Sometimes it takes to long and website display information Waiting for xxxx (I am using Chrome WebDriver).
Is it possible to treat it as an error and catch it using Selenium?

Sometimes when it takes to long and you see website display information Waiting for xxxx it is not actually an error. It means the the JavaScripts and the AJAX Calls associated with the Website are getting executed while the Page Loads. Once the JS & AJAX Calls gets executed completely, the Page Loading completes.
Now as per your question Is it possible to treat it as an error and catch it? the Answer is Yes.
This can be achieved by using the pageLoadTimeout method which Sets the amount of time to wait for a page load to complete before throwing an error.
Defined as pageLoadTimeout(long time, java.util.concurrent.TimeUnit unit), an example will be as follows :
driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
The error thrown is as follows :
Exception in thread "main" org.openqa.selenium.TimeoutException: Timeout loading page after 2000ms
You can find a detailed implementation of pageLoadTimeout in pageLoadTimeout in Selenium not working discussion.

Related

Issue with page load timeout and next task is directly sent to except

I was running a python script responsible for web scraping some pages using multiprocessing techniques.
In order to wait for a page to get fully loaded, I used the method set_page_load_timeout() set to 30s.
I put a driver.get() inside a try-except structure.
However, I observed that in my case, when all the 5 Chrome instances are busy with 5 different pages, the next page pulled from the task queue goes to my exception and stored in my error file. I presume this occurs since the Chrome instances are still trying to load the 5 previous pages and not able to redirect to this new page.
How could I get over this issue?

a wait that stops the test till nothing is happening in the web page anymore

I am using Java and Selenium to write a test. Many times in my test i have to wait till the page or some part of it loads something. I want to wait till the load is finished. I found these two:
1) pageLoadTimeout
2) setScriptTimeout
But I don't know the exact difference between them or even if they are what I need.
The document says:
setScriptTimeout: Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
and
pageLoadTimeout: Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
but I don't really get it.
what I need is a wait that stops the test till nothing is happening in the web page anymore.
Define "nothing is happening in the web page." Now define it in such a way that will satisfy every web page ever created, or that ever will be created. Did your definition include that no more network traffic is coming over the wire? Did your definition include that the onload event has fired? How about document.readyState returning a value meaning "complete?" Did you take JavaScript execution into account? How about JavaScript scheduled using window.setTimeout()? WebSockets? Service Workers? What about <frame> or <iframe> elements?
Now you know why WebDriver doesn't have a "wait for page complete" type of wait. The items mentioned above don't even begin to scratch the surface of the list of things a user could legitimately use in a definition of "nothing is happening on the page anymore." The correct approach would be to wait for the element you want to interact with on the next page to be available in the DOM. The WebDriverWait class is ideally suited for exactly this.

How to continue test when the page still not completely loaded in selenium

Actually, I am creating automation testing for an e-commerce website. Actually, the website have function lazy load or something. I am testing it on UAT server. So, it will load the page slowly because the specification of the server. It takes more than 60 sec or more to load all the resources from the webpage. So, when I am trying to create selenium automation, it always waiting more than 60 sec to continue the next step (because waiting the page fully loaded). Please, someone give me tips how to continue run the test step after 10 seconds wait the page to load. It won't throw an exception, just continue the test step.
Not possible.
If you find some element and try execute some action while loading you will get stale element error + due loading issue you will have a lot of failed tests and it will take a lot more time to debug.
Automation means to execute fast and have reliable results.
It seems that this environment is not built for automation, you should request more resources.
As an alternative maybe you can use a headless driver or see if you can put the same build on a VM.
Why this is an issue: Selenium needs to wait for each request to be complete.For example when you request a page, if the page is not received entirely and the server still sending info then the request is not done, it is logical that you need a complete request in order to continue.
You should address this to your Project Manager/QA Lead and ask for advice/option on how to handle this.
Please note that these costs should be included/added in the automation price.You need to address this in a simple way:
good server -> automation runs smoothly and fast and the testing is
done faster
bad server -> unable to run automation since is not reliable and each
test has a high rate of failure => alternative X day(s) of
manual testing for each build
If this would be a coding issue like some delayed ajax request then you would have some solutions, devs could help, but if is an infrastructure/resources issue then if not depending on you, and you cannot solve it.
You could use try any type of wait implicit/explicit, explicit would throw some exception, but this is not a solution for poor resources.

How to `executeScript` before page load by WebDriver in selenium?

I want to use webdriver in nodejs to control a website which use ajax very heavy, especially it always have http request to server.
When I use driver.executeScript , I found the promise returned almost never resovled.
I checked the website, found that it use a keep-alive http request loop to communicate with server. That means, it will always have at least one live connection to server for 30s, then another connection for another 30s, again and again. which cause document.readyState keep interactive instead of completed, and then driver.executeScript almost be blocked forever.
I had tried driver.manage().timeouts().pageLoadTimeout(PAGE_LOAD_TIMEOUT_MS) but it only throws a exception after timeout.
I also tried to stop the connections by press ESC on the browser window. after that, it seems driver.executeScript can run immediately. But I didn't find any function like window.stop() in WebDriver API.
so is there have a way to resolve this problem? Either should be ok like:
run executeScript right now, regardless of whether page is loaded;
a webdriver api like driver.browser.stop() could stop all live connections in page.
Nodejs code to re-produce this problem:
const WebDriver = require('selenium-webdriver')
const driver = new WebDriver.Builder()
.withCapabilities(
WebDriver.Capabilities.firefox()
.set('webdriver.load.strategy', 'unstable')
).build()
driver.get('https://wx.qq.com/')
driver.executeScript('return "99% will be blocked, 1% luck to return"')
.then(function (ret) {console.log(ret)})
thanks!
UPDATE:
I found maybe set webdriver.load.strategy to unstable in firefox will be help.
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("webdriver.load.strategy", "unstable"); WebDriver
driver = new FirefoxDriver(fp);
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/4993
https://w3c.github.io/webdriver/webdriver-spec.html#page-load-strategies-1
Links from others got same problem:
http://grokbase.com/t/gg/webdriver/1263dbyws6/how-to-click-stop-button-or-equivalent
I think that if you don't use Implicit time the driver won't wait till it's loaded so you'll be able to execute your script.
If this don't work you can try to use your own timer. Check if doc state is ready, if it's not ready after some timeout then stop it manually (by JS) and run your script.
By using Thread.sleep(secs);. For example, we want to load a page in 3 secs means we write thread.sleep(30000);.

When does driver.get() complete?

When using the InternetExplorerDriver (IE9 Win7) the driver.get() method blocks for some sites I'm testing. The site seems to be completely loaded, as the page is no longer refreshing, but the code just gets stuck at the driver.get() call. (this doesn't happen with the same site for the FirefoxDriver).
Anyone know any reason why this would happen or how best to troubleshoot it?
In particular the WebDriver api states
"the method will block until the load is complete".
How is "load is complete" defined?