Lab 4
Lab 4
<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.
1|Page
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
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;
@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();
}
}
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;
@Test
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");
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:
<suite name="TestSuite_CrossBrowser">
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>
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