In previous post we have seen introduction and setup for TestNG, in this post will focus on how to write a simple TestNG case.
Enter FirstTest as name and click on Finish
And also make sure to add all testng library from the script
Thanks
Step 1 – Write the business logic of the test
Step 2 – Insert TestNG annotations in the code
Step 3 – Add the information about your test (e.g. the class names, methods names, groups names etc…) in a testng.xml file
Step 4 – Run TestNG
Open eclipse and create a new Project as : TestNGSample
under this project create a new Package as com.test
Right click on the com.test package and go to Other option and look got TestNG Class as shown
Enter FirstTest as name and click on Finish
You will see some error in the programming like this and
We need to add TestnNG library, mouse move to org.testng and add the necessary library
For FirstTest class file lets add annotations
@BeforeMethod : Launch Firefox and direct it to the Base URL
@Test : Enter Username & Password to Login, Print console message and Log out
@AfterMethod : Close Firefox
Before copying this file make sure you add selenium jar to project class path
Script :
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTest {
WebDriver driver;
@BeforeMethod
public void Init(){
driver=new FirefoxDriver();
}
@Test
public void TestCase1() throws InterruptedException {
driver.get("http://opensource.demo.orangehrm.com/index.php/auth/login");
Thread.sleep(7000);
driver.manage().window().maximize();
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTest {
WebDriver driver;
@BeforeMethod
public void Init(){
driver=new FirefoxDriver();
}
@Test
public void TestCase1() throws InterruptedException {
driver.get("http://opensource.demo.orangehrm.com/index.php/auth/login");
Thread.sleep(7000);
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id='txtUsername']")).sendKeys("Admin");
driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("admin");
driver.findElement(By.xpath("//*[@id='btnLogin']")).click();
}
@AfterMethod
driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("admin");
driver.findElement(By.xpath("//*[@id='btnLogin']")).click();
}
@AfterMethod
// to close the browser
public void closeb(){
driver.close();
}
}
public void closeb(){
driver.close();
}
}
Right click and runt he program as TestNG Test
Should launch OrangeHRM and login as Admin / admin
Once ran, on the console should display output as
[TestNG] Running:
/private/var/folders/1q/8clrsf517cz46g_lmr_jr4nw0000gn/T/testng-eclipse-1009056383/testng-customsuite.xml
PASSED: TestCase1
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@39ed3c8d: 19 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@48cf768c: 3 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2d363fb3: 34 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 3 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@16b3fc9e: 4 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1e81f4dc: 4 ms
/private/var/folders/1q/8clrsf517cz46g_lmr_jr4nw0000gn/T/testng-eclipse-1009056383/testng-customsuite.xml
PASSED: TestCase1
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@39ed3c8d: 19 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@48cf768c: 3 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2d363fb3: 34 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 3 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@16b3fc9e: 4 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1e81f4dc: 4 ms
Analyzing output :
Open index.html in browser and output should look like this
Next we will run Testng class file through TestNG.xml file, before that we need to create testng.xml file
Right on the Project folder and create a file
Sample testng.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="com.test.FirstTest"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="com.test.FirstTest"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
Here under test class tag make sure you call the proper FirstTest class file.
Now Right click on Testng.xml file and run as TestNG Suite
That's it we have done with sample test by creating Testng file, testng.xml file and run the same as Testng Suite.
Thanks
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.