Use one class more than once in TestNG - java

There is and example of structure in my test running using TestNG and Selenium:
<test>
<classes>
<class name="com.pou.MyTest">
<methods>
// params here
<include name="myMethodOne" />
</methods>
</class>
<class name="com.pou.MyTest">
<methods>
// params here
<include name="myMethodTwo" />
</methods>
</class>
</classes>
</test>
After running this suite I recive an error:
org.testng.TestNGException: No free nodes found in:[DynamicGraph
I was told that it is caused by calling the same method twice in one
Now the question is: Can I use the same class twice in one test?
I'll also add that for some reasons I need to have it in one test, so spliting it to two different tests is not the approach I need.

Consider using a #DataProvider instead, classes can only appear once in each test tag.

Related

Is there a way to set global parameters in TestNG?

I'm currently automating web app using Selenium WebDriver framework together with TestNG. I want to provide parameters for each test classes within testing.xml file, but on the web resources, I've seen only approach where certain parameters are predefined to use for single classes like:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization Test Suite">
<test name="Testing Parameterization">
<parameter name="browser" value="Firefox"/>
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
</suite>
where the class name, clearly states only one TestParameters class.
Is there any way to set some parameters right there as global ones? So we can use them in any test class within the project?
I'm not really sure if I understand your question, but you can define parameters on suite level (for each test, in the example suite-param), or on test level:
As taken from https://howtodoinjava.com/testng/testng-test-parameters-through-testng-xml-and-parameters-annotation/
<suite name="Parameter test Suite" verbose="1">
<!-- This parameter will be passed to every test in this suite -->
<parameter name="suite-param" value="suite level parameter" />
<test name="Parameter Test one">
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestOne" />
</methods>
</class>
</classes>
</test>
<test name="Parameter Test two">
<!-- This parameter will be passed this test only -->
<parameter name="test-two-param" value="Test two parameter" />
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestTwo" />
</methods>
</class>
</classes>
</test>
<test name="Parameter Test three">
<!-- Overriding suite level parameter -->
<parameter name="suite-param" value="overiding suite parameter" />
<!-- Test specific parameter -->
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
</suite>

How to pass values between multiple testng xml files each running in different threads

In selenium, I am doing parallel execution of multiple xml files using ThreadLocal concept. Now, I want to make communication between threads that:
TestA in xml1 is passed.
TestA in xml2 should be executed when TestA in xml1 is passed. Until then, I need to wait xml2's thread.
I tried with dependencies, but it tells only within xml file.
Please note, I am working in a Product that has different modules with nearly 10k Test cases. It takes a whole day to complete run with the current ThreadLocal concept.
I want to minimize the execution time. Kindly state any ideas.
I am not sure, how it fits in your case but you can use groups in your tests and
you can specify your group dependencies in the testng.xml file(s).
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite for End To End">
<test name="AUT_E2E_01">
<parameter name="browser" value="Chrome" />
<classes>
<class name="com.myunit.regressiontests">
<methods>
<include name="Test1" />
<include name="Test11" dependsOnMethods="SomeMethod" />
</methods>
</class>
</classes>
<groups>
<dependencies>
<group name="SomeOther-Group" depends-on="Some-Group" />
</dependencies>
</groups>
</test>
</suite>

How to run test cases belonging to a particular group in parallel using TestNG

How to run test cases belonging to a particular group in parallel using TestNG.
For example, let say there is a group named "up" containing 10 test cases. I want to run all these test cases in parallel (threads=number of test cases). How can I achieve in using TestNG.
Try the following testng.xml. If you don't know the number of test cases before you run your code, just be sure that the threadCount is higher than it :)
<suite parallel="methods" threadCount="10">
<test name="MyTest">
<groups>
<run>
<include name="up" />
</run>
</groups>
<classes>
<class name="test.TestClass" />
</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>

Continue TestNG test after failed method

I have a TestNG suite set up to perform a number of selenium tests that. The suite has a number of tests in it, which each have a number of classes, which each have one or two test methods. I am currently experiencing behavior where the whole test ( tag) will stop if one test method fails (i.e. results will look like: pass, pass, pass, fail, skip, skip, skip, skip, skip, skip).
Is there a way for me to set up my tests so that a single method failure won't prevent the others from running? My test methods are mostly independent from one another, and the classes definitely are, but all of the classes are sharing a WebDriver instance.
Here is an example of one of my tags in my XML file. There are several other test cases with the same class list but different parameters. unfortunately I cannot show my actual code as it contains privileged information.
<test name="OS_X_Mavericks_Firefox_27_Test">
<parameter name="browser" value="Firefox" />
<parameter name="browser_version" value="27.0" />
<parameter name="os" value="OS X" />
<parameter name="os_version" value="Mavericks" />
<parameter name="resolution" value="1920x1080" />
<classes>
<class name="SuperTestClass" />
<class name="testClass1" />
<class name="testClass2" />
<class name="testClass3" />
<class name="testClass4" />
<class name="testClass5" />
<class name="testClasa6" />
<class name="testClass7" />
<class name="testClass8" />
<class name="testClass9" />
</classes>
</test>
As it turns out, the problem was with my inheritance structure. I had an #AfterClass method in SuperTestClass, which all of the other test classes inherited from. So, when one test failed in the #AfterClass method, all subsequent tests that inherited from SuperTestClass (all of the tests) failed.

Categories