0% found this document useful (0 votes)
34 views5 pages

Lab 3

Uploaded by

zannatulmaoameem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
34 views5 pages

Lab 3

Uploaded by

zannatulmaoameem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

BANGLADESH UNIVERSITY OF PROFESSIONALS (BUP)

Department of Information and Communication Technology


ICT 4102 : Software Testing and Maintainance Lab
Lab No - 03

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.

Step 1: Setup Eclipse IDE


1. Open Eclipse IDE.
2. Workspace Setup: Choose a workspace directory for your project and click on Launch.

Step 2: Create a Maven Project in Eclipse


1. Go to File > New > Project.
2. Select Maven Project and click Next.
3. If prompted, select Create a simple project (skip archetype selection) and click Next.
4. Fill in the details:
o Group Id: testNG
o Artifact Id: signupTest
o Version: (Leave it as default)

1|Page
5. Click Finish

Step 3: Add Selenium and TestNG Dependencies in pom.xml


1. Open the pom.xml file of your project.
2. Add the following dependencies inside the <dependencies> tag:
i) https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
ii) https://mvnrepository.com/artifact/org.testng/testng/7.10.2

<dependencies>
<!-- Selenium Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.26.0</version> <!-- Use the latest version -->
</dependency>

<!-- TestNG Dependency -->


<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

Step 4: Setup TestNG in Eclipse


1. Go to Help > Eclipse Marketplace.
2. Search for TestNG and install it.

3. Restart Eclipse after the installation is complete.

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;

public class login_TestNG {

public WebDriver driver;

private Map<String, Object> vars;


JavascriptExecutor js;

@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

public void login_test() throws Exception {

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");

WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(5));


// wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("datepicker")));
driver.findElement(By.id("datepicker")).click();
driver.findElement(By.id("datepicker")).sendKeys("12/12/1990");
driver.findElement(By.id("datepicker")).sendKeys(Keys.ENTER);

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.

Step 7: Additional Configuration (Optional)


• Download ChromeDriver:
o Make sure you download the ChromeDriver that matches your Chrome browser version from
ChromeDriver. https://googlechromelabs.github.io/chrome-for-testing/
• Download GeckoDriver:
o Firefox Driver : https://github.com/mozilla/geckodriver/releases/tag/v0.35.0
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.35.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();

• 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

You might also like