0% found this document useful (0 votes)
32 views

Lab 4

Uploaded by

zannatulmaoameem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lab 4

Uploaded by

zannatulmaoameem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
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 - 04

Title: Cross-Browser Testing using TestNG suite to test any website.


TestNG suite
A test suite is a collection of test cases intended to test a behavior or a set of behaviors of software program. In
TestNG, we cannot define a suite in testing source code, but it is represented by one XML file, as suite is the
feature of execution. It also allows flexible configuration of the tests to be run. A suite can contain one or more
tests and is defined by the <suite> tag.

<suite> is the root tag of your testng.xml. It describes a test suite, which in turn is made of several <test>
sections.

Objective:
To set up and execute cross-browser testing in Chrome, Firefox, and Edge using TestNG.

Prerequisites:
▪ Java Development Kit (JDK) installed.
▪ Integrated Development Environment (IDE) like Eclipse.
▪ Apache Maven installed (for dependency management).
▪ Browsers: Chrome, Firefox, Edge installed on your machine.

Step 1: Set Up Your Project


1. Create a Maven Project:
o Open your IDE and create a new Maven project.
o Add the following dependencies to your pom.xml file:
<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>

1|Page
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

Step 2: Create Browser Driver Utilities


1) Download WebDriver Executables:
• Download the ChromeDriver, GeckoDriver (for Firefox), and EdgeDriver from their respective
official sites and place them in a known directory.
2) Setup WebDriver Paths:
• Create a utility class to setup paths for each WebDriver:

package tv_search;

import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

public class common_login {


public WebDriver driver;

private Map<String, Object> vars;


JavascriptExecutor js;

@BeforeMethod
@BeforeTest
@Parameters("browser")
public void setUp(String browser) throws Exception {

// System.setProperty("webdriver.chrome.driver","D:\\chromedriver-
win64\\chromedriver.exe");
// driver = new ChromeDriver();

// System.setProperty("webdriver.Edge","D:\\edgedriver_win64\\msedgedriver.exe");
// driver = new EdgeDriver();
//
// System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.35.0-
win64\\geckodriver.exe");
// driver = new FirefoxDriver();

if(browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.35.0-
win64\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver","D:\\chromedriver-
win64\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browser.equalsIgnoreCase("edge")) {

System.setProperty("webdriver.Edge","D:\\edgedriver_win64\\msedgedriver.exe");
driver = new EdgeDriver();
}
else {

2|Page
throw new Exception("Browser not found");
}

js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}

@AfterMethod
@AfterTest
public void tearDown() {
driver.quit();
}
}

Step 3: Create TestNG Test Class


▪ Setup Your Test Class:
▪ Create a test class that uses TestNG annotations.

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 extends Class_name {

@Test

public void login_test() throws Exception {

driver.get("https://thetestingworld.com/testings/");
Thread.sleep(3000);
driver.manage().window().maximize();

driver.findElement(By.name("fld_username")).click();

3|Page
driver.findElement(By.name("fld_username")).sendKeys("dew");

driver.findElement(By.name("fld_email")).click();
driver.findElement(By.name("fld_email")).sendKeys("[email protected]");

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();
}

}
Step 4: Configure TestNG XML File
Create testng.xml:

This file defines the browsers to be used for the tests.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="TestSuite_CrossBrowser">

<test name="FirefoxTest" enabled="true">


<parameter name="browser" value="firefox" />
<classes>
<class name="tv_search.login_TestNG_Firefox" />

4|Page
</classes>
</test>
<test name="ChromeTest" enabled="true">
<parameter name="browser" value= "chrome" />
<classes>
<class name="tv_search.login_TestNG_Chrome" />
</classes>
</test>
<test name="EdgeTest" enabled="true">
<parameter name="browser" value= "edge" />
<classes>
<class name="tv_search.login_TestNG_Edge" />
</classes>
</test>
</suite>

Step 5: Execute Tests


1. Run the Test Suite:

o Right-click on the testng.xml file in your IDE and select "Run As" > "TestNG Suite".

o The tests will run sequentially in Chrome, Firefox, and Edge as defined in the testng.xml file.

5|Page

You might also like