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

Simple Program: Public Class Public Static Void

The document contains code snippets demonstrating various TestNG concepts and features including: 1) A simple program that launches Chrome and navigates to Facebook using WebDriver. 2) A TestNG program with a test method to verify the login page title using Assertions. 3) An example using the @BeforeTest annotation to launch the browser before running tests. 4) Code using @BeforeMethod and @AfterMethod annotations to log in and out before each test. 5) A class demonstrating use of the @DataProvider annotation to pass test data from an Excel file.

Uploaded by

vis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Simple Program: Public Class Public Static Void

The document contains code snippets demonstrating various TestNG concepts and features including: 1) A simple program that launches Chrome and navigates to Facebook using WebDriver. 2) A TestNG program with a test method to verify the login page title using Assertions. 3) An example using the @BeforeTest annotation to launch the browser before running tests. 4) Code using @BeforeMethod and @AfterMethod annotations to log in and out before each test. 5) A class demonstrating use of the @DataProvider annotation to pass test data from an Excel file.

Uploaded by

vis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Simple Program

public class FirstClass {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

//System.setProperty("webdriver.opera.driver","C:/Gecko/operadriver.exe");
//WebDriver driver = new OperaDriver();
driver.get("https://en-gb.facebook.com/");

}
}

TestNG Program

public class FirstNG {

@Test
public void verifylogin()
{

System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://en-gb.facebook.com/");
String titles="FB";

String original=driver.getTitle();
System.out.println(original);
Assert.assertEquals(titles, original);

/*
if (driver.getTitle().equals(titles)) {
System.out.println("Correct");
} else {
System.out.println(" Not.");
}
*/

}
Before Test

public class Beforet {

WebDriver driver=null;

@BeforeTest
public void launchBrowswer()

System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://en-gb.facebook.com/");
}

@Test
public void verify()
{

String titles="FB";

String original=driver.getTitle();
System.out.println(original);
Assert.assertEquals(titles, original);

Before Method

public class Beforem {

@BeforeMethod
public void accountlogin()
{
System.out.println("Account has been logged in");
}

@AfterMethod
public void accountlogout()
{
System.out.println("Account has been logged out");
}

@Test
public void updateprofile()
{
System.out.println("Account has been updated");
}

}
DATAPROVIDER

public class Datapro {

private static WebDriver driver;

@DataProvider(name = "Authentication")

public static Object[][] credentials() {

return new Object[][] { { "opensourcecms", "opensourcecms" },


{ "opensourcecms", "opensourcecms" }};

}
@Test(dataProvider = "Authentication")

public void test(String sUsername, String sPassword) {

//driver = new FirefoxDriver();

System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("https://s2.demo.opensourcecms.com/orangehrm/symfony/web/index.php/
auth/login");

driver.findElement(By.id("txtUsername")).sendKeys(sUsername);

driver.findElement(By.id("txtPassword")).sendKeys(sPassword);

driver.findElement(By.id("btnLogin")).click();

driver.findElement(By.xpath("//*[@id=\"option-menu\"]/li[3]/a")).click();
}
}

XML TestNG
public class test {

@Test
public void test1() // First test case.
{
System.out.println("test1");
}
@Test
public void test2() // Second test case.
{
System.out.println("test2");
}
}
XML File Configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite">
<test name="Test">
<classes>
<class name="FTest.test"/>
<class name="FTest.Beforem"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Java File
class Prods
{
int pid;
String name;
int price;
//constructor
Prods()
{
System.out.println("product object constructed");
}

//methods
void setproductdetails(int pid, String name, int price)
{
this.pid=pid;
this.name=name;
this.price=price;

void showproductdetails()
{

System.out.println("product id: "+pid+" ");


System.out.println("name\t"+name);
System.out.println("price\t"+price);
System.out.println("----------------");
}
//getter and setter are requird when you setup your attribyte private
}

public class Nconstr {

public static void main(String[] args) {


// TODO Auto-generated method stub

Prods Prods=new Prods();


Prods.setproductdetails(100, "Vivo", 1250);
Prods.showproductdetails();
}}
POM Class
public class Nlogihrm {

WebDriver driver;
By username=By.id("txtUsername");
By Password=By.id("txtPassword");
By Loginbutton= By.id("btnLogin");
By Sent=By.id("firstName");

public Nlogihrm(WebDriver driver)

this.driver=driver;

public void typeusername()


{
driver.findElement(username).sendKeys("Admin");
}

public void typepassword()


{
driver.findElement(Password).sendKeys("admin123");
}

public void Clickbutton()


{
driver.findElement(Loginbutton).click();
}

public void senttext()


{
driver.findElement(Sent).sendKeys("Nameof");
}
Test Class

public class Nverifyhm {

@Test(priority=1)
public void verifylogins()

System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.get("https://opensource-demo.orangehrmlive.com/index.php/pim/addEmp
loyee");

Nlogihrm Logi= new Nlogihrm(driver);


Logi.typeusername();
Logi.typepassword();
Logi.Clickbutton();
Logi.senttext();
driver.close();
}

Test Class Data Provider

public class NGooglefo {

ChromeDriver driver;

@Test(dataProvider="testdataa")
public void DemoProject(String username, String password,String tees) throws
InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Register.html");

driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[1]/div[1]/inp
ut")).sendKeys(username);

driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[1]/div[2]/inp
ut")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[2]/div/textar
ea")).sendKeys(tees);

Thread.sleep(15000);
}

@AfterMethod
void ProgramTermination()
{
driver.quit();
}

@DataProvider(name="testdataa")

/*
public Object[][] data()
{
ReadExcelFile config = new
ReadExcelFile("C:\\Users\\user\\Desktop\\Fantast\\LoginCredentials.xlsx");
Object[][] testObjArray = config.getdata();
System.out.println(testObjArray);
return (testObjArray);
}*/

public Object[][] TestDataFeed()


{

ReadExcelFile config = new


ReadExcelFile("C:\\Users\\user\\Desktop\\Fantast\\LoginCrss.xlsx");

int rows = config.getRowCount(0);

Object[][] credentials = new Object[rows][3];

for(int i=0;i<rows;i++)
{
credentials[i][0] = config.getData(0, i, 0);
credentials[i][1] = config.getData(0, i, 1);
credentials[i][2] = config.getData(0, i, 2);
}

return credentials;
}

}
public class NGooglefo {

ChromeDriver driver;

@Test(dataProvider="testdataa")
public void DemoProject(String username, String password,String tees) throws
InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"E:\\p2\\Users\\vishal\\Desktop\\TestWorkspace\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://demo.automationtesting.in/Register.html");
driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[1]/div[1]/inp
ut")).sendKeys(username);

driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[1]/div[2]/inp
ut")).sendKeys(password);

driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[2]/div/textar
ea")).sendKeys(tees);

driver.findElement(By.xpath("//*[@id=\"basicBootstrapForm\"]/div[5]/div/label[
1]/input")).click();

Thread.sleep(15000);
}

@DataProvider(name="testdataa")

public Object[][] TestDataFeed()


{

ReadExcelFile config = new


ReadExcelFile("C:\\Users\\user\\Desktop\\Fantast\\LoginCrss.xlsx");

//int rows = config.getRowCount(0);


// System.out.println(rows);
//rows=rows-1;
int rows=2;
Object[][] credentials = new Object[rows][3];

for(int i=1;i<rows;i++)
{
credentials[i][0] = config.getData(0, i, 0);
credentials[i][1] = config.getData(0, i, 1);
credentials[i][2] = config.getData(0, i, 2);
//System.out.println(i);
}

return credentials;
}

You might also like