Sunday, April 16, 2023

Write Automation Test using Selenium with Java & JUnit

Selenium with Java and JUnit5 is a popular framework for web application testing, and here are some reasons why it is considered a good framework:

  1. Java is a popular programming language: Java is a widely-used programming language, and it has a large community of developers who contribute to open-source projects like Selenium. Java is also a powerful language that is known for its reliability and performance, which makes it a good choice for developing test automation scripts.

  2. JUnit5 is a powerful testing framework: JUnit5 is a powerful testing framework that is used for writing and executing tests in Java. It provides a wide range of features, including assertions, test fixtures, parameterized tests, and more. JUnit5 is also highly customizable, which makes it suitable for a variety of testing needs.

  3. Selenium provides browser automation capabilities: Selenium is a browser automation framework that provides developers with the ability to automate interactions with web browsers. This means that tests can be written to simulate user actions on a website, such as clicking buttons, filling out forms, and navigating between pages. Selenium also supports multiple web browsers, which allows tests to be run on different browsers to ensure compatibility.

  4. Supports Continuous Integration (CI): Selenium with Java and JUnit5 supports continuous integration, which allows automated tests to be run in a continuous integration pipeline. This helps to ensure that code changes do not introduce bugs or regressions and that the application remains functional over time.

  5. Large community support: Selenium has a large community of developers and users who contribute to open-source projects, provide support, and share knowledge. This means that there are many resources available for developers who are using Selenium with Java and JUnit5, including documentation, tutorials, and forums.

Overall, Selenium with Java and JUnit5 is a good framework for web application testing because it provides a powerful set of tools for developing and executing tests, supports browser automation, is highly customizable, supports continuous integration, and has a large community of developers and users.


How to set up Selenium Web Driver with Java & Junit5 on Windows

Step 1: Install Java and Eclipse 

  • If you haven't already, download and install Java from the official website.
  • Download and install Eclipse IDE (or any of your preferred IDE) for Java Developers from the official website.

Step 2: Create a new Java project in Eclipse

  • Open Eclipse and create a new Java project by going to File -> New -> Java Project.
  • Give the project a name and select a location to save it.
  • Click Finish.

Step 3: Download the Selenium WebDriver JAR files

  • Download the latest version of the Selenium WebDriver JAR files from the official Selenium website: https://www.selenium.dev/downloads/
  • Save the JAR files to a directory on your computer.

Step 4: Add the Selenium WebDriver JAR files to your Eclipse project

  • In Eclipse, right-click on the project and go to Build Path -> Configure Build Path.
  • In the Libraries tab, click Add External JARs and select the JAR files you downloaded in Step 3.
  • Click Apply and Close.

Step 5: Create a new Java class Right-click on the src folder in your project and go to New -> Class.

  • Give the class a name (e.g., "ExampleTest") and select the option to create a JUnit test case.
  • Click Finish.

Step 6: Write a Selenium test using JUnit5

In the ExampleTest class, import the necessary Selenium and JUnit5 libraries:


Example Test Class Libraries
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;            

  • Write a test method using JUnit5's @Test annotation:


JUnit Annotations
@Test
public void exampleTest() {
  // Set the path to the ChromeDriver executable
  System.setProperty("webdriver.chrome.driver""/path/to/chromedriver");
   
  // Create a new instance of the ChromeDriver
  WebDriver driver = new ChromeDriver();
   
  // Navigate to a website
  driver.get("https://www.example.com");
   
  // Verify the page title
  String pageTitle = driver.getTitle();
  assertEquals("Example Domain", pageTitle);
   
  // Close the browser
  driver.quit();
}

  • Replace the path to the ChromeDriver executable with the path on your local machine.
  • Run the test by right-clicking on the  Example Test class and selecting Run As Jnuit Test.

Sample Complete Test Example
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.*;
 
public class ExampleTest {
    private WebDriver driver;
     
    @BeforeEach
    public void setUp() {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver""/path/to/chromedriver");
         
        // Create a new instance of the ChromeDriver
        driver = new ChromeDriver();
    }
     
    @Test
    public void testExample() {
        // Navigate to a website
        driver.get("https://www.example.com");
         
        // Verify the page title
        String pageTitle = driver.getTitle();
        assertEquals("Example Domain", pageTitle);
    }
     
    @AfterEach
    public void tearDown() {
        // Close the browser
        driver.quit();
    }
}
k   

Write Automation Test using Selenium with Java & JUnit

Selenium  with  Java  and  JUnit5  is a popular framework for web application testing, and here are some reasons why it is considered a goo...