- Bianca Freitas
Selenium with Java - basic automated test tutorial
For this tutorial I am working with:
Eclipse IDE for Java Developers - Version: 2020-06 (4.16.0)
FirefoxDriver - Version 0.27.0 (win64)
ChromeDriver-Version 86.0.4240
Java SE (JDK 14.0.2)
What is Selenium ?
Selenium is a framework for the automated testing of web applications. Using Selenium, you can automate every task in your browser as if a real person were to execute the task. The interface used to send commands to the different browsers is called Selenium WebDriver.
How WebDriver works ?
WebDriver is basically the API we use to programmatically control the webBrowser through test script.
Why not recording ?
Simply because if something is modified in the code, we would need to record everything again.
Create a new project:
Launch Eclipse IDE
From the Package Explorer, do right-click > New > Maven Project > then click on Next
Note: If "Maven Project" is not visible, click on "Other" and then search for "Maven Project"

Then, enter the Group Id and Artifact ID
Note: You may find more information regarding Artifact names here
group id uniquely identifies your project across all projects
artifact id is the name of the jar without version.

2. Update the POM.xml file
What is a POM?
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.selenium.project</groupId>
<artifactId>SeleniumIDEProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Perform Maven update project in Eclipse IDE:
Right-click on your Maven Project > select Maven > Update Project > press OK button

4. Installing Drivers:
Download mozilla geckodriver
Download chromeDriver
5. Setup your WebDriver path into Environment variables:
From windows > Start > type in <Env>, then select Edit the system environment variables > click on Environment Variable button > Then, from System variables click on New… button > enter the Variable name > enter the Variable value (path for your web drivers in your computer) > click on Ok button

Then select the path > and click on Edit... button

Then, enter %GECKO_PATH% in front of your path > then click on OK button

6. Creating first test using Java:
Right-click on src/main/java > New > class > enter the <Package>, check the method and click on Finish button

Here is the code made for this test, it's a simple code which validate a positive scenario where user is able to enter their details and send a message from the website https://www.bianca.net.nz/
package com.seleniumIDE.project;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTest {
public static void main(String[] args) {
//Select web Driver
WebDriver driver = new ChromeDriver();
//Tell driver to go to the website
driver.get("http://bianca.net.nz");
//Maximize the window
driver.manage().window().maximize();
//Search for the element id
WebElement name_field = driver.findElement(By.id("comp-jxbvzvl9input"));
name_field.sendKeys("LUKE SKYWALKER");
WebElement email_field = driver.findElement(By.id("comp-jxbvzvlninput"));
email_field.sendKeys("LUKE.SKYWALKER@testing.com");
WebElement phone_field = driver.findElement(By.xpath("//*[@id=\"comp-jxbvzvlzinput\"]"));
phone_field.sendKeys("07400000052");
WebElement message_field = driver.findElement(By.xpath("//*[@id=\"comp-jxbvzvmbtextarea\"]"));
message_field.sendKeys("COME TO THE DARK SIDE - Testing");
WebElement send_button = driver.findElement(By.xpath("//*[@id=\"comp-jxbvzvmplabel\"]"));
//Wait 3 seconds
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
send_button.click();
//Close the browser
driver.close();
}
}
6. Running the test:
Right click on your test code > Run as > Java Application: