testsigma
Topics
left-mobile-bg

Selenium Get Current URL using Java: Tutorial

June 18, 2024Shreshtha Gokhru
right-mobile-bg
Selenium Get Current URL using Java: Tutorial
image

Start automating your tests 5X Faster in Simple English with Testsigma

Try for free

A URL (Uniform Resource Locator) is a web address that specifies the location of a resource on the internet. The URL of an application typically refers to the web address or endpoint that allows users to access or interact with the application.

The URL of an application can vary depending on the specific application and how it is hosted or deployed. For example, if the application is a web-based application hosted on a server, the URL may look something like this:

http://www.example.com/my-application/

If the application is a mobile application, the URL may refer to a specific endpoint or resource that the application interacts with through an API (Application Programming Interface).

It is important to test the URL-related functionality, and Selenium helps us validate URL or related content. 

getCurrentUrl() is a method in Selenium WebDriver that retrieves the current URL of the web page opened in the browser. This method returns a String value representing the current URL of the web page.

Let’s get started!

Pre-requisite

To get started with Selenium automation getCurrentUrl(), the following components are necessary:

  1. Download and Install JDK

  2. Download and Install IDE (Integrated Development Environment) like Eclipse, and IntelliJ

  3. Selenium Client Configuration

  4. Integrate TestNG with IDE

  5. Browser drivers for Firefox, Chrome, IE, etc.

Selenium and Selenium WebDriver

Selenium is an open-source test automation framework that automates web browsers. It is used for functional and regression testing of web applications and supports a variety of programming languages, including Java, Python, C#, and Ruby.

Selenium WebDriver is a component of the Selenium framework that automates the interaction of a web browser with a web application. It provides a programming interface for controlling and automating web browsers. This allows users to perform actions like clicking buttons, filling out forms, and navigating between pages. WebDriver supports several popular web browsers, including Google Chrome, Firefox, Safari, Microsoft Edge, and Internet Explorer.

Selenium WebDriver provides a rich set of APIs that enable developers to write automated tests in their preferred programming language, making it a flexible and powerful tool for automating web applications. Its key features include support for multiple browsers and operating systems, the ability to interact with web elements using a variety of locators, and the ability to wait for elements to load and perform actions asynchronously.

Overall, Selenium and Selenium WebDriver are popular tools in the field of test automation, providing a robust and flexible solution for automating web application testing.

How to get the current URL in Selenium – Java?

In Selenium-Java, you can use the getCurrentUrl() method of the WebDriver interface to get the current URL of the web page.

Method name – getCurrentUrl()

Code Snippet:


import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class CurrentUrl{ public static void main(String[] args) { System.setProperty(“webdriver.chrome.driver”, “C:\Users\Sgokhru\Desktop\Java\chromedriver.exe”); WebDriver driver = new ChromeDriver(); String url = ” https://testsigma.com/”; driver.get(url); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // get the current URL String strUrl = driver.getCurrentUrl(); //Print the URL System.out.println(“Current Url is:”+ strUrl); //Close the browser driver.close(); } }

In this example, we first create a new instance of the Chrome driver and navigate to a web page using the get() method. Then, we call getCurrentUrl() to get the current URL of the web page and store it in a String variable called currentUrl. Finally, we print the current URL to the console and close the browser using the close() method.

getCurrentUrl() returns a String that represents the URL of the current web page. This method can be useful for verifying that the browser has navigated to the expected URL, or for capturing the current URL for later use in your test script.

Conclusion

The getCurrentUrl() method in Selenium Java is a useful method that returns the URL of the current web page being displayed in the browser. It verifies that the browser has navigated to the expected URL after performing some action, such as clicking a link or submitting a form.

This method is part of the WebDriver interface, which is the main interface used to control the browser in Selenium. It can be called on an instance of the WebDriver object, which is typically obtained by instantiating a subclass of the WebDriver interface, such as the ChromeDriver or FirefoxDriver.

Frequently Asked Questions

What are the different methods to open a URL in Selenium?

There are several methods in Selenium to open a URL:

  • get(): The get() method is the most commonly used method to open a URL in Selenium. It opens the specified URL in the current browser window or tab. Example: driver.get(“https://www.example.com”)

  • navigate().to(): The navigate().to() method is also used to open a URL, but it provides more flexibility than get(). It allows you to navigate back and forward through the browser history, and also to refresh the page. Example: driver.navigate().to(“https://www.example.com”)

How to get the current URL in Selenium using JavaScript executor?

You can get the current URL in Selenium using JavaScript executor in the following way:

JavascriptExecutor js = (JavascriptExecutor) driver; String currentURL = (String) js.executeScript(“return window.location.href;”); System.out.println(“The current URL is: ” + currentURL);

Explore Testsigma, a low code test automation tool, to automate your end-to-end UI tests for web, mobile, desktop and APIs, from the same place, 5x faster.

Start here
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

RELATED BLOGS


Top 6 Game Testing Tools You Need to Know
RAUNAK JAIN
TEST AUTOMATION
Power of POC in Testing: Your Exclusive Guide to Success
VIJAYARAGHAVAN VASUDEVAN
AUTOMATION TESTINGTEST AUTOMATION
Test objects in software testing | Types & How to Create it?
RANJANA KODLEKERE
TEST AUTOMATION