Continue TestNG test after failed method - java

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.

Related

How to set TestNG to run which test that available

I have suite of testNG (Maven above) that have tests inside it, I want to run some of the tests meanwhile the others won't run (they can ignored or even failed) - the case is for few mobiles that will be connect to the PC and some that not,
the suite looks like so:
<test name="Redmi 6A">
<listeners>
<listener class-name="com.qa.listeners.TestListener" />
</listeners>
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="deviceName" value="Redmi 6A" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="redmi6A.Installation"/>
</classes>
</test>
<test name="OnePlus 6 Pro">
<listeners>
<listener class-name="com.qa.listeners.TestListener" />
</listeners>
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="deviceName" value="onePlus 6Pro" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="onePlus6Pro.Installation"/>
</classes>
</test>
so I want that the code will work if both devices are connected and in other hand only if one of them is connected
Option1
Just delegate testNG xml generation to some pre-build CI step.
Create a class, which e.g. checks adb devices list and includes or excludes some xml parts.
Run the class and generate xml and use it in test run.
Option2
Use IAnnotationTransformListener to disable tests based on some logic.
You have to define somehow which test depends on which device. In your example, I see you can retrieve this from the class package name. Then you have to check if the device is connected and disable the test if needed.
https://techfortesting.wordpress.com/2019/12/27/iannotation-transformer/
Note: you have to apply this listener in xml, not in the code. This is because it should be applied in the early stage of TestNG execution.
Sorry, I've not provided the final code snippets, because it takes time to implement this.

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 pass Jenkins configurations to overwrite Automation Configuration

I have a project which have Grid + Selenium + Java + Testng + maven.
Is there is a way to set up, that the person who will run test he will be able to pass his own configurations which will overwrite default configs?
Here is my testng.xml file which i`m running:
<suite name="TestSuite" parallel="tests">
<test name="firefox test">
<parameters>
<parameter name="platform" value="MAC" />
<parameter name="browser" value="firefox" />
<parameter name="version" value="50.1.0" />
<parameter name="url" value="google.com" />
</parameters>
<classes>
<class name="com.ParallelTest.CreateRandomProfileTest"/>
<class name="com.ParallelTest.LogInTest">
</class>
</classes>
</test>
</suite>
For example if someone will want to run 5 Tests against Chrome on Win? How to pass it without changing the code and where it should be overwritten. Can someone drop me a link for resources where i can check it? Thanks!
It can be done easily by file parameter plugin of Jenkins.
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
Ie, Lets assume this a workspace with default folder structure
data
pageobjects
tests
suite.xml
now an user uploads his own version of suite.xml and run the job.
Jenkins will pull the project from source control/github and replaces the suite.xml with the one uploaded by the user and runs the project.
You could try this way as well.
Pass variable from jenkins to testng.xml

Use one class more than once in TestNG

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.

Categories