Passing Paramters to TestNG XML file from Jenkins - java

I am trying to create a Jenkins jobs that will build an XML request for me, it will eventually be a tool.
I already have a number of Tests running using this system working with no issue but I want to be able to pass a parameter from Jenkins straight into the XML.
I can pass it into the Test using the Get System Property method.
My XML looks like this
<parameter name="env" value="xxx"/>
<parameter name="testName" value="Supplier: SKY Test"/>
<test name="multicom-test-sky">
<!--Build Post URL from the following-->
<parameter name="protocol" value="http://"/>
<parameter name="env" value="xxxx"/>
<parameter name="server" value="xxxx"/>
<parameter name="port" value="xxx"/>
<parameter name="endPoint" value="xxx"/>
<!--Syndicator login-->
<parameter name="syndicatorID" value="xxxxxxx"/>
<parameter name="syndicatorPassword" value="xxxxxx"/>
<parameter name="target" value="test"/>
<!--Searching Parameters-->
<parameter name="departure" value="LGW"/>
<parameter name="destination" value="GVA"/>
<parameter name="supplier" value="SKY"/>
<parameter name="numOfAdults" value="2"/>
<parameter name="numOfChildren" value="2"/>
<parameter name="numOfInfants" value="0"/>
<parameter name="startDate" value="20170318"/>
<parameter name="endDate" value="20170318"/>
<parameter name="minNumberofNights" value="7"/>
<parameter name="maxNumberofNights" value="7"/>
<parameter name="childOneAge" value="15"/>
<parameter name="childTwoAge" value="10"/>
<!--tests to run-->
<classes>
<class name="com.multicom.fab.api.EndToEndSimpleBooking"/>
</classes>
</test>
I want to be able to pass the value of these parameters in from Jenkins and set them at this level so then they are passed to the test for execution.
Any ideas on how this is possible?
Appreciate the help in advanced

TestNg can use system properties as parameters so, if you do not want to write any additional code - just run TestNG from command line in Jenkins and pass your parameters like java -Dpath=$PATH org.testng.TestNG your.class.java.
Better way is to use properties or run it via maven or gradle.

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.

How can I get parameters into this test run using org.testng.TestNG?

I am testing a web client application using Java 8 Update 151 & Selenium 3.8.1. and TestNG. I have to start one test from within an already running test.
Here is my code for that:
ITestNGListener testListenerAdapter = null;
TestNG testNG = new TestNG();
testNG.addListener(testListenerAdapter);
testNG.setTestClasses(new Class[] { tests.login.LoginTest.class });
testNG.run();
My tests run using a bunch of parameters defined in the testng.xml file, but this does not pick those parameters up, which is causing the test to fail.
This is the top of my testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<parameter name="browser" value="firefox" />
<parameter name="url" value="URL goes here!"/>
<parameter name="printToFile" value="false" />
<parameter name="trace" value="false" />
<parameter name="opt" value="true" />
<parameter name="customer" value="Demo" />
<parameter name="network" value="NSG Designated Streets (Type 1/2)" />
<parameter name="buildNo" value="312" />
<listeners>
<listener class-name="listeners.TestNGCustomReportListener" />
</listeners>
<test name="firefoxTest">
<parameter name="browser" value="firefox" />
<parameter name="url" value="URL goes here!" />
<parameter name="printToFile" value="false" />
<parameter name="trace" value="false" />
<parameter name="opt" value="true" />
<parameter name="customer" value="Demo" />
<parameter name="network" value="NSG Designated Streets (Type 1/2)" />
I tried adding #Parameters at the top of the test, as shown here, but that did not work. No parameter was picked up.
public class AutoLogOutTest extends CrossBrowserTest {
#Parameters("url")
#Test(groups = { "all", "login", "simple" })
public void autoLogOut(#Optional("") String url) {
This is described here, but does not work as this author suggests it should: http://toolsqa.com/selenium-webdriver/testng-parameters-data-provider/
For some reason testng 6.4 and up no longer supports parameter inside the method: Is there a way to specify parameters for included methods in TestNG suite.xml?
How can I get my parameters into this test?
First of all, you are not using DataProvider anywhere in the Test you mentioned.
Second you need to choose, either run the XML or the class file, you cannot run both. If you want to use XML parameters, you need to use the XML file, not the LoginTest.class.
You can use the XML file like this:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;
And then you can pass this XmlSuite to TestNG:
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
The class is not aware about the XML file the way you start TestNG.

Unable to switch locale in Appium Android

Things were working pretty well until this issue come up. Locale unable to switch while I'm running test case through Appium+TestNG+Android Emulator.TestNG configuration is below:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose="1" name="AndroidSuite"
skipfailedinvocationcounts="false" junit="false" parallel="false"
data-provider-thread-count="50" annotations="JDK">
<parameter name="mobileNodeType" value="appium" />
<parameter name="seleniumhost" value="localhost" />
<parameter name="seleniumport" value="4723" />
<parameter name="mobileAppLanguage" value="de_DE" />
<parameter name="mobileAppLocale" value="DE" />
<parameter name="mobilePlatform" value="ANDROID" />
<parameter name="appPath" value="{apk file path}"/>
<!-- <parameter name="appPath" value="{APP path}"/> -->
<parameter name="mobileDevice" value="android:6.0"/>
<parameter name="mobileDeviceType" value="nexus5"/>
<test verbose="2" name="AndroidTest" annotations="JDK">
<classes>
<class name="com.x.y.{ClassName}">
<methods>
<include name="{CaseName}"></include>
</methods>
</class>
</classes>
</test>
</suite>
Approaches I've tried:
1.Set locale from Appium command line.
2.Wipe emulator data and restart.
3.Delete and recreate emulator.
4.Reinstall Appium command line tool.
Please take a look if anyone had the similar issue.
Thanks in advance.
I figured out the solution myself. Mobile App language in Android must be like
<parameter name="mobileAppLanguage" value="de_DE" />
Instead of
<parameter name="mobileAppLanguage" value="de" />

Set system property inside TestNG XML suite

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.

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

Categories