How to Write First Automation Script Using Selenium+Java

Automation testing has become a popular choice for software testers to ensure the quality of their application. One of the most popular tools for automation testing is Selenium. Selenium is an open-source testing tool that is used to automate web applications.

To get started with Selenium, you need to write automation code using a programming language. In this article, we will discuss how to write your first automation code in Selenium using Java.

Practical Example

We will automate a simple web application that allows users to submit the form. We will use Selenium and Java to automate the form submission process. The website we will automate is https://www.selenium.dev/selenium/web/web-form.html

Step 1: Setup

Before we start writing automation code, we need to set up the project. We will use eclipse for writing the script. Create a new project in eclipse and add the Selenium dependency to your project. You can add the following dependency to your pom.xml file.

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.8.0</version>
</dependency>

Step 2: Launch the Browser

To automate the web application, we need to launch the browser using Selenium. In our case, we will launch Google Chrome. To do this, we need to create an instance of the ChromeDriver class.

WebDriver driver = new ChromeDriver();

Step 3: Navigate to the Website

Once we have launched the browser, we need to navigate to the website we want to automate. In our case, we want to navigate to the form submission page of the website https://www.selenium.dev/selenium/web/web-form.html

driver.get("https://www.selenium.dev/selenium/web/web-form.html");

Step 4: Fill out the Form

Now, we need to fill out the form on the website. We will fill the following fields using our automation script: Text input, Password, Textarea
To automate the form, we need to locate the web elements using Selenium. We can locate web elements using various methods such as id, name, class name,tagName, cssSelector, and XPath.

driver.findElement(By.id("my-text-id")).sendKeys("Testingmint");
driver.findElement(By.cssSelector("input[name='my-password']")).sendKeys("Password");
driver.findElement(By.xpath("//textarea[@name='my-textarea']")).sendKeys("Software Testing Blog");

Step 5: Submit the Form

After filling out the form, we need to submit the form. In our case, we will click the submit button to submit the form.

driver.findElement(By.tagName("button")).click();

Step 6: Verify Form Submission

Once the form is submitted, we need to verify that the form submission is successful. We can verify that based on the text “Form submitted”

String Verificationtext = driver.findElement(By.tagName("h1")).getText();
if(Verificationtext.contains("Form submitted"))
	{
			System.out.println("Form Submission Successful");
	}
else 
    {
			System.out.println("Form Submission Unsuccessful");
	}

Step 7: Quit the Browser

After verifying the registration, we need to quit the browser.

driver.quit();

Putting Everything Together

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Firstprogram {

	public static void main(String[] args) {
         
        //Launch the Browser
		WebDriver driver = new ChromeDriver();
        

        //Navigate to the Website
		driver.get("https://www.selenium.dev/selenium/web/web-form.html");


        //Fill out the Form
		driver.findElement(By.id("my-text-id")).sendKeys("Testingmint");
		driver.findElement(By.cssSelector("input[name='my-password']")).sendKeys("Password");
		driver.findElement(By.xpath("//textarea[@name='my-textarea']")).sendKeys("Software Testing Blog");


        //Submit the Form
		driver.findElement(By.tagName("button")).click();
		

        //Verify Form Submission
		String Verificationtext = driver.findElement(By.tagName("h1")).getText();
		if(Verificationtext.contains("Form submitted"))
		{
			System.out.println("Form Submission Successful");
		}
		else {
			System.out.println("Form Submission Unsuccessful");
		}


        //Quit the Browser
		driver.quit();
	}

}