Categories
Uncategorized

Various techniques to Refresh Browser window in Selenium

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

Webdriver Refresh
Refresh Yourself 


Solution:-

Approach1:- 
Using “Navigation” commands, we have already read about navigation commands, now we will try to use its methods to refresh the page.

Syntax:- 

driver.navigate().refresh();


Approach2:-
Using “Get” Commands. We will use “GetCurrentUrl()” command,
This command returns the current opened URL of the browser

Syntax:- 

driver.get(driver.getCurrentUrl());


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

Approach3:-
Using both “Navigation” + “Get” interface commands.

Syntax:-

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


Logic:– Get the current URL and navigate to that(same as approach2 above).

Approach4:-
Using “sendKeys

Syntax:-
Using Keys Method,

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



Using Ascii Code,

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


Logic:-Refresh the page by sending the “F5” Command or its Ascii equivalent code.

Note:- Same can be achieved using “Action” class of Selenium.

Syntax:-
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();

Approach5:-
Using ExecuteScript method
Note:- This method is used to execute Javascript on browser.

Syntax:-

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


Logic:- 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.
Thus “History.Go(0)”, means opening the current URL from the history list.

Note: – Go (-1) and Go (1) will refer to back one page and forward one page respectively.


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++;

}
}

One reply on “Various techniques to Refresh Browser window in Selenium”

Leave a Reply

Your email address will not be published. Required fields are marked *