Unable to run TestNG classes in sequential order - java

I want my testng to run my classes in Sequential mode. I have mentioned suite name="WebData-Sanity" parallel="false" thread-count="1" in my testng.xml, Still its running parallel. All the test classes are starting at a time. How ever If I am putting individual classes in individual <test> its running sequential.
<suite name="WebData-Sanity" parallel="false" thread-count="1">
<test name="WebData-Sanity" preserve-order="`enter code here`true" parallel="false"
thread-count="1">
<groups>
<run>
<include name="Sanity" />
</run>
</groups>
<classes>
<class name="flipKartTest.FlipKartFirstPageTest" />
</classes>
</test>
<test name="Regression">
<classes>
<class name="flipKartTest.FlipKartHomePageTest" />
</classes>
</test>
</suite>
I want all my classes in Same <tests> tag

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

Referring test groups in testng.xml

Let's say I've annotated my test class with a test group in TestNG:
#Test(group='smoke-tests')
public class CheckEnvironmentTest {
...
}
Is there a way to refer to this group in testng.xml? Something like this (if it was implemented):
<suite name="Suite1" verbose="1" >
<test name="Sequential" parallel="false" >
<test-group ref="smoke-tests" />
</test>
<test name="ParallelGroup" parallel="classes" >
<test-group ref="regular-tests" />
</test>
</suite>
You might want to do something like this
<test name="sample">
<groups>
<run>
<include name="smoke-tests"/>
</run>
</groups>
<classes>
<class name="CheckEnvironmentTest"/>
</classes>
</test>

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>

TestNG - cannot run same test twice in a suite

I have a TestNG test suite that is working fine except for one thing: I cannot run the same test twice. The reason I want to run a test twice is that I am testing web services that log a user in/out, and I want to verify that the login works, then that the logout work, and then log them in again so that the subsequent tests, which require the user to be logged in, can proceed.
Here is the relevant section of my testng.xml:
<suite name="WebServiceTestSuite">
<test name="webServiceTest" verbose="10" parallel="none">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationCreateTest"></include>
<include name="registrationActivateTest"></include>
<include name="appUserLoginTest"></include>
<include name="appUserLogoutTest"></include>
<include name="appUserLoginTest"></include>
<include name="sessionValidateTest"></include>
<include name="sessionExtendTest"></include>
</methods>
</class>
</classes>
</test>
</suite>
As you can see, the "appUserLoginTest" is called twice. However, when I debug this I can clearly see that it is only actually run the first time it is called. After that the execution proceeds as if the second line calling it didn't exist.
This is actually by design in TestNG. I see two potential options:
First, look at using a DataProvider with your tests so they can be executed repeatedly with different parameters.
Or, alter the XML layout so that each method is part of its own tag. In this configuration, the XML will be longer, but you'll be able to invoke a test method as many times as you like as part of a suite.
EDIT: More details now.
In response to your comment, here's a sample XML layout that worked for me when I was up against this same problem. Try this:
<suite name="WebServiceTestSuite">
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationCreateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationActivateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLoginTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLogoutTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLoginTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="sessionValidateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="sessionExtendTest"></include>
</methods>
</class>
</classes>
</test>
</suite>
I removed the verbose="10" parallel="none" from the <test> tags, as the layout that worked for me didn't have this... You may be able to add the verbose="10" to the <suite> tag to get any specific functionality there that you need. Have a go with this and post your results and I'll be happy to help further. Without seeing your runner configuration or knowing too much about the rest of your setup, there may be some other things to consider.
You'll find that this is a much more verbose XML layout, but it solved this same problem for me at the time. You could also consider re-arranging your code a bit and making an #Test method that calls all the methods on this XML as a single test, allowing you to reduce the length of your XML file.
Solving the problem of code duplication in TestNG test I could find only this solution:
Create a rootsuite.xml file like:
<suite name="Description">
<suite-files>
<suite-file path="../../Actions/Test1.xml"/>
<suite-file path="../../Actions/Test2.xml"/>
<suite-file path="Actions/Test3.xml"/>
<suite-file path="../../Common/Close Browser.xml"/>
</suite-files>
<suite-files>
<suite-file path="../../Actions/Test1.xml"/>
<suite-file path="../../Actions/Test2.xml"/>
<suite-file path="Actions/Test4.xml"/>
<suite-file path="../../Common/Close Browser.xml"/>
</suite-files>
</suite>
Important! You can not include duplicated suite-files inside Test1.xml, Test2.xml, Test3.xml, Test4.xml - all duplicated suites will be ignored.
Note the version of TestNG must be at least 6.9.9
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.9</version>
<scope>test</scope>
</dependency>

Categories