How to refresh browser in Selenium WebDriver using different methods

In this article, we will talk about how can we refresh browser using various methods.

Some time it is required to refresh the webpage to accomplish the testing objective. Let us see how this can be achieved.

Method1: Using navigate().refresh() method

Method2: Using JavaScriptExecutor

Method3: Using Actions Class

Mehtod4: Get the current URL & navigate using get() method

Method5: Get the current URL & navigate using navigate().to() method

Look at the below example –
package basicsOfSelenium;

import java.util.concurrent.TimeUnit;

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.interactions.Actions;

public class RefreshBrowserInDiffWays {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("https://www.google.co.in");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  // Method1:  Using navigate().refresh() method
  driver.navigate().refresh();
  
  //Method2: Using JavaScriptExecutor
  JavascriptExecutor js = (JavascriptExecutor)driver;
  js.executeScript("history.go(0)");
  
  //Method3: Using Actions Class
  Actions actions = new Actions(driver);
  actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
  
  //Mehtod4: Get the current URL & navigate using get() method
  driver.get(driver.getCurrentUrl());
  
  //Method5: Get the current URL & navigate using navigate().to() method
  driver.navigate().to(driver.getCurrentUrl());
  
 }

}

Hope this helps!!!!

How to handle Authentication Required popup using AutoIT?

While browsing some websites, Authentication required popup comes asking username and password. Observe below snap.

Now, this can be handled using either robot class or by using AutoIT script.
Let us see how this Authentication Required popup is handled using AutoIT script.
Script used to handle this Authentication popup is as below –
WinWaitActive(“Authentication Required”,””,”10″)

 

If WinExists(“Authentication Required”) Then

 

Send(“username{TAB}”)

 

Send(“Password{Enter}”)

 

EndIf
Add this script in SciTE editor. Save the file with some name by .au3 extension.
Now compile the script by right clicking on it, with respective version.
Once compilation is successful, .exe file will be created with same name.
Now, we can use this .exe file & run this via our selenium Webdriver script.
                        Runtime.getRuntime().exec(“C:/Users/Prakash/Desktop/TestScript.exe”);
That’s, all, this is how we can handle Authentication Required Popup using AutoIT

How to upload file in Selenium WebDriver using Robot class

Upload file using Robot class –

Suggested post to read –  How to upload file in Selenium WebDriver using sendKeys() method

In this article, we will see how can we upload file in Selenium WebDriver using robot class.

Upload file in Selenium WebDriver using ROBOT Class

This is another way using which we can upload file. Here we can do key press and key release using robot class.

This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:

1.   Click on the File Upload / Choose File button, so that the File Upload dialog is displayed.
        driver.findElement(By.xpath(“//*[@id=’uploadfile_0′]”)).click();

2. Copy your file’s absolute path to the clipboard
    StringSelection ss = new StringSelection(” C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx”);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

3. Paste the file’s absolute path into the File name field of the File Upload dialog box

    //native key strokes for CTRL, V and ENTER keys

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

Refer below code –

package basicsOfSelenium;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FileUploadInSelenium_UsingRobotClass {

 public static void main(String[] args) throws AWTException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://demo.guru99.com/selenium/upload/");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  //open upload window
     driver.findElement(By.xpath("//*[@id='uploadfile_0']")).click();

     //put path to your image in a clipboard
     StringSelection ss = new StringSelection("C://Users//Prakash//Desktop//Desktop//TOSCA tutorial.pptx");
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

     //imitate mouse events like ENTER, CTRL+C, CTRL+V
     Robot robot = new Robot();
     robot.delay(250);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.delay(90);
     robot.keyRelease(KeyEvent.VK_ENTER);

  
 }

}

Refer below articles to learn more about Actions class in Selenium WebDriver –

Hope this helps !!