How to run TestNG tests parallelly through xml with priority - java

I have a scenario where a class "Pre-Requisite" class have to be executed first and then I have 5 testng classes which have to use certain data gathered from "Pre-Requisite.java" class and execute all 5 classes parallelly. Eg. of an xml below:
<test name="Suite">
<classes>
<class name="Pre-Requisite"/> // To be run first
</test>
<test name="Suite">
<classes>
<class name="AClass"/> // To be run in parallel with below classes
<class name="BClass"/>
<class name="CClass"/>
<class name="DClass"/>
<class name="EClass"/>
</test>
</suite>
can someone please help me how do I achieve this?

You can mark the methods in the class "Pre-Requisite" with the TestNG annotations - "#BeforeClass" or "#BeforeSuite", so it will execute the code in the class "Pre-Requisite" first.
Then you can execute other classes in parallel through testng.xml, like:
<suite name="Suite">
<test name="Test1" parallel="classes">
<classes>
<class name="AClass"/>
<class name="BClass"/>
<class name="CClass"/>
<class name="DClass"/>
<class name="EClass"/>
</classes>
</test>
</suite>

Related

How to execute two suite files(which contains class files) in Parallel in Selenium Java Maven Framework

In my organization as per existing framework. The suite are getting created in xml file with class files and these xml files are called in testng.xml. I have provided the content below.
The current execution pattern is, Suite1.xml will trigger first and it will execute TestScript1,TestScript2,TestScript3(class files) in sequential order. Then it will initiate Suite2.xml and it will execute TestScript11,TestScript12,TestScript13(class files) in sequential order.
PROBLEM: I would like to have a mechanism in such a way, Scripts/Class files mentioned in Suite1.xml should go sequentially. But at the same time in parallel, Suite2.xml related Scripts/Class files should get executed sequentially.
So suites should execute parallel, but scripts/classes in the suite should execute sequentially.
REQUEST: Can someone please suggest a solution or what modification to be done to them xml to achieve this.
Testng.xml looks as below:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<suite name="MyFramework">
<suite-files>
<suite-file path="SUITE1.xml"/>
<suite-file path="SUITE2.xml"/>
</suite-files>
</suite>
SUITE1.xml as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<suite configfailurepolicy="continue" name="SUITE1" preserve-order="true">
<test name="TestScript1">
<classes>
<class name="com.scripts.customer.module.TestScript1"/>
</classes>
</test>
<test name="TestScript2">
<classes>
<class name="com.scripts.customer.module.TestScript2"/>
</classes>
</test>
<test name="TestScript3">
<classes>
<class name="com.scripts.customer.module.TestScript3"/>
</classes>
</test>
</suite>
SUITE2.xml is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<suite configfailurepolicy="continue" name="SUITE1" preserve-order="true">
<test name="TestScript11">
<classes>
<class name="com.scripts.customer.module.TestScript11"/>
</classes>
</test>
<test name="TestScript12">
<classes>
<class name="com.scripts.customer.module.TestScript12"/>
</classes>
</test>
<test name="TestScript13">
<classes>
<class name="com.scripts.customer.module.TestScript13"/>
</classes>
</test>
</suite>

testng suite doesn't run in the order specified

I have below testng test suite.
<suite name="MySuite" parallel="false">
<test name="MyTest" parallel="false" preserve-order="true">
<classes>
<class name="com.my.package.Test1"/>
<class name="com.my.package.Test2"/>
<class name="com.my.package.Test3"/>
<class name="com.my.package.Test4"/>
<class name="com.my.package.Test5"/>
<class name="com.my.package.Test6"/>
<class name="com.my.package.Test7"/>
<class name="com.my.package.Test8"/>
<class name="com.my.package.Test9"/>
<class name="com.my.package.Test10"/>
<class name="com.my.package.Test11"/>
<class name="com.my.package.Test12"/>
<class name="com.my.package.Test13"/>
<class name="com.my.package.Test14"/>
<class name="com.my.package.Test15"/>
<class name="com.my.package.Test16"/>
<class name="com.my.package.Test17"/>
<class name="com.my.package.Test18"/>
<class name="com.my.package.Test19"/>
<class name="com.my.package.Test20"/>
<class name="com.my.package.Test21"/>
<class name="com.my.package.Test22"/>
<class name="com.my.package.Test23"/>
<class name="com.my.package.Test24"/>
<class name="com.my.package.Test25"/>
</classes>
</test>
</suite>
I use testng 6.14.3
And I execute the suite with following command.
mvn test -DsuiteXmlFile=suite.xml
I expect the tests to run in the order specified in the xml (I have used preserve-order="true" attribute). But, the tests are executed in below order.
com.my.package.Test1
com.my.package.Test23
com.my.package.Test2
com.my.package.Test24
com.my.package.Test3
com.my.package.Test25
com.my.package.Test4
com.my.package.Test5
com.my.package.Test6
...
...
If the number of test classes is less than 23, they run in the given order. But, when the count is 23 or more, tests are executed in above pattern.
What could be wrong here?
I found the issue. The cause was class Test22 not having any test methods in it. The test that was there had been commented out. When the test was added again to the class, suite started to run again in expected order.
Not sure whether this is a bug in Testng or not.

