Task 2: Refresh, Go back and Go forward
In the previous example, we have opened google.com. Now in this task, we are going to refresh the page, go forward and backward.
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Smile Please!");
driver.navigate().back();
driver.navigate().refresh();
driver.navigate().forward();
Have you Noticed? We are not using `driver.get()` to get the URL, we are using `driver.navigate().to()`. Because, using `get()` we can only hit the URL and we can't perform any other operations on that. We may need to perform operations like refresh,going forward and backward. In that case, we have to opt for `navigate` method !
Code explanation
I think the method names are self explanatory, refresh method is to refresh, back method is to go back and forward is to go forward. Not tough right?
You should have a question. What’s the work of this linedriver.findElement(By.name("q")).sendKeys("Smile Please");
We are instructing the program to find the text box in google home page and asking it to type “Smile Please”. That’s the job of this line.Don’t worry about, if you don’t get it How to find the element is our next topic.