Learn Selenium with Java to run Automated Tests

July 2024 · 8 minute read

Selenium is the first thing that comes to mind when planning to automate web application testing. It is not only open source but also a portable framework for web applications that supports Java, C#, Ruby, and Python. Choosing the correct language depends on the application under test, the supporting community, available test automation frameworks, usability, elegance, and seamless build integration.

Popular Programming Languages

Source: StackOverflow Survey 2022

As per StackOverflow 2022 Survey, Java is the fifth most popular back-end technology after JavaScript and SQL. Also, the Java testing framework can help a project in cost management by reducing the project’s over-expenditure or poor-quality management approaches. Going ahead, we will learn Selenium with Java to run automated tests.

Table of Contents

Why do Developers learn Selenium with Java?

A good community of developers to create documentation and resolve issues has helped Java to become the most preferred language among application developers. Thus, writing Selenium test cases using Java has multiple benefits:

Eventually, choosing the correct language varies by project, organization, and individuals driving it. An essential criterion is to know the language in-depth when dealing with Selenium.

Run Selenium Tests with Java

Get Started with Selenium Automation Framework in Java

To learn Selenium with Java, one must combine the different components to start coding.

This section teaches how to set up and run a simple test through Selenium with Java bindings.

Pre-requisites for Setup and Configuration of Selenium in Java

The following components will get started with Java to run Automated Tests:

  • Install Java (JDK)
  • Install Eclipse
  • Selenium Client and WebDriver Language bindings
  • Configuring Selenium Webdriver with Eclipse
  • Creating and Running the first test with Selenium and Java
  • Step 1 – Install Java

    Selenium automation framework Java

    Step 2 – Install Eclipse

    Eclipse is a Java development platform for writing and running code.

    Install Eclipse for Selenium automation framework Java

    Step 3 – Selenium Client and WebDriver Language Bindings

    Selenium Webdriver supports multiple languages, and each language has its client driver. As we are using Selenium with Java, we need to have Selenium Java Client Driver. One can download the client driver from the official Selenium website and check the multiple language client drivers provided.

    Selenium Client and WebDriver Language BindingsOnce downloaded, extract the contents of the downloaded file and then move to the next step, configuring Selenium Webdriver with Eclipse.

    Step 4 – Configuring Selenium WebDriver With Eclipse

    This is a vital step of starting with Selenium. To configure Eclipse with the Selenium Webdriver client,

  • Double-click on the eclipse.exe file to launch it
  • Create a workspace Configure Eclipse with the Selenium Webdriver clientThink of it just like any other folder, which stores all the scripts in one place. One can choose to create as many workspaces as required. Click on Launch to launch the workspace.
  • Create a new Java project by clicking on File-> New-> Java Project and name the project
  • Create a package under this project by right-clicking on the ‘src’ folderConfigure Eclipse with the Selenium Webdriver client
  • Once the package is created, right-click on the package and create a class. Configure Eclipse with the Selenium Webdriver clientOnce the class is created, go ahead with adding the Selenium Jars to the project.
  • To add the Selenium Jars, right-click on the project folder and go to Properties:Tutorial on Selenium Automation with Java Step 9
  • From the Properties window, navigate to ‘Java Build Path’ and click on ‘Add External JAR’s

    Java Build Path for Selenium automation framework Java

    Add the downloaded Selenium Jars and click on ‘Apply and Close.’ Selenium with Eclipse is configured now. Now Eclipse is ready to execute the first script.

    Step 5 – Creating and Running the first test using Selenium and Java

    Post downloading, below is the code snippet to run the first test using Selenium and Java:

    import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FirstTestInSelenium { public static void main(String[] args) { // TODO Auto-generated method stub //setting the driver executable System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe"); //Initiating your chromedriver WebDriver driver=new ChromeDriver(); //Applied wait time driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //maximize window driver.manage().window().maximize(); //open browser with desried URL driver.get("https://www.google.com"); //closing the browser driver.close(); } }

    In the code snippet above, we have used the Selenium keyword driver.get(“URL to open in browser”) to open URL in the desired browser. Other keywords like driver.close help to close the browser window as a cleanup part.

    This was a quick starter to learn Selenium with Java and run Automated Tests. One should also consider the best practices while writing Selenium tests.

    Best Practices while writing Selenium tests with Java

    Some of the essential aspects to remember while writing Selenium tests with Java are:

    Selecting locators are the building blocks of a Selenium script, and using the right one is critical. If incorrect locators are used, they tend to make the script flaky and unreliable. Using ‘ID’ and ‘Name’ locators is easy. They also provide faster execution and are more reliable than CSS and XPath.

    When we talk about testing, it is about testing the software on multiple permutations and a combination of data. The same should be incorporated into the Selenium tests. Multiple data points should drive all Selenium tests, and a data-driven framework helps achieve this.

    For WebElements or a page to load, it is essential to give a specific halt time to the script and avoid failure. Selenium provides certain waits like ‘Implicit’ or ‘Explicit’ to achieve this. Both these waits halt the execution of the script until it finds the element.

    The moment it finds the element, it continues the execution of the script. ‘Thread.sleep’, on the other hand, stops the execution for the defined period even when it finds the element in the defined interval. This increases the execution time of the script.

    Cross browser testing plays a vital role in testing. Depending on business needs, one may expect the scripts to run on multiple browsers or a specific browser. Selenium frameworks like TestNG provide annotations like @parameters, and JUnit provides annotations like @RunWith, which helps run tests on multiple browsers and corresponding drivers.

    The key to writing a good test is validating the tests. Just like when one writes a test case and mentions the actual and expected results, one needs to assert the tests in Selenium with the help of assertions provided in frameworks like TestNG and JUnit. If the assertions are not used, the testing process is incomplete, as it does not validate the test build’s correctness.

    As a QA tester, it is essential to provide proof of testing for failures with supportive screenshots. The same stands for Automated Selenium testing. In case a test fails, it is vital to have corresponding screenshots. This helps explain the bug to the developer, who can debug it instantly.

    Similarly, from a reporting perspective, to provide insight to the stakeholders, it is valuable to share reports with them, to establish the stability of the product. For this, Selenium provides a default reporting system with frameworks like TestNG and provides further customizations to them using TestNG listeners.

    Learn Selenium with Java to run Automated Tests

    Conclusion

    Cloud-based automated infrastructure like BrowserStack provides instant access to a Cloud Selenium Grid of 3000+ desktop browsers & real mobile devices. Join the testing infrastructure preferred by global teams and developers worldwide for Selenium automation with Java.

    Run Selenium Tests with Java

    ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6snKWdnp7CrnnWoquhZZqWw6J5xaipZpmlqbyurdOem2aslajB