Suppose a webpage has a functionality, it refreshes the whole webpage after a fixed time interval and nothing changes on the webpage.
How we can test this with automation?
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this communitySuppose a webpage has a functionality, it refreshes the whole webpage after a fixed time interval and nothing changes on the webpage.
How we can test this with automation?
One possible way is to force a change in a parallel session or in the database with a script. Then you can keep polling for some seconds until the new data becomes visible.
I would expect the feature is not to refresh the page, but to refresh to show new data. Test the feature, not only the known behavior, thus test why the page refreshes instead. Ask yourself how you would test it manually, I guess automated would be the same.
Also in a single page web-application the page does not refresh, only the content is partly updated, making it even harder to check if the page is really updated.
Another way is to ask the development-team to add an invisible counter to the page which you can read out to check the page has finished refreshing. In short ask them to make the application testable for you.
Ask development to place a HTML comment that has the timestamp, e.g.
<!-- 06-jan-2016 08:09-->
or add it to the actual page so that tools that have access to the full dom can easily access it, but make it not display on the browser
<div display:none">06-jan-2016 08:09</div>
If it has to be present but not visible then
<div visibility:hidden">06-jan-2016 08:09</div>
Another option is to look into http timestamps as mentioned in
http://code.tutsplus.com/tutorials/http-the-protocol-every-web-developer-must-know-part-1--net-31177
Specifically "The Date header field is used to timestamp the request/response message"
You can make use of window.sessionStorage
.
You can ask development to define an pre-defined object, for example reloadflag="true"
.
window.sessionStorage.getItem("reloadflag")
window.sessionStorage.setItem("reloadflag", "false")
If you do not have possibility to make change in development, you can try to hack it, dig in session storage for element, which is always present and "not absolutely necessary" and after reload check you will simply remove that item by window.sessionStorage.removeItem("itemname")
. Beware, do not remove cornerstones of the page. By removing or changing an element you can change behaviour of the application. If there is nothing in session storage, you cannot do it.
I hope this might help
public void waitForPageLoad(WebDriver driver){
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver driver)
{
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); }
};
Wait<WebDriver> wait = new WebDriverWait(driver,30);
try
{
wait.until(expectation);
}
catch(Throwable error)
{
assertFalse("Timeout waiting for Page Load",true);
}}
In addition to that you can also check whether js actions are performed.
return ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0");
An element you interacting become stale when it is destroyed and recreated. Hence when page refreshes, every element in that page become stale. So we can check if the page is refreshed by waiting for an element in that page to become stale. The following Java code illustrate how it is achieved using Selenium webdriver. If the page is not refreshed in 30 seconds, then TimeoutException will be thrown
WebDriverWait wait=new WebDriverWait(driver,30);
try{
wait.until(ExpectedConditions.stalenessOfElement(element));
}catch(TimeoutException e){
System.out.println("Page is not refreshed in 30 seconds");
}
So the problem with refreshes, and this goes into wait logic as well, is that if nothing is different on the web page, there's really nothing to wait for. So the solution we came up with is to inject something unique on the page, trigger the refresh, wait for that thing that we injected to disappear, and then perform whatever wait logic you usually use for that page to load.
JavascriptExecutor js = (JavascriptExecutor) Browser.driver;
js.executeScript("arguments[0].appendChild(document.createTextNode('"
+ textToInject + "'))",
elementToInjectTextInto, textToInject);
This injects a text node into the specified element. Then you would wait for this element to no longer have the text node.
try {
WebDriverWait wait = (WebDriverWait) new WebDriverWait(driver, secondsToWait);
wait.ignoring(NullPointerException.class).until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
return !driver.findElement(By.xpath("elementXPathGoesHere"))
.getAttribute("innerText").contains(textWeInjected);
}
});
} catch (Exception e) {
}
Then we wait for that element to no longer contain that text.
Use a network traffic tool such as Fiddler to capture the traffic; see the Web Debugging page.