During test automation of web-based application, there comes a need for the page to be refreshed multiple times for all web elements to be loaded completely. On the initial page load, some web elements might be loaded while it takes a second page refresh for all web elements to be loaded. This can be done using the refresh command provided by Selenium web driver. Browser refresh operation can be performed using the following ways in Selenium. We will discuss the below mentioned ways in detail throughout the article.

  1. Driver.navigate.refresh command
  2. Send Keys command
  3. Driver.navigate.to command

Driver.navigate.refresh command:

This is the inbuilt method for performing page refresh operation provided by Selenium web driver. This command is the most commonly used command across test automation for performing a page refresh operation. Refresh command can be used in a simple way as mentioned below.

driver.get("http://demo.guru99.com/selenium/guru99home/");
driver.navigate().refresh();

Navigation is an interface that is used to perform various browser operations like navigating to the previous page, navigating to next page, page refresh, browser close. Navigation interface methods can be accessed using the command driver.navigate(). Refresh method of Navigation interface does not take any arguments or return any values.

Example:

Test Scenario:

  1. Open firefox browser with web page - http://demo.guru99.com/selenium/guru99home/
  2. Once the page is loaded successfully, refresh the web page using driver.navigate.refresh method
  3. Close the browser using driver.close() method

Code

package Guru99Demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class RefreshDemo {
public static void main(String args[]) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D: \\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/selenium/guru99home/");
driver.manage().window().maximize();
driver.navigate().refresh();
driver.close();
}
}

Code Output:

Initial page load

After page refresh operation… the video has changed

Multiple other ways to refresh a page

1) Get method:

Get method can be used in a recursive way to refresh a page. In order to achieve this, we need to pass another method as an argument to the get method.

Example:

driver.get("https://www.guru99.com");
driver.get(driver.getCurrentURL());

2) Navigate method:

This method uses the same concept of recursion as mentioned above. getCurrentURL() method is passed as an argument to driver.navigate.to method.

Example:

driver.get("https://www.guru99.com");
driver.navigate.to(driver.getCurrentURL());

3) Send Keys method using F5 Key:

This is the second most commonly used method to refresh a page in Selenium. It takes the refresh key (F5 Key) as an argument to send keys method. Since send keys works only on web elements rather than the browser, we must initially identify a valid web element on the web page and then use the send keys method. This can be accomplished as shown below.

Example:

driver.get("https://www.guru99.com");
driver. findElement(By.id("username")).sendKeys(Keys.F5); 

4) Send Keys method using ASCII Code:

This method uses the same concept as above, but instead of passing the F5 key as an argument, we send the ASCII Code of refresh key as an argument. This can be accomplished as shown below.

driver.get("https://www.guru99.com");
driver. findElement(By.id("username")).sendKeys(“\uE035”); 

Summary:

  • Page refresh operation ensures that all the web elements of a page are loaded completely before we proceed with our test cases automation
  • Most commonly used method for page refresh in Selenium is the driver.navigate().refresh() method
  • Get method and navigate methods include the recursive ways to refresh a page in Selenium
  • Send Keys method can be used to refresh a page either by passing F5 key as an argument or its corresponding ASCII Code

 

YOU MIGHT LIKE: