Get Priority Number In Selenium TestNG - selenium

Is there any way of getting/knowing the priority number within a test suit? i.e.
#Test (priority = 1, groups = { "requiredSetUp" })
public void login(){
System.debug('This test is assigned ' + getPriorityNumber());
}

You can implement it using IAnnotationTransformer. For example you could do something like:
public class NewTest implements IAnnotationTransformer{
private static Map<String, Integer> priorities = new ConcurrentHashMap<>();
#Test(priority = 123)
public void test123(ITestContext ctx){
System.out.println("Running: " + Thread.currentThread() +
" - " + priorities.get(new Object(){}.getClass().getEnclosingMethod().getName()));
}
#Test(priority = 321)
public void test321(ITestContext ctx){
System.out.println("Running: " + Thread.currentThread() +
" - " + priorities.get(new Object(){}.getClass().getEnclosingMethod().getName()));
}
#Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
priorities.put(testMethod.getName(), annotation.getPriority());
IAnnotationTransformer.super.transform(annotation, testClass, testConstructor, testMethod);
}
}
where this is implemented in thread-safe way to support parallelism.
This new Object(){}.getClass().getEnclosingMethod().getName() is the way how to obtain current method name. See: Getting the name of the currently executing method
Disclaimer: This type of listener can only be registered inside testng.xml or as command line argument. So below is the example of testng.xml implementing proper configuration
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="methods" thread-count="3">
<listeners>
<listener class-name="so.testng.NewTest"/>
</listeners>
<test name="Regression1">
<packages>
<package name="so.testng"/>
</packages>
</test>
</suite>

Related

ExtentReport5 SparkReporter is generating only 1 report from 2 Tests when thread-count is 1

