We have seen in last post which explains how can we load a URL without using any get() and navigate() methods. In this post we will learn in how many ways we can refresh a web page. It is also frequently asked interview question.
During developing automation test scripts, we need to refresh a web page. Manually we can refresh a web page using keys such as F5, ctrl+R or using context menu reload option. We can refresh a web page in many ways in Selenium Webdriver. We will see all ways below:
- By simulating keys actions using Actions and Robot classes.
- Using selenium methods.
By simulating keys actions:
We can simulate keyboard actions using Actions class (Provided by Selenium) and Robot class(Provided by Java AWT).
Using Robot Class:
This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Manually, we use CTRL+R to reload a page which is key event. We will simulate same key event using Robot class as below.
Robot r= new Robot(); r.keyPress(KeyEvent.VK_CONTROL); r.keyPress(KeyEvent.VK_R); r.keyRelease(KeyEvent.VK_R); r.keyRelease(KeyEvent.VK_CONTROL);
We also use F5 to reload a page. We can simulate same key events using Robot class as below:
Robot r= new Robot(); r.keyPress(KeyEvent.VK_F5); r.keyRelease(KeyEvent.VK_F5);
Using Actions class:
Actions class which is a user-facing API provided by Selenium is also used for emulating complex user gestures such as mouse and keyboard events. We can achieve keyboard events using Actions class as well.
//Using F5 key Actions a= new Actions(driver); Action a1= a.keyDown(Keys.F5).keyUp(Keys.F5).build(); a1.perform();
Actions a= new Actions(driver); Action a1= a.keyDown(Keys.CONTROL).sendKeys("r").keyUp(Keys.CONTROL).build(); a1.perform();
Problem with Actions class is that it may work or may not depending on browser and selenium version.So if you want to choose from Actions or Robot, go for Robot class. It will work in all browsers with all version of selenium.
Methods are best way of refreshing a web page. Methods are easy and convenient to use.
// using F5 driver.findElement(By.tagName("a")).sendKeys(Keys.F5); // Using Unicode value for F5 driver.findElement(By.tagName("a")).sendKeys("\uE035"); // using ctrl+R driver.findElement(By.tagName("a")).sendKeys(Keys.chord(Keys.CONTROL,"r"));
Above command may or may not work based on browser and selenium version.
using go() method:
go(index) is used to navigate back and forward as well using negative and positive indexes respectively. go(-1) will navigate back while go(1) will navigate to forward if any history.
using reload() method:
So, we have seen many ways of refreshing a webpage. I will advise stick to refresh() method as it will work for sure always. Keep other methods in mind for interview purpose only.
That’s it guys. I hope you will have a better idea of ways of refreshing a webpage after reading this post.
If you like my posts, please like, comment, share and subscribe. Feedback and suggestions are always welcomed.