Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

TestNgNotes(Basic_Selenium)

Helpful for Automation testing
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

TestNgNotes(Basic_Selenium)

Helpful for Automation testing
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

TestNG:

Unit testing framework which supports java and .net


TestNG = Junit + Nunit

TestNG for developers:


-> Developers will make use of TESTNG for Unit testing of the source code

TestNG for Automation Testers:


-> Automation Testers will make use of TESTNG for developing the test scripts
in a more optimised way.

Annotations in TestNG:
-> @Test
-> @BeforeMethod
-> @AfterMethod
-> @BeforeClass
-> @AfterClass
-> @BeforeTest
-> @AfterTest
-> @BeforeSuite
-> @AFterSuite
-> @DataProvider
-> @Listeners
-> @Parameters

Annotations in TestNG

=> Basic Configuration Annotations - ReturnType is void

@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.

@BeforeTest: It is executed before the opening of <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

@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

@BeforeMethod: It will execute before every @Test annotation


The number of times it will execute depends on number of @Test
It is used for login to Application

@AfterMethod: It will execute after every @Test annotation


The number od times it will execute depends on number of @Test
It is used for logout of Application

=> 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"})

=> To disable a prticular test script/@Test : enabled in testNG


@Test(enabled = false)
-> Enabled is feature which accepts boolean(true/false)
-> Default value for enabled is true,
-> If you want to disable the test script execution give enabled = false.

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.

public class Sample {

@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.

public class Sample {

@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
===============================================
-----------------------------------------------------------------------------------
--------------------------------------

public class Sample {

@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.

public class Sample {

@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.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite parallel="tests" name="Suite">
<test thread-count="5" parallel="tests" name="Test">
<classes>
<class name="PopUps.Authentication"/>
<class name="FrameWork.Properties_file"/>
</classes>
</test>

<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

-HardAssert:-When ever hard Assert method fails. testNG generate AssertError


Execption and stop the current test Execution and continue execution with
remaining test.

public class HardAssert {

@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;

public class SoftAssertEx {


@Test
public void m1()
{
System.out.println("Step1");
System.out.println("Step2");
SoftAssert soft = new SoftAssert();
soft.assertEquals(false, true);
System.out.println("Step3");
System.out.println("Step4");
soft.assertAll();
}

@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;

public class ScreenShotEx {

@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);
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------

HardAssert :-ALl the methods are static in nature


-When ever the Assertion Fails stops the Execution in same line and
throw the Exception
-Used for all the madatory field
-AssertAll() is not madantory

SoftAssert:- All the methods are non-static in nature


-When ever the Assertion Fails Continues the Execution at the end
Throws the Exception
-Used for all the non-madatory field
-AssertAll() is mandatory
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------

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;

public class ScreenShotEx {

@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);

You might also like