I am using Waitforpagetoload in my automation,
When i give 20000 test case fails but passes if i give 60000.
when i compare the time by printing the current time in milli secs, the page is not taking more than 50ms.
How exactly the waitforpagetoload function works?
By default the WebDriver waits until the page is completely loaded and you don't need to do nothing. In fact when the WebDriver gets some page will be waiting for the onload event that is generated by the browser once the page is fully loaded.
In case your page is loading data through Ajax after the onload event is triggered, you could use a different solutions to detect when the page is fully loaded such as ask for the elements that are loaded through Ajax, so when all these elements are present in the page you are sure the page is fully loaded.
Are you using the method Waitforpagetoload for any other reason?
Related
I am new to selenium webdriver (java coding). I have been given a website and asked to write script to check if all the pages load properly.I was not sure how to check a page load. One of my friend suggested that when a page load, i should look for last element which gets loaded and use a wait method on that element to check until its visible.
My question is I have more than 50 pages to check. For each page should i use a wait method and check visibility, becoz it takes a lot of time . Is there any other way?
You can solve the issue by following the steps given below (for all 50 pages, one at a time):
Identify an element which is displayed at last in the browser rendering.
Add visibility_of_element_located, to check whether the element is displayed on the web page or not.
Wait for visibility of an element:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "myDynamicElement")) # here, you can use any available locator of your choice, like, By.XPATH, BY.CLASS_NAME etc.
This waits up to 10 seconds before throwing a TimeoutException or if it finds that the element is displayed, will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.
So, you are not actually waiting for 10 seconds to check whether the element is displayed. Once the element is displayed on the page, the method will return immediately within 10 seconds.
Note: 10 seconds is configurable.
You need to look in visual testing direction, and pure selenium not designed for that.
You may try two addons for Selenium:
1) Applitools (https://applitools.com/), cloud based, you could try it for free,
examples is here https://eyes.applitools.com/app/Web/Deploy/Tutorial/tutorial.html
2) Or you could use Sikuli(http://www.sikuli.org/), free, non cloud.
I have a Dynamic web page that refreshes and loads new data. I am using Selenium and I need to wait till the page has finished rendering before I can continue checking the page. I have seen many posts about waiting for the page to load or using explicit waits (implicit wait could solve the problem but is very unelegant and not fast enough). The problem with using explicit waits is not having any info on what will come up after the refresh. I have seen some solutions talking about waiting for all the connections to the server to end but that will not promise me that the actual UI has finished rendering.
What I need is a way to know if a page has finished to render (not load!!)
First of all you DO KNOW what sctructure will come after the page will load. You don't know what tags were not on the page, but you know what DOM will look like. That's why usually it's worst practice to test with page text, but a good one to have a website tested by DOM only. So, depending on what structure appears on the page you will need to have explicit ways waiting for specific tags will appear on the page.
One more solution (but as I told it's not really the best practice) is to wait for document.readystate is "complete". This state will not help in every situation, but if you have something still loading on the page, in more then half cases it will not return complete. So, you should have some kind of implicit state that is executing:
string readyState = javascript.ExecuteScript("if (document.readyState) return document.readyState;").ToString();
and then checking:
readyState.ToLower() == "complete";
btw if you will use protractor as an angular js application test executor, it's waiting for angular page loaded by default, or in some difficult situations you can use:
browser.waitForAngular();
OR do something in a callback
button.click().then(function() {
// go next step
});
I have had similar problem with the product I am testing. I had to create special functions for click,click_checkbox,sendKeys and getText for example. These include try catch methods for cathcing NoSuchElement StaleElementReference exceptions. Also it retries if the actions fail, like sending the wrong text.
document.readyState
This isnt enough for you. It only waits for the DOM to load. WHen the HTML tag are loaded, it returns "complete". So you have to wait for each individual item.
This works for me well with dynamically rendered websites - I do not have a time constraint in getting the data as it would run in background for me, if efficiency is an issue may be this is not the solution for you (50s is a huge time for any website to load after the ready state is completed) :
Wait for complete page to load
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
Make another implicit wait with a dummy condition which would always fail
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'" + "This text will always fail :)" + "')]"))); // condition you are certain won't be true
}
catch (TimeoutException te) {
}
Finally, instead of getting the html source - which would in most of one page applications would give you a different result , pull the outerhtml of the first html tag
String script = "return document.getElementsByTagName(\"html\")[0].outerHTML;";
content = ((JavascriptExecutor) driver).executeScript(script).toString();
I am testing a web application which needs to refresh one of the header elements every 5 seconds. That header element updates all the users with a message whenever a Quote/Policy is issued through anyone using this same application in a group. So for that, our developers are doing AJAX calls continuously every 5 seconds on all pages and for all users.
For navigating through different pages, I was initially using implicit wait like,
DRIVER.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
After that continuous AJAX calls, I had to use WebDriver wait explicit function and look for object visibility when navigating through pages; like,
Wait<WebDriver> wait = new WebDriverWait(DRIVER, 20);
wait.until(visibilityOfElementLocated(By.id(objID)));
(Please note visibilityOfElementLocated is another function which also handles exception handling.)
My issue now is, this explicit calls code changes works great when I run the scripts in IE 8. But, when I run this on IE 9 it still behaves the same and endlessly wait for page to load (or wait for that AJAX call to finish). And, if I stop the browser calls (by pressing Esc or x link), my script continues for that page and hangs again for next page.
Any idea, why IE 9 doesn't work as IE 8 does for page load? Is there a way I can debug this?
PS: I tried updating IEDriverServer but in vain. Also, this works fine on Chrome and FF browser.
I can't say why IE 8/9 behave differently.
If you want to wait for AJAX to complete you can call a Javascript statement to check for active calls.
JQuery (which I assume you are using) has $.active which gives you the number of currently running ajax calls. So you can use the WebDriver as JavascriptExecutor and call this JS snippet:
return typeof $ === 'function' && $.active === 0;
When the result is true there is no AJAX in progress and you can continue with your test.
Looks like I figured the reason (sadly not the solution). It's a known issue in IEDriverServer of WebDriver for IE9.
Refer below link:
Issue 6402: IEDriverServer always returns document.readyState != complete for application using SignalR - library
https://code.google.com/p/selenium/issues/detail?id=6402
I'm using Selenium.waitforpagetoload method to wait in an ajax enabled webpage. When I run the test code, selenium waits even after the page has loaded and throws a "timed out waiting for action to complete" Exception. And I may have used waitforpagetoload in places where there isn't even a page load event. I presumed it just skips the part if the page is loaded. My code is like this
selenium.click("id=elmnt_id");
selenium.waitForPageToLoad("50000");
I'm using selenium-java version 2.33.
google Chrome 27.0
I found out that selenium.click() itself implicitly waits for pages to load after the click event. The timeout occurred inside click method. But the page loads after the click event in less than 10 seconds. selenium.click waits for more than 30 seconds and says timed out waiting for action to complete. Any advice is much appreciated. thanks!
I'm not sure exactly how org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad
works, but the package name says "internal.seleniumemulation" . I am not sure what that means but because I don't know what "emulation" means in this context, I would instead choose to use a different method of waiting for the page to load, such as a JavaScriptExecutor or using a FluentWait method in a loop until the element is ready.
I read information about method click of interface WebElement, but not clearly understand what mean such statement "If click() causes a new page to be loaded via an event or is done by sending a native event"?
Here is the full javadoc:
Click this element. If this causes a new page to load, this method
will attempt to block until the page has loaded. At this point, you
should discard all references to this element and any further
operations performed on this element will throw a
StaleElementReferenceException unless you know the element and the
page will still be present. If click() causes a new page to be loaded
via an event or is done by sending a native event then the method will
not wait for it to be loaded and the caller should verify that a new page has been loaded.
This means that click() is a blocking call if a new request in the browser is issued. And returns when the page is loaded.
click() is not a blocking call when it executes some javascript that loads the new page (or parts of it) (ajax). In this case you have to use WebDriverWait and wait for some css-classes / ids / text to appear.