How to Get Started with Selenium: Tying It All Together
Selenium is a powerful tool for automating web applications for testing purposes. In this post, we'll get our hands dirty by tying all the elements together and creating a fully functional automation script.
Prerequisites
Before you start, ensure you have:
- Installed Java Development Kit (JDK)
- Installed and configured a Selenium WebDriver
- Basic knowledge of Java programming
Setting Up Your Project
- Create a new Maven project.
- Add the following dependencies to your
pom.xml:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
- Create a package named
com.automation.tests.
Writing the First Test
Create a new Java class named LoginTest inside your package:
package com.automation.tests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}
@Test
public void testLogin() {
driver.get("https://example.com/login");
// Add your test logic here
}
@After
public void tearDown() {
driver.quit();
}
}
Navigating to a Page
driver.get("https://example.com/login");
Locating Elements
Use Selenium locators to find elements:
WebElement usernameField = driver.findElement(By.id("username"));
WebElement passwordField = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("loginButton"));
Performing Actions
Simulate user interactions by performing actions on the located elements:
usernameField.sendKeys("yourUsername");
passwordField.sendKeys("yourPassword");
loginButton.click();
Validating Outcomes
Use assertions to verify that expected outcomes are met:
String expectedTitle = "Dashboard - Example";
String actualTitle = driver.getTitle();
assertEquals(expectedTitle, actualTitle);
Full Example
Here is the complete code for a basic login test:
package com.automation.tests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.assertEquals;
public class LoginTest {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}
@Test
public void testLogin() {
driver.get("https://example.com/login");
WebElement usernameField = driver.findElement(By.id("username"));
WebElement passwordField = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("loginButton"));
usernameField.sendKeys("yourUsername");
passwordField.sendKeys("yourPassword");
loginButton.click();
String expectedTitle = "Dashboard - Example";
String actualTitle = driver.getTitle();
assertEquals(expectedTitle, actualTitle);
}
@After
public void tearDown() {
driver.quit();
}
}
Our RESTful AI tool can significantly enhance your testing process. Perform RESTful tests on APIs, execute test cases leveraging AI, and autonomously generate test cases without needing prior knowledge. Just plug in your cURL request, run the test, and watch as our AI optimizes your Software Development Life Cycle (SDLC), whether you are a developer or a QA specialist.
For more information, visit BLVCK.AI.