TestNgNotes(Basic_Selenium)
TestNgNotes(Basic_Selenium)
Annotations in TestNG:
-> @Test
-> @BeforeMethod
-> @AfterMethod
-> @BeforeClass
-> @AfterClass
-> @BeforeTest
-> @AfterTest
-> @BeforeSuite
-> @AFterSuite
-> @DataProvider
-> @Listeners
-> @Parameters
Annotations in TestNG
@Test:--- act like a main method, which is identified by JVM to start the execution
@BeforeSuite: It is executed before the <suite> tag in the suite xml file
It is executed only once per execution as there will be only one
<suite>
It is used for establishing database connection.
@AfterSuite: It is executed after the closing of suite tag </suite> in suite xml
file
It is executed only once per execution as there will be only one
</suite>
It is used for closing database connection.
@AfterTest: It is executed after the closing of test tag </Test> in suite xml file
The number of times it will execute depends on number </Test> tags
This is mostly used for parallel executions as it create multiple
threads
@BeforeClass: It will execute before opening of every class <class> in suite xml
file
or simply we can tell before evry test class
The number of times it will execute depends on the number of <Class>
or Test class
It is used for launching browser
@AfterClass: It will execute after closing of every class </Class> in suite xml
file
or simply we can tell after evry test class
The number of times it will execute depends on the number of </Class>
or Test class
It is used for closing browser
=> To change the execution order of @Test annotations inside a test class: priority
in testNg
@Test(priority = int)
-> Lowest priority will execute first
-> Default priority will be 0
-> Negetive priorities are allowed
=> To run the same test script/ @Test mutiple Times:invocation count in TestNG
@Test(invocationCount = int)
-> Default invocation count is 1
-> If we want to run the same test script more than once then provide invocation
count
-> If invocation count is given 0 or negetive values, Then that @Test will not be
executed
we can give both invocation count and priority for the same test script
@Test(invocationCount = 4, priority = 2) - priority is given first preference
=> To make the execution of one test script depend on the status(pass/fail) of
other test sript
- DependsOnMethods in TestNG
@Test(dependsOnMethods = "method name")
- if one test script should depend on execution status of multiple test scripts
like
@Test(dependsOnMethods = {"method name 1","method name 2"})
Priority:
When ever we execute testNg class , by default all the test methods will be
executed based on Alphabetical order, in order to change the order of Execution, we
go for priority.
@Test(priority = 0)
public void createContact()
{
System.out.println("contact created");
}
@Test(priority = -1)
public void modifyContact()
{
System.out.println("modify contact");
}
@Test(priority = 1)
public void deleteContact()
{
System.out.println("delete contact");
}
}
modify contact
contact created
delete contact
PASSED: createContact
PASSED: modifyContact
PASSED: deleteContact
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
-----------------------------------------------------------------------------------
---------------------------------------
Depends on method:
Its help us to check the dependent test script is pass or fail.
If dependent test script get pass, execution will continue
if dependent test-script get fail, skip all other test script which is dependent on
first test.
@Test
public void createContact()
{
System.out.println("contact created");
@Test(dependsOnMethods = "createContact")
public void modifyContact()
{
System.out.println("modify contact");
}
@Test(dependsOnMethods = "createContact")
public void deleteContact()
{
System.out.println("delete contact");
}
}
[RemoteTestNG] detected TestNG version 7.4.0
contact created
delete contact
modify contact
PASSED: modifyContact
PASSED: createContact
PASSED: deleteContact
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
-----------------------------------------------------------------------------------
--------------------------------------
@Test
public void createContact()
{
.
System.out.println(arr[5]);
System.out.println("contact created");
@Test(dependsOnMethods = "createContact")
public void modifyContact()
{
System.out.println("modify contact");
}
@Test(dependsOnMethods = "createContact")
public void deleteContact()
{
System.out.println("delete contact");
}
}
===============================================
Default test
Tests run: 3, Failures: 1, Skips: 2
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 0, Failures: 1, Skips: 2
===============================================
-----------------------------------------------------------------------------------
--------------------------------------
Invocation count:
Same test-script executed with multiple times with same test data.
@Test
public void createContact()
{
System.out.println("contact created");
}
@Test
public void modifyContact()
{
System.out.println("modify contact");
}
@Test(invocationCount = 2)
public void deleteContact()
{
System.out.println("delete contact");
}
}
contact created
delete contact
delete contact
modify contact
PASSED: deleteContact
PASSED: createContact
PASSED: deleteContact
PASSED: modifyContact
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
-----------------------------------------------------------------------------------
---------------------------------------
Batch Execution:
Executing all the test script one after the another is called Batch Exceution
To achive any kind of suite execution we requried TestNG.xml file.
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="FrameWork.Properties_file"/>
<class name="FrameWork.Authentication"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
-----------------------------------------------------------------------------------
---------------------------------------
Parallel Execution:Executing all the TestScript in a same browser at same time with
different instance is called parallel
Execution.
<test name="Test1">
<classes>
<class name="FrameWork.Authentication"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
-----------------------------------------------------------------------------------
-------------------------------------
Assertion:-
Assertion is a feature available in TestNg used to validate the TestScripts
expected Results.
Types of Assertion
1)Hard Assert
2)Soft Assert
@Test
public void createCustomer()
{
System.out.println("step1");
System.out.println("step2");
Assert.assertEquals(false, true);
System.out.println("step3");
System.out.println("step4");
}
@Test
public void m1()
{
String expName="shobha";
String actuName="shobha";
Assert.assertEquals(expName, actuName);
}
}
[RemoteTestNG] detected TestNG version 7.4.0
step1
step2
PASSED: m1
FAILED: createCustomer
java.lang.AssertionError: expected [true] but found [false]
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================
Example 2
public class HardAssert {
@Test
public void createCustomer()
{
System.out.println("step1");
System.out.println("step2");
Assert.assertEquals(true, true);
System.out.println("step3");
System.out.println("step4");
}
@Test
public void m1()
{
String expName="shobha";
String actuName="shobhaRani";
Assert.assertEquals(expName, actuName);
}
}
[RemoteTestNG] detected TestNG version 7.4.0
step1
step2
step3
step4
PASSED: createCustomer
FAILED: m1
java.lang.AssertionError: expected [shobhaRani] but found [shobha]
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================
-----------------------------------------------------------------------------------
--------------------------------------
2)SoftAssert:-When ever SoftAssert fails, TestNg Generates Assert Error Exception
and continue Execution with remaining STeps of same TestScripts.
package FrameWork;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
@Test
public void m2()
{
String exp = "Shobha";
String act = "Shobha ";
SoftAssert soft = new SoftAssert();
soft.assertEquals(act, exp);
soft.assertAll();
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
@Test
public void m1() throws Throwable
{
WebDriver driver=new ChromeDriver();
driver.get("https://demo.actitime.com/login.do");
TakesScreenshot screen = (TakesScreenshot)driver;
File src = screen.getScreenshotAs(OutputType.FILE);
File dest = new File("./FailedScript.png");
FileUtils.copyFile(src, dest);
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------
package FrameWork;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
@Test
public void method() throws Throwable
{
WebDriver driver=new ChromeDriver();
driver.get("https://www.flipkart.in");
driver.manage().window().maximize();
TakesScreenshot ts = (TakesScreenshot)driver;
File src = ts.getScreenshotAs(OutputType.FILE);
File dest = new File("./ScreenShotFailedScript.png");
FileUtils.copyFile(src, dest);