I have to generate report in extent reporter 5, I can generate report from multiple tests, However this seems to be possible only if thread-count is set in TestNG.xml So If i have 5 test(in separate files/classes as per POM model). and thread-count is set to 5, then I am getting all 5 reports in spark.html
But I might need to run 50+ test, so I need to keep thread-count to 2. So testng will run 2 tests in parallel and once one test is done, it will proceed to third test and so on. But using only 2 as thread count 2 tests are getting reported in spark.html So how can I generate report for all 50 test keeping thread-count to 2 only?
sample testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="1" parallel="classes" name="BuildAcceptanceTesting - GP">
<listeners>
<listener class-name="com.TestUtil.WebEventListener"/>
</listeners>
<test name ="GP pages">
<classes>
<class name="com.byui.gp.StudentSideTest.Test1"/>
<class name="com.byui.gp.StudentSideTest.Test2"/>
</classes>
</test >
</suite> <!-- StudentSideTests-Parallel -->
Sample program:-
base.java
public static WebDriver initialize() {
WebDriver contr0l = null;
reportingTool(); // Adding extent reporter
WebDriverManager.chromedriver().setup();
contr0l = new ChromeDriver();
return contr0l;
public static void reportingTool() {
extent = new ExtentReports();
spark = new ExtentSparkReporter("spark.html");
spark.config().setTheme(Theme.STANDARD);
spark.config().setDocumentTitle("Report");
extent.attachReporter(spark);
}
#AfterClass
public void closeDriver(WebDriver contr0l)
{if(contr0l != null) {
contr0l.quit();}
}
public void FlushExtent(WebDriver contr0l)
{extent.flush();}
Test1.java
public class Page1 extent base{
private WebDriver contr0l;
#BeforeClass
Contr0l=base.initialize();
#Test
public void TC1(){ sysout("whatever");}
#Test
public void TC2(){ sysout("whatever");}
#AfterTest
public void kapat() {
base.FlushExtent(contr0l);
base.closeDriver(contr0l);
}
}
Test2.java
public class Page1 extent base{
private WebDriver contr0l;
#BeforeClass
Contr0l=base.initialize();
#Test
public void TC1(){ sysout("whatever");}
#Test
public void TC2(){ sysout("whatever");}
#AfterTest
public void kapat() {
base.FlushExtent(contr0l);
base.closeDriver(contr0l);
}
}

TestNG - Only Priorirty = 1 method is executed

I have a testNG.xml file as below:
<suite name="example suite 1" >
<test name = "exampletest2" preserve-order="true" >
<classes>
<class name = "sprint0.class1" />
<class name = "sprint0.class2" />
</classes>
</test>
</suite>
each class has mutliple test methods where priority is set:
example:class1
public class class1 extends TestSetup {
#Test(priority = 1, dataProvider = "TerminateEmpData" , enabled = true )
public void test1class1 (String searchKey, String ChangedKey) {
/* code */
}
#Test(priority = 2, dataProvider = "TerminateEmpData" , enabled = true )
public void test2class1 (String searchKey, String ChangedKey) {
/* code */
}
#Test(priority = 3, dataProvider = "TerminateEmpData" , enabled = true )
public void test3class1 (String searchKey, String ChangedKey) {
/* code */
}
}
Another class2 as below:
public class class2 extends TestSetup {
#Test(priority = 1, dataProvider = "TerminateEmpData" , enabled = true )
public void test1class2 (String searchKey, String ChangedKey) {
/* code */
}
#Test(priority = 2, dataProvider = "TerminateEmpData" , enabled = true )
public void test2class2 (String searchKey, String ChangedKey) {
/* code */
}
}
After I run above testng.xml file, only method with Priority =1 is execueted.
I would like to understand what am i doing wrong here?
Also I would like to mention in TestSetup class,
**#BeforeMethod, I invoke the browser and login to the application
and
#AfterMethod, I do teardown and take screenshot for test failure.**
I would like to understand what am i doing wrong here as I am not able to get all my methods run.
An alternative which i created is to create a testsuite for individual class and then call all the test suits and run in seperate testng.xml which works fine.
Any suggestions?

How to add timeout test case fail in TestNg and Allure reporting framework?

Not able to write Allure test result if I use following timeout in TestNG.xml and methods.
<test name="TestName" time-out="60000">
<classes>
<class name="ClassName">
<methods><include name="MethodName"></include>
</methods>
</class>
</classes>
</test>
OR
#Test (timeOut = 10*60000)
Exception:
io.qameta.allure.AllureResultsWriteException: Could not write Allure test result
Caused by: java.nio.channels.ClosedByInterruptException
Listener I used:
public class AllureListener implements IHookable {
#Override
public void run(IHookCallBack callBack, ITestResult testResult) {
callBack.runTestMethod(testResult);
if (testResult.getThrowable() != null || testResult.getStatus() == ITestResult.FAILURE
|| testResult.getStatus() == ITestResult.SKIP
|| testResult.getStatus() == ITestResult.SUCCESS_PERCENTAGE_FAILURE
|| testResult.getStatus() == ITestResult.SUCCESS_PERCENTAGE_FAILURE) {
String methodName = testResult.getName().toString().trim();
ITestContext context = testResult.getTestContext();
WebDriver driver = (WebDriver) context.getAttribute("driver");
if (driver != null) {
AllureReportUtil ru = new AllureReportUtil();
ru.addAttachment(driver, methodName);
}
}
}
Kindly help me in this.

Tests executes by priority through entire suite instead of class using TestNG and Gradle

all. I'm using IntelliJ IDEA with Selenium, TestNG and Gradle. After I had added a Gradle to the project my tests running in strange priority.
So, I have a XML file for run with TestNG:
<suite name="Mobile. Firefox. 320x480">
<parameter name="browserType" value="firefox" />
<parameter name="resolution" value="320x480"/>
<listeners>
<listener class-name="Listeners.Listeners"/>
</listeners>
<test name="320x480">
<classes>
<class name="TestsPerPage.TopMenu.TopMenuTests"/>
<class name="TestsPerPage.TopMenu.EstimateProjectTests"/>
</classes>
</test>
Test classes looks like:
TopMenuTests.class:
public class TopMenuTests extends SetupDriver {
TopMenuPageObjects topmenuPO;
#BeforeClass
public void initClasses(){
topmenuPO = new TopMenuPageObjects(driver);
System.out.println("Before Class TM");
}
#Test(groups = {"mobile"}, priority = 3)
public void mobilePortfolioOpen() {
System.out.println("Test TM, priority = 3");
}
#Test(groups = {"mobile"}, priority = 4)
public void mobileHomePageOpen() {
System.out.println("Test TM, priority = 4");
}
#Test(groups = {"mobile"}, priority = 5)
public void mobileCompanyPageOpen() {
System.out.println("Test TM, priority = 5");
}
EstimateProjectTests.class:
public class EstimateProjectTests extends SetupDriver {
EstimatePageObjects estimatePO;
#BeforeClass(groups = "mobile")
public void initMobileClasses() {
estimatePO = new EstimatePageObjects(driver);
System.out.println("Before Class EP");
}
#Test
public void estimatePageOpen(){
System.out.println("Test EP, priority = 0");
}
#Test(priority = 1)
public void mandatoryFieldsCheck(){
System.out.println("Test EP, priority = 1");
}
#Test(priority = 1)
public void labelsCheck(){
System.out.println("Test EP, priority = 1");
}
So, at the end, after running an XML file it should:
Run TopMenuTests.class -> run BeforeClass -> All tests by priority
Run EstimateProjectTests.clall -> run BeforeClass -> All tests by priority
Output shold be:
Before Class Tm
Test TM, priority = 3
Test TM, priority = 4
Test TM, priority = 5
Before Class EP
Test EP, priority = 0
Test EP, priority = 1
Test EP, priority = 1
But after I'd connected Gradle to the project I have this output:
Before Class Tm
Before Class EP
Test EP, priority = 0
Test EP, priority = 1
Test EP, priority = 1
Test TM, priority = 3
Test TM, priority = 4
Test TM, priority = 5
So, firstly had run all #BeforeClass and then all tests by, some kind of, "global" priority.
Have anyone faces the same issue?
My Gradle code:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "io.qameta.allure:allure-gradle:2.3"
}
}
plugins {
id "io.qameta.allure" version "2.5"
}
group 'com.indeema.web-site-automation'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: "io.qameta.allure"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
allure {
version = "2.2.1"
autoconfigure = true
aspectjweaver = true
allureJavaVersion = "2.0-BETA18"
}
dependencies {
testCompile group: 'org.testng', name: 'testng', version: '6.10'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.8.1'
}
test {
useTestNG()
}
P.S. Sorry for such big amount of the text, don't know how to explain it in a shorter way.

How to get the Passed, Failed and Skipped method count from the testng suite level

I am overriding the generateReport() of the IReporter interface. To do some action, I need to figure out the total Passed method, Failed method, Skipped method count and total execution time of the testng suite.
I am not sure how to get those counts. Appreciate your help on this. TIA.
You can get all information through following code :
//Iterating over each suite included in the test
for (ISuite suite : suites) {
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map<String,ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '" + suiteName +
"' is:" + tc.getPassedTests().getAllResults().size());
System.out.println("Failed tests for suite '" + suiteName +
"' is:" +
tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '" + suiteName +
"' is:" +
tc.getSkippedTests().getAllResults().size());
System.out.println("Total excution time for test '" + tc.getName() +
"' is:" + (tc.getEndDate().getTime()- tc.getStartDate().getTime()));
For more details,see ITestContext interface.
You can get the total passed failed and skipped tests using the ITestListener interface
public class TestStatistics implements ITestListener {
List<ITestNGMethod> passedtests = new ArrayList<ITestNGMethod>();
List<ITestNGMethod> failedtests = new ArrayList<ITestNGMethod>();
List<ITestNGMethod> skippedtests = new ArrayList<ITestNGMethod>();
#Override
//This method will automatically be called if a test runs successfully
public void onTestSuccess(ITestResult result) {
//add the passed tests to the passed list
passedtests.add(result.getMethod());
}
#Override
//This method will automatically be called if a test fails
public void onTestFailure(ITestResult result) {
//add the failed tests to the failed list
failedtests.add(result.getMethod());
}
#Override
//This method will automatically be called if a test is skipped
public void onTestSkipped(ITestResult result) {
//add the skipped tests to the skipped list
skippedtests.add(result.getMethod());
}
}
you can get the passed,failed,skipped tests count by list.size() for respective lists.
you can also get the name of the tests passed, failed, skipped from the lists
If you want to get the test execution time then you can use the below methods
#Override
//This will be called on after the test class is instantiated
public void onStart(ITestContext context) {
context.getStartDate();
}
#Override
//This will be called after the test class run
public void onFinish(ITestContext context) {
context.getEndDate();
}
If you have multiple test classes in a suite then you can add the time of all the test classes which will be your suite run time
Hope this helps you.Kindly get back if you have any queries...