TestNG test are not getting executed as per the priority - selenium

Below is my project structure in eclipse :
->testclasses
---->AccountTest(Priority of Methods from 1-6)
---->BillingTest(Priority of Methods from 7-13)
---->HomePageTest(Priority of the only method is 17)
---->SupportTest (Priority of Methods from 14-16)
All the test classes above have methods where priority is set in incremental order as shown above.
Now when I right click on testclasses package and run it as Testng. It starts the execution with HomePageTest.
I am setting the priority of my test methods as below :
#Test(priority=6, dataProvider="Setup")
I want the execution to be as per the priority defined for each method and Thus method with priority 1 should execute first irrespective of which class it is in.

The correct way to run all the tests present in multiple classes with priorities is to run those by a testng file. So write all the classes name in the testng.xml file and then run the testng file by right clicking on it from the package explorer--> Run As-->TestNG Suite. Your test cases will run according to the priorities set irrespective of the classes they belong to.
You testng.xml should look like:
<test name="TestSuiteName">
<classes>
//Insert the whole path of the classes here like
<class name="packageName.AccountTest" />
<class name="packageName.BillingTest" />
<class name="packageName.HomePageTest" />
<class name="packageName.SupportTest" />
</classes>
</test>

TestNG runs test cases in order of the priorities. If there is no priority for a testmethod then by default TestNG sets the priority to 0. In your case, in HomePageTest class there may be some method with priority 0. Either set priority of all the methods or use test methods in xml runner file to run in a given order
<test name="DummyTest">
<classes>
<class name="apitestset.inventory.Test">
<methods>
<include name="create"/>
<include name="update"/>
<include name="get"/>
<include name="check"/>
<include name="initiate"/>
<include name="confirm"/>
<include name="extend"/>
</methods>
</class>
<classes>
Here testclass has 7 methods and they run in the order given in xml runner file. Don't set the priority if you are using methods in xml runner file.

Related

TestNG - Implementation of sequential and parallel test scripts

