Selenium WebDriver have many different methods and one of them is getCurrentUrl() method. So what is the actual usage of getCurrentUrl() method and when to use it. Well, any one can get a clue about usage of getCurrentUrl() method from its name. It will simply return current URL of browser after loading the page.
How getCurrentUrl() Works?
Let's look at practical example and see how it is working.
package TestPack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class currentUrlTest {
public static void main(String[] args) {
String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
driver.get("https://only-testing-blog.blogspot.com/");
//Get current URL from browser and store it in s
String s = driver.getCurrentUrl();
//Get result of s in console
System.out.println("Current URL is : "+ s);
driver.close();
}
}
Here you can see, We retrieved current browser URL using driver.getCurrentUrl() and then print it in console.
You may like to see how Get method works in webdriver.
You may like to see how Get method works in webdriver.
When you need getCurrentUrl()
You will need it when you wants to verify current loaded URL in your browser during test. Supposing you are clicking on Next button and you want to check if browser URL redirected on expected URL or not. In that case you can get current URL and compare it with expected URL string.
No comments:
Post a Comment