Parameterization with TestNG:-
1.
By
using Parameters annotation and testing.xml
2.
By
using DataProvider annotation
Parameterization
annotation with testing.xml :-
Now take a scenario so that we can
better understand:
- Open - https://www.freecrm.com/index.html
- Login with valid username and password
Now in this
case we will pass the parameters value of username and password in testing.xml
file.
1.First we will write the code for opening given above URL and then login –
package seleniumPkg;
import
java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.testng.annotations.Parameters;
import
org.testng.annotations.Test;
public class ParamterizationTestNG {
WebDriver
driver;
@Test()
@Parameters({ "browser", "username", "password" })
public void openBrowser(String br, String usernm, String pwd) throws InterruptedException {
if (br.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium
Jars\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://freecrm.com/index.html");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
driver.findElement(By.name("username")).sendKeys(usernm);
driver.findElement(By.name("password")).sendKeys(pwd);
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(5000);
driver.quit();
}
else if (br.equals("FF")) {
System.out.println("open firefox
browser");
}
}
}
Note:- You can see above after @Test annotation we have added @Parameters and also passed browser, username and password because I want to make these should be parameterized.
Note:- You can see above after @Test annotation we have added @Parameters and also passed browser, username and password because I want to make these should be parameterized.
2. Now we will pass value of browser, username and password from testing.xml file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<parameter name="browser"
value="chrome"/>
<parameter name="username"
value="naveenk"/>
<parameter name="password"
value="test@123"/>
<classes>
<class name="seleniumPkg.ParamterizationTestNG"/>
</classes>
</test> <!-- Test -->
</suite> <!--
Suite -->
Internally passed values like below :-
Note:- You can see here at a time we can only give one value to the parameter because this not possible with @parameters annotation to pass multiple values so if we want to pass different set of values to the parameters then we have to use Data Provider annotation.
Now
run the test script by right clicking on testing.xml file. In coming tutorial
we will see parameterization with DataProvider.
The blog is so interactive and Informative , you should write more blogs like this Ruby on rails Online course
ReplyDelete