Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait for a page refresh in Selenium

This is an extension to my previous question Unable to understand on getting the value

Here the case is like below.

Based on the zipcode the result vary.

Currently I've the below content in a file.

99546
60089

and my code is as below.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test1 {
    public static void main(String[] args) throws InterruptedException, FileNotFoundException {
        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\home\\Downloads\\geckodriver.exe");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        driver = new FirefoxDriver(capabilities);
        driver.get("https://www2.chubb.com/us-en/find-agent-page.aspx");

        try {
            File file = new File("zip.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                driver.findElement(By.xpath(".//*[@id='tbAddress']")).clear();
                driver.findElement(By.xpath(".//*[@id='cphHeroContent_drpDistanceMiles']")).sendKeys("Select Distance");

                driver.findElement(By.xpath(".//*[@id='tbAddress']")).sendKeys(line);
                driver.findElement(By.xpath(".//*[@id='cphHeroContent_drpDistanceMiles']")).sendKeys("2");
                driver.findElement(By.xpath(".//*[@id='cphHeroContent_rdType_0']")).click();

                driver.findElement(By.xpath(".//*[@id='cphHeroContent_btnSearch']")).click();

                String title = driver.getTitle().toString();
                System.out.println(title);



                WebElement element = (new WebDriverWait(driver, 10)).until(
                        ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div/div[1]/div[@style='']")));

                String getHeadingTitle = element.getText();

                System.out.println(line + "\t" + getHeadingTitle);

            }
            fileReader.close();
            System.out.println("Contents of file:");
            System.out.println(stringBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Expected Result:

99546   No Results Found
Find Agent Page
60089   1 Agents Found. Please scroll down to see a list of agents.

current result:

99546   No Results Found
Find Agent Page
60089   No Results Found

Issue: I'm using the below code.

WebElement element = (new WebDriverWait(driver, 10)).until(
                            ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div/div[1]/div[@style='']")));

Initially, this block is hidden so i'm waiting till it is visible, from second time, the same block gets updated instead of going invisible and coming back visible. This is the value that is retrieved from the previous submit, on the first submit. Please let me know how can I get the updated values.

Thanks

like image 461
user3872094 Avatar asked Dec 21 '16 08:12

user3872094


People also ask

What would you do to make the WebDriver wait for a page to refresh before executing the test?

until(conditionToCheck); This way you can wait for your element to be present before executing your test2.

What is the wait command in Selenium?

In automation testing, wait commands direct test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.

How can I tell if Selenium site is refreshed?

UPDATE: Page refresh can also easily be verified if there is some textbox element present. Simply use type command to insert some text into the textbox field, then refresh the page, and the text inside textbox should return to it's original state.


2 Answers

The best answer to this question is here: https://stackoverflow.com/a/32278904/349169

WebElement elementOldPage = driver.findElement(By.id("yourid"));

... do login etc ...

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.stalenessOf(elementOldPage));

WebElement elementNewPage = driver.findElement(By.id("yourid"));
like image 173
Chris Avatar answered Oct 21 '22 12:10

Chris


Maybe, you can solved it by asserting that an element which was originally present on the page, is not present on the page right after refresh

  • refreshAndWait or clickAndWait on refresh button

  • assertElementNotPresent page_specific_element -> check that refresh was really executed

  • pause 3000ms -> wait for refresh to end

  • assertElementPresent page_specific_element -> check that refresh was

    really executed and same page has been loaded

like image 35
MatWdo Avatar answered Oct 21 '22 12:10

MatWdo