Set system property inside TestNG XML suite - java

I have a test suite like this:
<suite name="MySuite">
<parameter name="key" value="val"/>
<test name="testing">
<packages>
<package name="mypack.testpackage.*"/>
</packages>
</test>
</suite>
but I need to use key as a system property instead of a parameter (because I use it later in some Spring ELs where I can't use #Parameters to take its value).
So I'd like to use something like:
<system-property name="key" value="val"/>
or
<parameter name="key" value="val" system-property="true"/>
instead of the parameter
<parameter name="key" value="val"/>
Does TestNG support this?

Doesn't support out of the box - you will need to write code for it to override if values are available as system properties.

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 #Parameters value from Jenkins?

Currently Jenkins is able to run This test using Maven and Testng.
I Want to make a Drop Down list in Jenkins Which will have:
For example:
Platform: - MAC / - Win / - Android / - IOS.
Users: -user1#gmail / -user2#gmail - user3#gmail.
And if i will choose from drop down Android and User3 it will start the test with this set up.
Is it possible to implement this way? Because currently in order to change test i need to enter XML file and overwrite the configurations, then push them, and then run.
<test name="Chrome test 1">
<parameters>
<parameter name="platform" value="MAC" />
<parameter name="browser" value="chrome" />
<parameter name="version" value="56.0" />
<parameter name="userEmail" value="123456#gmail.com" />
<parameter name="password" value="123123Test" />
<parameter name="url" value="https://dev2.com" />
</parameters>
<classes>
<class name="test1"/>
</classes>
</test>
<test name="Chrome test 2">
<parameters>
<parameter name="platform" value="MAC" />
<parameter name="browser" value="chrome" />
<parameter name="version" value="56.0" />
<parameter name="userEmail" value="123123#gmail.com" />
<parameter name="password" value="123123Test" />
<parameter name="url" value="https://dev1.com" />
</parameters>
<classes>
<class name="test2"/>
</classes>
</test>
I would propose a slightly different solution.
Since your tests will be run on Jenkins, you can go for Parametrized Build.
It is a plug-in that allows for building with parameters.
You can also choose them from a drop-down.
I think you would need a choice parameter to choose from a drop-down.
You can access such parameters with Maven (no need for TestNG here).

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

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