Lab 3
Lab 3
Title: Install Selenium Webdriver and use TestNG to test any website.
Introduction to Selenium WebDriver
If you are beginning with desktop website or mobile website test automation, then you are going to be using
WebDriver APIs. WebDriver uses browser automation APIs provided by browser vendors to control the
browser and run tests. This is as if a real user is operating the browser. Since WebDriver does not require its
API to be compiled with application code, it is not intrusive. Hence, you are testing the same application which
you push live.
Selenium Overview
Objective:
Step-by-Step Guide to Install Selenium WebDriver in Eclipse IDE Using Maven Project and Use TestNG
Prerequisites:
• Java JDK: Make sure you have Java Development Kit installed on your system.
• Eclipse IDE: Download and install Eclipse IDE (https://www.eclipse.org/downloads/ ) if you haven't
already.
1|Page
5. Click Finish
<dependencies>
<!-- Selenium Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.26.0</version> <!-- Use the latest version -->
</dependency>
2|Page
Step 5: Write a TestNG Test Case Using Selenium
1. Create a New Package:
1. Right-click on src/test/java > New > Package.
2. Name the package WebsiteSignup.
2. Create a Test Class:
1. Right-click on the newly created package > New > Class.
2. Name the class SignupTest.
3. Write Your Test Code:
package tv_search;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
@BeforeMethod
@BeforeTest
public void setUp() {
System.setProperty("webdriver.chrome.driver","D:\\chromedriver-win64\\chromedriver.exe");
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@Test
3|Page
driver.get("https://thetestingworld.com/testings/");
Thread.sleep(3000);
driver.manage().window().maximize();
driver.findElement(By.name("fld_username")).click();
driver.findElement(By.name("fld_username")).sendKeys("dew");
driver.findElement(By.name("fld_email")).click();
driver.findElement(By.name("fld_email")).sendKeys("mahmud@gmaiil.com");
driver.findElement(By.name("fld_password")).sendKeys("123456");
driver.findElement(By.name("fld_cpassword")).click();
driver.findElement(By.name("fld_cpassword")).sendKeys("123456");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("phone")));
driver.findElement(By.name("phone")).click();
driver.findElement(By.name("phone")).sendKeys("0122100012");
driver.findElement(By.name("address")).click();
driver.findElement(By.name("address")).sendKeys("BUP, Mirpur, Dhaka");
driver.findElement(By.name("add_type")).click();
driver.findElement(By.name("sex")).click();
{
WebElement dropdown = driver.findElement(By.name("sex"));
dropdown.findElement(By.xpath("//option[. = 'Male']")).click();
}
driver.findElement(By.id("countryId")).click();
{
WebElement dropdown = driver.findElement(By.id("countryId"));
dropdown.findElement(By.cssSelector("[value='18']")).click();
}
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='348']")));
driver.findElement(By.id("stateId")).click();
{
WebElement dropdown = driver.findElement(By.id("stateId"));
dropdown.findElement(By.cssSelector("[value='348']")).click();
}
Thread.sleep(4000);
// wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='7291']")));
driver.findElement(By.id("cityId")).click();
{
WebElement dropdown = driver.findElement(By.id("cityId"));
dropdown.findElement(By.cssSelector("[value='7291']")).click();
}
driver.findElement(By.name("zip")).click();
driver.findElement(By.name("zip")).sendKeys("1206");
driver.findElement(By.id("tab-content1")).click();
driver.findElement(By.name("terms")).click();
}
@AfterMethod
@AfterTest
public void tearDown() {
// driver.quit();
}
4. Note: Replace "path/to/chromedriver" with the actual path to your chromedriver.exe file.
4|Page
Step 6: Run Your Test Using TestNG
1. Right-click on the SignupTest.java file.
2. Select Run As > TestNG Test.
3. The console will show the results of the test execution, and your browser will open the specified website.
• Download EdgeDriver:
o https://developer.microsoft.com/en-us/microsoft-
edge/tools/webdriver?form=MA13LH#downloads
System.setProperty("webdriver.Edge","D:\\edgedriver_win64\\msedgedriver.exe");
driver = new EdgeDriver();
5|Page