How to set TestNG to run which test that available - java

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.

Related

TestNG get the value of "test" level parameter from "ISuiteListener" class's "onStart" method

In my TestNG.xml file , I have two different values for Test level parameter, "TestRailRunId".
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="4" name="Change_Management_Regression_Suite" parallel="tests">
<listeners>
<listener class-name="com.sam.reporting.SuiteListener" />
<listener class-name="com.sam.reporting.Listeners" />
</listeners>
<parameter name="browser" value="Chrome" />
<parameter name="browserVersion" value="104" />
<test name="CM-04">
<parameter name="TestRailRunId" value="200" />
<classes>
<class name="com.sam.testdata.TC01"/>
</classes>
</test> <!-- Test -->
<test name="CM-04">
<parameter name="TestRailRunId" value="201" />
<classes>
<class name="com.sam.testdata.TC02"/>
</classes>
</test> <!-- Test -->
</suite>
So in my "SuiteListener" class I implements ISuiteListener class. So in it's onStart method,
I want to get the TestRailRunId value from current test.
IS this possible? If so please help.
Currently what I did was defining it in Suite level and using below code snippet ,get the value.
String TestRailRunId = suite.getXmlSuite().getParameter("TestRailRunId");
Here is the current full code.
#Override
public void onStart(ISuite suite) {
String TestRailRunId = suite.getXmlSuite().getParameter("TestRailRunId");
}

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>

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

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 can I ensure testNG reports exceptions for the correct test?

I'm attempting to create a suite of tests to run in parallel using testNG, PerfectoMobile, and Maven. The tests are running in parallel, but it seems like exceptions are being reported for a random thread each time. I know this because the test i have named "Xperia M2" is the one that is generating the exception, but testNG's IntelliJ reporter shows the exception coming from (seemingly) random tests instead.
You can see a screenshot here: http://i.imgur.com/fLscGqD.png
As you can see from the screenshot, the test "Test Android HTC One M9" is reporting the exception that was generated from the Xperia M2 test (just below it, marked as skipped (as a side-note, why is that test being marked as skipped?)).
Here is my TestNG code:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="tests">
<parameter name="mcm" value="-redacted-" />
<parameter name="mcmUser" value="-redacted-" />
<parameter name="mcmPassword" value="-redacted-" />
<test name="Test Android Galaxy S6">
<parameter name="platformName" value="Android" />
<parameter name="app" value="-redacted-" />
<parameter name="appId" value="-redacted-" />
<parameter name="deviceModel" value="Galaxy S6" />
<classes>
<class name="testNG.SmokeTest"/>
</classes>
</test>
<test name="Test Android LG G4">
<parameter name="platformName" value="Android" />
<parameter name="app" value="-redacted-" />
<parameter name="appId" value="-redacted-" />
<parameter name="deviceModel" value="G4" />
<classes>
<class name="testNG.SmokeTest"/>
</classes>
</test>
<test name="Test Android HTC One M9">
<parameter name="platformName" value="Android" />
<parameter name="app" value="-redacted-" />
<parameter name="appId" value="-redacted-" />
<parameter name="deviceModel" value="One M9" />
<classes>
<class name="testNG.SmokeTest"/>
</classes>
</test>
<test name="Test Android Galaxy Note 4">
<parameter name="platformName" value="Android" />
<parameter name="app" value="-redacted-" />
<parameter name="appId" value="-redacted-" />
<parameter name="deviceModel" value="Galaxy Note 4" />
<classes>
<class name="testNG.SmokeTest"/>
</classes>
</test>
<test name="Test Android Xperia M2">
<parameter name="platformName" value="Android" />
<parameter name="app" value="-redacted-" />
<parameter name="appId" value="-redacted-" />
<parameter name="deviceModel" value="Xperia M2" />
<classes>
<class name="testNG.SmokeTest"/>
</classes>
</test>
<test name="Test Android Xperia Z3">
<parameter name="platformName" value="Android" />
<parameter name="app" value="-redacted-" />
<parameter name="appId" value="-redacted-" />
<parameter name="deviceModel" value="Xperia Z3" />
<classes>
<class name="testNG.SmokeTest"/>
</classes>
</test>
</suite>
I've tried my best to search around for this question, but i can't seem to figure out exactly how to phrase it. I was hoping someone here could help me out!

Categories