Getting Started with Selenium Web Driver in Java
Kushan Shalindra Amarasiri
Director Quality Engineering at Social Catfish
This article is written upon the request of people in the community. How we can kick start Selenium WebDriver in Java.
To start with Selenium WebDriver in Java first we need to install JDK 1.8 which is most used and stable JDK version.
Next we need to add an IDE. I love eclipse. So lets download and install eclipse IDE
There after we need to add a project and lets open up and add a maven project.
Go to File > New > Maven Project
Click on creating simple project
Next give a meaning full artifact ID and Group ID
After clicking finish you will have your maven project.
Now lets add the selenium, testng and web driver manager dependencies to pom.xml
<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>3.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
Once done save the pom.xml and all the dependencies will be downloaded.
Now add the package and a class, and add the following script.
@Test
public void test()
{
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
String baseUrl = "https://demo.guru99.com/V4/";
driver.get(baseUrl);
driver.findElement(By.name("uid")).sendKeys("mngr206361");
driver.findElement(By.name("password")).sendKeys("AdUbupU");
driver.findElement(By.name("btnLogin")).click();
// System.out.println (driver.findElement(By.xpath("https://tr[@class='heading3']/td")).getText());
Assert.assertEquals(driver.findElement(By.xpath("https://tr[@class='heading3']/td")).getText(), "Manger Id : mngr206361");
driver.close();
driver.quit();
}
Run it as a testng and you will your script will get executed in the chrome browser.