How we start automation testing using selenium tools

How we start automation testing using selenium tools

AUTOMATION TESTING

5/14/20243 min read

a purple and black background with lots of small flowers
a purple and black background with lots of small flowers

Starting automation testing using Selenium involves several steps, from setting up the environment to writing and executing test scripts. Here’s a comprehensive guide to get you started:

1. Set Up the Environment

Install Java Development Kit (JDK)

Download JDK: Visit the Oracle JDK download page or use OpenJDK.

Install JDK: Follow the instructions for your operating system.

Set Environment Variables: Add JAVA_HOME to your system environment variables and include the JDK bin directory in your PATH.

Install an Integrated Development Environment (IDE)

Eclipse: Popular for Java development.

IntelliJ IDEA: Another robust option for Java developers.

Install Selenium WebDriver

Download Selenium WebDriver: Go to the Selenium Downloads page and download the Java bindings.

Add WebDriver to Your Project: In your IDE, create a new Java project and add the Selenium WebDriver JAR files to your project's build path.

Install Browser Drivers

ChromeDriver: Download ChromeDriver.

GeckoDriver (for Firefox): Download GeckoDriver.

EdgeDriver: Download EdgeDriver.

Make sure the driver executables are in your system PATH or specify their location in your test scripts.

2. Write Your First Selenium Test Script

Create a New Java Class

Open your IDE and create a new Java class in your project.

Import Selenium Libraries:

java

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.By;

Write Test Code

Here's a basic example to open a website and perform a simple action:

java

public class SeleniumTest {

public static void main(String[] args) {

// Set the path to the ChromeDriver executable

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Initialize WebDriver

WebDriver driver = new ChromeDriver();

// Open a website

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

// Find an element and perform an action

driver.findElement(By.name("q")).sendKeys("Selenium WebDriver");

driver.findElement(By.name("btnK")).click();

// Close the browser

driver.quit();

}

}

3. Run Your Test Script

Compile and Run: In your IDE, compile and run the Java class.

Observe the Results: The browser should open, perform the actions specified in the script, and then close.

4. Enhance Your Test Scripts

Add Assertions

Use assertions to verify that the outcomes are as expected:

java

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.junit.Assert;

public class SeleniumTest {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

WebDriver driver = new ChromeDriver();

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

WebElement searchBox = driver.findElement(By.name("q"));

searchBox.sendKeys("Selenium WebDriver");

searchBox.submit();

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement result = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result-stats")));

Assert.assertTrue(result.getText().contains("results"));

driver.quit();

}

}

Implement Page Object Model (POM)

Enhance maintainability by using the Page Object Model:

java

public class HomePage {

private WebDriver driver;

public HomePage(WebDriver driver) {

this.driver = driver;

}

public void search(String query) {

driver.findElement(By.name("q")).sendKeys(query);

driver.findElement(By.name("btnK")).click();

}

}

5. Integrate with a Testing Framework

JUnit Example

Add JUnit to your project and structure your tests:

java

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class SeleniumTest {

private WebDriver driver;

@Before

public void setUp() {

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

driver = new ChromeDriver();

}

@Test

public void testSearch() {

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

WebElement searchBox = driver.findElement(By.name("q"));

searchBox.sendKeys("Selenium WebDriver");

searchBox.submit();

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement result = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result-stats")));

assertTrue(result.getText().contains("results"));

}

@After

public void tearDown() {

driver.quit();

}

}

6. Integrate with Continuous Integration (CI) Tools

Jenkins Example

Install Jenkins: Download and install Jenkins from Jenkins.io.

Configure Jenkins Job:

Create a new job.

Configure the job to pull your test code from a version control system (e.g., Git).

Add a build step to compile and run your tests.

Execute Tests: Set up triggers to run tests automatically on code changes or at scheduled intervals.