I want to create xml in order to execute my classes

I have created a TestNG XML File, which I have called multiple classes. My concern is that I want to execute my test cases on all classes which I have arranged from top to bottom like below. However some of the bottom cases depend on upper cases, and whenever I run my XML File it selects the classes randomly. How would I go about doing this?
Type - 1
{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite3">
<test name="Test3" preserve-order="true">
<classes>
<class name="com.netgearInsight.testReports.RegistrationTestCase"/>
<class name="com.netgearInsight.testReports.LoginTestCase"/>
<class name="com.netgearInsight.testReports.AddNetworkTestCase"/>
<class name="com.netgearInsight.testReports.AddDevicesTestCase"/>
<class name="com.netgearInsight.testReports.CreateSsidTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDevicesTestCase"/>
<class name="com.netgearInsight.accountCheck.About"/>
<class name="com.netgearInsight.accountCheck.AccountManage"/>
<class name="com.netgearInsight.testRunEnd.GetTotalStatus"/>
<class name="com.netgearInsight.testRunEnd.SendReport"/>
</classes>
</test> <!-- Test3 -->
</suite> <!-- Suite3 -->
}
Type - 2
{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
<test thread-count="5" name="Test One" >
<classes>
<class name="com.netgearInsight.testReports.RegistrationTestCase"/>
<class name="com.netgearInsight.testReports.LoginTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Two" >
<classes>
<class name="com.netgearInsight.testReports.AddNetworkTestCase"/>
<class name="com.netgearInsight.testReports.AddDevicesTestCase"/>
<class name="com.netgearInsight.testReports.CreateSsidTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Three" >
<classes>
<class name="com.netgearInsight.testReports.UrlFilterTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDomainTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDevicesTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Four" >
<classes>
<class name="com.netgearInsight.testReports.AddDataVLAN"/>
<class name="com.netgearInsight.accountCheck.AccountManage"/>
<class name="com.netgearInsight.accountCheck.About"/>
<class name="com.netgearInsight.accountCheck.ChangePassword"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Five">
<classes>
<class name="com.netgearInsight.testRunEnd.GetTotalStatus"/>
<class name="com.netgearInsight.testRunEnd.SendReport"/>
</classes>
</test> <!-- Test -->`enter code here`
</suite> <!-- Suite -->
}

TestNG Based Selenium Tests not running in Parallel

I am using the below TestNG Config to enable parallel execution of Selenium tests.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Java Code:
#Test(dataProvider = "TestData")
public void firstTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void secondTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
The Selenium tests are expected to run in parallel. I expect 2 browsers to be open and run the test script.
But I see only 1 browser and all 3 tests run one after the other and not in parallel. I have tried using test, methods, class, instance options for "parallel" attribute.
Any help?
This is due to a bug in TestNG 6.13.1 [ See GITHUB-1636 for more details ]
I have fixed this in the latest SNAPSHOT of TestNG (6.14-SNAPSHOT) and this should be available for use in the released version of TestNG (6.14).
But until then, please alter your suite xml file to look like below :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
The work-around is basically to add the attributes parallel="methods" thread-count="2" at the <test> level also.
Seperate all the test and then try with parallel="test"
<test name="Suite Test1">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
<test name="Suite Test2">
<classes>
<class name="//..//SampleTest">
</class>
</classes>
</test>
<test name="Suite Test3">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
</suite>

TestNG:Cannot find class in classpath, testng.xml

This is my testng.xml file, i named it differently, but it should still work:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TEST_LINKS">
<test name="TECT на подбор спеца">
<classes>
<class name="Podbor_specalista_url_click_test" />
</classes>
</test>
<test name="TECT на клик блога">
<classes>
<class name="Blog_url_click_test" />
</classes>
</test>
</suite>
Now, i get the error, but where do I fix it?
i want to run the whole test suite consisting of these tests:
You need to give the fully qualified class name of the class you want to test in class tag, something like this
<suite thread-count="5" name="Ant suite" junit="false" annotations="JDK">
<test name="Ant test">
<classes>
<class name="com.sample.test.MyUnitTest"/>
</classes>
</test>
</suite>

Categories