Know Your Test Automation Tools : Working with Selenium Java
Kushan Shalindra Amarasiri
Director Quality Engineering at Social Catfish
Selenium is a free and open source test automation API used for Web UI test automation, and it supports all the web browsers, implemented in variety of language bindings and get executed in multiple OS platforms. Lets look at how we can start our work with Selenium Java.
First create a maven Java project and add the following dependencies in your pom.xml file.
<dependencies>
? <dependency>
? ? <groupId>org.seleniumhq.selenium</groupId>
? ? <artifactId>selenium-java</artifactId>
? ? <version>3.141.59</version>
</dependency>
<dependency>
? ? <groupId>io.github.bonigarcia</groupId>
? ? <artifactId>webdrivermanager</artifactId>
? ? <version>4.4.3</version>
</dependency>
</dependencies>
The second dependency is Web driver manager which allows us the automatic download and manage the relative browser drivers needed to invoke the browser.
Next add the TestNG support for your project from the build path option.
Next add the following code which will execute a selenium automated script to login to the Guru 99 Bank demo.
package SeleniumPckage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SeleniumClass {
WebDriver driver;
SoftAssert sa;
@BeforeTest
public void setup()
{
sa = new SoftAssert();
WebDriverManager.chromedriver().setup();
? ? ? ChromeOptions options = new ChromeOptions();
? options.setHeadless(false);
? driver = new ChromeDriver(options);
? driver.get("https://demo.guru99.com/V4/");
}
@Test
public void test1()
{
? driver.findElement(By.name("uid")).sendKeys("mngr332873");
? driver.findElement(By.name("password")).sendKeys("umEtyvy");
? driver.findElement(By.name("btnLogin")).click();
? sa.assertEquals(driver.findElement(By.xpath("https://tr[@class='heading3']/td")).getText(), "Manger Id : mngr332873");
? sa.assertAll();
??
}
@AfterTest
public void teardown()
{
? driver.close();
? driver.quit();
}
}
Execute the following test as a TestNG test....