Various techniques to Refresh Browser window in Selenium – User friendly Tech help

Scenario: -We need to refresh the current instance of Application, using Selenium?
n
n

n

n

n

n

n

n

n

Refresh Yourself 

n

nSolution:-
n
nApproach1:- 
nUsing “Navigation” commands, we have already read about navigation commands, now we will try to use its methods to refresh the page.
n
nSyntax:- 

n

driver.navigate().refresh();

n

nApproach2:-
nUsing “Get” Commands. We will use “GetCurrentUrl()” command,
nThis command returns the current opened URL of the browser
n
Syntax:- 

n

driver.get(driver.getCurrentUrl());

n

nLogic: – Get the current URL of the opened browser and redirect the browser to the same URL again using Get command.

n

Approach3:-
nUsing both “Navigation” + “Get” interface commands.
n
nSyntax:-

n

driver.navigate().to(driver.getCurrentUrl());

n

nLogic:– Get the current URL and navigate to that(same as approach2 above).
n
nApproach4:-
nUsing “sendKeys
n
nSyntax:-
nUsing Keys Method,

n

driver.findElement(By.id("Contact-us")).sendKeys(Keys.F5); 

n

n
nUsing Ascii Code,

n

driver.findElement(By.id("Contact-us")).sendKeys("uE035");

n

nLogic:-Refresh the page by sending the “F5” Command or its Ascii equivalent code.
n
nNote:- Same can be achieved using “Action” class of Selenium.
n
nSyntax:-
nActions actions = new Actions(driver);
nactions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
n
nApproach5:-
nUsing ExecuteScript method
nNote:- This method is used to execute Javascript on browser.
n
nSyntax:-

n

driver.executeScript("history.go(0)");

n

nLogic:- We will utilize the History object (it contains the URL visited by User) and its Go method, to load the specific URL from the History list. 0, means the current URL.
nThus “History.Go(0)”, means opening the current URL from the history list.
n
nNote: – Go (-1) and Go (1) will refer to back one page and forward one page respectively.
n
n

n

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class RefreshMe {
//Counter Variable
static int iCnt =1;
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
System.out.println("Launching Browser");
//Opening the URL
driver.get("http://www.uftHelp.com");
//Refresh the browser Using 'Refresh' command
driver.navigate().refresh();
System.out.println("Approach"+increment()+": Refreshed browser using 'Refresh'command");
//Refresh the browser Using 'Get' command
driver.get(driver.getCurrentUrl());
System.out.println("Approach"+increment()+": Refreshed browser using 'GetCurrentUrl' Command");
//Refresh using Navigate and Get
driver.navigate().to(driver.getCurrentUrl());
System.out.println("Approach"+increment()+": Refreshed browser using 'To' Command");
//Refresh me using SendKeys
//Adding Explicit wait so that element is visible before action can be taken
//otherwise it will show nosuchelementexception
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("contact-Us")));
driver.findElement(By.id("contact-Us")).sendKeys(Keys.F5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("contact-Us")));
System.out.println("Approach"+increment()+": Refreshed browser using 'Keys'command");
driver.findElement(By.id("contact-Us")).sendKeys("uE035");
System.out.println("Approach4: Refreshed browser using 'Ascii' value");
//Using ExecuteScript JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("history.go(0)");
System.out.println("Approach"+increment()+": Refreshed browser using 'ExecuteScript' Command");
//Closing the browser window using Quit method
//driver.quit();}
//Function to increment the counter
public static int increment() {
return iCnt++; }}

n@ImageCreditn

More Examples

Was this article helpful?
YesNo

Similar Posts