API Testing using Rest Assured

Tuesday, September 1, 2020

Cross Browser Testing in TestNG| Parallel Testing in multiple browser using TestNG in selenium | @Parameter annotation in TestNG






In real time scenario we have to run our selenium test script in multiple browser simultaneously.
TestNG makes is possible to achieve that using @Parameter annotation.


TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng. xml file. Some times it may be required for us to pass values to test methods during run time. ... The @Parameters annotation can be placed on any method that has a @Test, @Before/After or @Factory annotation. Here i will show an example that how can we use this annotation and run our test cases in parallel .

package Baseclass;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Baseclass {

	public static WebDriver driver;
	public static pageobjects pageobjects;
	
@Test
@Parameters(value="browsername")
public static void setup(String browsername)
{
	if(browsername.equalsIgnoreCase("chrome"))
	{
	WebDriverManager.chromedriver().setup();
	driver=new ChromeDriver();
	}
	
	if(browsername.equalsIgnoreCase("ff"))
	{
	WebDriverManager.firefoxdriver().setup();
	driver=new FirefoxDriver();
	}
	
	driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
	driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
	driver.get("https://www.goibibo.com/");
	driver.manage().window().maximize();
	String title=driver.getTitle();
	
	System.out.println("page title is ---->>"+title);
	pageobjects=new pageobjects(driver);	
}

This is my one class which will run my test based on browsername parameter which it get from the TestNG file.

Now, just create a TestNG file of this project by Right click on project name > TestNG>convert to TestNG





Now, add  this code to the TestNG.xml file.

		<parameter name="browsername" value="chrome">
		</parameter>

This will take browsername=chrome and run this test on chrome. Now, to run this test on multiple browser we need to create other test tag for other browser. we need to create N of test tag for N browser. at the end our TestNG.xml file will look like this


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="tests" thread-count="2">


	<test name="testonchrome">

		<parameter name="browsername" value="chrome">
		</parameter>

		<classes>
			<class name="Baseclass.Hotelsearch" />
		</classes>
	</test>
	
	<test name="testonff">

		<parameter name="browsername" value="ff">
		</parameter>

		<classes>
			<class name="Baseclass.Hotelsearch" />
		</classes>
	</test>
</suite> 
Now, run this TestNG.xml file you will see the result it will run testcase in chrome and firefox parallely.

No comments:

Post a Comment