March 12, 2020

How to use Selenium Automation testing with Java?

Selenium is the first thing that comes to mind when one wants to automate web application testing. Selenium is a valuable tool as it is not only an open-source but also a web application development platform for portable applications that support multiple languages such as Java, C #, Ruby, Python. The choice of the correct language depends on the application being tested, the supporting community, the available test automation systems, usability, elegance and, of course, seamless integration of the project.

Developers Prefer Selenium with Java, Why?

A strong developer community to build documentation and fix problems has helped Java become the most favored language among the developers of the application. Writing selenium test cases using Java thus has many advantages: Java supports Selenium. Testers can use the active community of developers and comprehensive documentation to write test cases. Java-written programs are simpler than other common languages such as Python. Java is more commonly used in commercial applications compared to other programming languages such as Python and thus incorporates Selenium tests.

Developers Prefer Selenium with Java, Why?

A strong developer community to build documentation and fix problems has helped Java become the most favored language among the developers of the application. Writing selenium test cases using Java thus has many advantages: Java supports Selenium. Testers can use the active community of developers and comprehensive documentation to write test cases. Java-written programs are simpler than other common languages such as Python. Java is more commonly used in commercial applications compared to other programming languages such as Python and thus incorporates Selenium tests.

The following components are required to start automation:

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

Java Installation:

To write and run Java programs, you need to install a Java development kit that includes the JRE (Java Runtime Environment).

JRE is a JDK child which comes with the installation of JDK. Even for running applications involving Java dependency, JDK must be enabled. One such application is Eclipse IDE Download and install Java, and set the path for the system. Once the path has been set, the installation can be checked by typing java-version on the command prompt, which provides information on the java version installed. For a better understanding please refer to the picture below.

Eclipse installation:

Eclipse is a platform that allows Java developers to write and run their code. From their office website access Eclipse. One can go with the necessary option, based on the operating system. Extract the downloaded file, once downloaded.After completing, the eclipse.exe can be seen in the folder of the eclipse.

Selenium web drivers:

What is selenium web driver?

It is a collection of open source APIs used to automate testing of web applications.It supports browsers like chrome,firefox,opera,etc.However, the selenium web driver is only used in web applications and not in the windows based applications.It runs on any platform windows or linux.

Selenium Webdriver supports multiple languages and has a client driver for each language. We need to have a’ Selenium Server Client Driver’ since we use selenium with Java. From the official Selenium web site, one can access the client software. One can see Selenium’s multiple language system drivers.

After downloading extracts, contents of the downloaded file.Proceed to the next step.

Selenium webdriver with eclipse:

In the next step configure selenium web driver with eclipse.

By following the procedure you have configured selenium and eclipse .Now you can run selenium automation testing code with java.

Running selenium testing on 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) {

System.setProperty(“webdriver.chrome.driver”, “.\\Driver\\chromedriver.exe”);

WebDriver driver=new ChromeDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.manage().window().maximize();

driver.get(“https://www.google.com");

driver.close();

}

}

What are the best practices?

These are the important aspects to remember while using Selenium with java.

Right locator –Choosing locators is the building blocks of a selenium script, so it is important to choose the appropriate one. If incorrect locators are used they continue to make the script inaccurate and flaky. It’s quick to use the locators ‘ ID’ and’ Name.’ They also offer quicker execution and are more robust compared to CSS and XPath.

Test-driven script-When we talk about testing, it’s about checking the program on various permutations and data mixture. The same applies to the Selenium samples. All Selenium tests should be guided by multiple data points, and using a data-driven system helps to achieve this.

Thread.sleep–It is important to give the script a clear halt period for Web-elements or a page to load, and to prevent script failure. To accomplish this, selenium offers other waits, such as’ Implicit’ or’ Explicit.’ Both of these waits pause script execution until the element is identified. The moment they find the item, the execution of the script begins. On the other hand,’ Thread.sleep’ prevents execution even when it finds the entity in the specified interval for the defined period. It raises script execution time.

Browser specific texts should not be done –Cross-browser testing plays a vital role in testing. Depending on business needs, one might expect the scripts to run on several browsers or a particular browser. Selenium frameworks like TestNG provide annotations such as @parameters and JUnit provides annotations like @RunWith, which helps in running tests on multiple browsers and corresponding drivers.

Screenshot- As a QA tester, it is important to have supporting screenshots to provide evidence of fault testing. The same is true for the Automated Selenium check. It is important to have corresponding screenshots in case a test fails. It helps to explain the problem to the developer, who can immediately patch it. Similarly, from a reporting perspective, it is worth exchanging reports with stakeholders to provide feedback to the stakeholders, to create product stability. With this, Selenium provides frameworks such as TestNG with a default reporting structure and offers additional customizations to them using listeners.(Listener is an interface to modify test behaviour in selenium).

Conclusion:

Automation testing code with Java using Selenium has made life easier for developers and testers alike. Being an open-source tool, it offers the opportunity to speed up the execution time and reduce manual duplication and human-prone errors. Java is a widely popular programming language, given the extensive support it receives from the developer community.You can also know how to use selenium automation testing with java with Selenium online training Hyderabad.