I was creating a demo framework for Open MRS Application using POM design. The credentials for the Application are "admin/Admin123"
I created the following 3 Classes:
1) Login tests: Which contains all the Login (1 +ve and 2 -ve scenarios) and Logout test (Total 4 #Tests)
2) Register Patient test: Which will register a patient and also verify if the patient has been registered successfully (Total 3 +ve #Tests)
3) Capture Vitals test: Which will Capture Vitals of the patient registered above and verify if the details captured are correct. (Total 2 +ve #Tests)
I'm planning to add a few more Classes which will contain tests related to Adding Visits, adding Allergies etc.
There is a dependency between the classes i.e. only execute the Register patient test if the #Test for Login passes. Similarly, execute the remaining tests like capture vitals, adding visits, adding allergies only if the Register patient test passes. However, there is no dependency between the capture vitals, adding visits and adding allergies and can run in parallel.
This is how it looks from a top-level:
class Login
#Test T_001_LoginTest
#Test T_002_LogoutTest
class RegisterPatient
#Test T_003_RegisterNewPatient
#Test T_004_ConfirmPatientRegisteredUsingPatientID (dependent on T_003)
#Test T_005_ConfirmPatientRegisteredUsingPatientName (dependent on T_003)
class CaptureVitalsTest
#Test T_006_CreateAVisitAndCaptureVitals (dependent on T_001 and T_003)
#Test T_007_VerifyVitals (dependent on T_006)
What I have done so far is added defined group "login" for class Login and group "registerpatient" for class Register. For class RegisterPatient, I've added dependsOnGroup "login". Similarly, for class CaptureVitals, I've added dependsOnGroups for both "login" and "registerpatient". I've also set alwaysRun = true for the #Test Methods which have "dependsOnMethods" dependency.
Below is my testng.xml file
<suite name="Open MRS Automation" parallel="tests">
<listeners>
<listener class-name="com.utils.CustomListener" />
</listeners>
<test thread-count="5" name="Login Test">
<classes>
<class name="com.testcases.LoginTest" />
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Register Patient Tests">
<classes>
<class name="com.testcases.RegisterPatientTest" />
</classes>
</test>
<test thread-count="5" name="Capture Vitals Tests">
<classes>
<class name="com.testcases.CaptureVitalsTest" />
</classes>
</test>
</suite>
What would be the right approach in setting initial sequential and then parallel execution of the tests? Any help would be appreciated! Thanks.
You should give Testcases method name by numbers.
you must be using excel sheet for data provider format that will help you run test cases according to Testcases number

Running specific test class inside a package first in TestNG

I want to run a specific test class before running other tests in the same package using testng xml.
test_package
-------------
testclass1
testclass2
testclass3
I want testclass3 to run first.
Selenium grid is being used. Currently the longer test runs at last and other nodes are idle making the test duration longer. How can i give first priority to longer test classes.
<test name="Tests">
<packages>
<package name="tests.*"/>
</packages>
</test>-->
There are multiple classes inside the package but i want selected test class to run ahead of all.
If you want to run the classes in order, you can specify the order in the xml as given below, By default testng, run the classes in order mentioned in xml.
<test name="Tests" preserve-order="true">
<classes>
<class name="tests.testclass3"/>
<class name="tests.testclass1"/>
<class name="tests.testclass2" />
</classes>
</test>
If you include
<package name="tests.*"/>
Then looks there is no way to say order of classes to execute.
You can try by providing classes with preserve order is testng.xml file or you can create xml file by coding like here.

"Executing a test in testNG based on condition"

i have a testNG xml which will execute a (one)test case 2 times. the difference between each time is the test level parameter. so i have to execute the test case with 2 different parameter (2 user).
Now i will be creating new xml (suite of xml) from which i will call the existing xml. i will define a new parameter in suite xml for the User.
Expectation is if suite xml parameter="user2", then the child xml should execute the test case only once which has parameter as user2.
i tried beanshell scripting and found it useful for method-selector. but i want to make decision for test level and not method level.
Below is the testNG.xml which calls the test case 2 times with different user value. TestCase will be called first time with User="USER1" and second time with User="USER2".
<?xml version="1.0"?>
<suite name="TestLoad">
<test verbose="10" name="TestForUser1" preserve-order="true">
<parameter name="User" value="USER1"/>
<classes>
<class name="com.dummy.test.TestCase"/>
</classes>
</test>
<test verbose="10" name="TestForUser2" preserve-order="true">
<parameter name="User" value="USER2"/>
<classes>
<class name="com.dummy.test.TestCase"/>
</classes>
</test>
</suite>
Below is the Suite of xml which i will be newly creating to call many testng.xml described as above.
<?xml version="1.0"?>
<suite name="suiteOfXml">
<parameter name="User" value="USER1"/>
<suite-files>
<suite-file path="TestLoad.xml"/>
<suite-file path="TestStage.xml"/>
</suite-files>
</suite>
Expectation is something like:
if the suiteOfXml has User="USER1" then each testNG xml should run the TestCase only once with User=USER1.
if the suiteOfXml has User="USER2" then each testNG xml should run the TestCase only once with User=USER2.
if the suiteOfXml has User="ALL" then each testNG xml should run the TestCase twice. once with User=USER1 and next time with User=USER2.
I cannot make any changes to the TestCase (java class level). condition should be made at xml only.
Kindly provide a possible solution. Thanks in advance
Here's what you need to be doing to get past this issue (You dont need a suite of suites etc., )
Create an implementation of the TestNG listener interface org.testng.IAlterSuiteListener within which you basically read the value of the parameter and then based on it, you decide which <test> should be created and how it would look like etc.,
You create a suite xml file that only contains the parameter which would help you decide what <test> should be there and also a reference to the newly created listener from the previous step.
That should do. Using this approach you can basically do all the things that you have listed out.
Here's how a sample listener would look like (In this example, we are getting the packages to be executed via a JVM argument and then based on that, we are building our <test> tag)
package com.rationaleemotions.wordpress;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.Collections;
import java.util.List;
public class SimpleSuiteAlterer implements IAlterSuiteListener {
#Override
public void alter(List suites) {
XmlSuite suite = suites.get(0);
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName("CommandLine_Test");
String packages = System.getProperty("package", suite.getParameter("package"));
XmlPackage xmlPackage = new XmlPackage(packages);
xmlTest.setXmlPackages(Collections.singletonList(xmlPackage));
}
}
And here's how the suite xml would look like
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package-Suites" verbose="2">
<parameter name="package" value="com.rationaleemotions"/>
<listeners>
<listener class-name="com.rationaleemotions.wordpress.SimpleSuiteAlterer"/>
</listeners>
</suite>
For a more detailed explanation and some code samples, refer to my blog post https://rationaleemotions.com/building_dynamic_testng_suites/

Jenkins TestNG result integration

I have lots of longrunning separate tests, grouped by functionality in suites in some xml files.
<suite name="Suite1" thread-count="1" parallel="false" verbose="1">
<test name="A" preserve-order="true">
<classes>
...
</classes>
</test>
<test name="B" preserve-order="true">
<classes>
...
</classes>
</test>
</suite>
My problem is, I'd like to generate some kind of html/xml result from grouping the results based on their test name / tag.
For example, I have these tests from A to E, and the result groups woudl be :
A,B,C
B,D,E
I know I could create a separate suite for the tasks (A,B,C) but in that case B would execute again in the second suite, which is not desirable since they are long running... So the idea would be to run every test once, but generate combined reports based on their name/tag.
Is there a plugin for this ? Or how could I achieve this ?
You can make your own reporter which will be generated the expected result.
The default reporter could be a good start.

testng preserve-order doesn't work with priority

I have a testng suite file which has 10 classes inside a test. Say
<classes>
<class name = "Class1">
<class name = "Class2">
<class name = "Class3">
</classes>
Now class1 has 3 methods with priority 1,2,3 in order and the same with class 2 and class3. When I run the job in Jenkins with suite having preserve-order="true", the tests run as class1-priority1 test, class2-priority1, class3-priority1, class1-priority2, class2-priority2 etc.
I need to have all the tests associated with class1 executed inorder first and then class2 and then class3.
Could someone tell me why this behavior is?
The default order depends on the Java reflection API. You may include method names under < methods > in the order you want. This may be cumbersome & may look clumsy while file gets bigger. Nevertheless, it may help you in this regard.
Example xml:
<class name="Fully qualified class name without extension">
<methods>
<include name="method_1" />
<include name="method_1" />
.....
.....
<include name="method_N" />
</methods>
</class>
Other than the previous solution, you may add dependency for test cases in your test methods using annotation. But, changing/modifying xml will far better than modifying codes.
The order you describe is the expected one.
Instead of priority, you should try to use dependsOnMethods.