"Executing a test in testNG based on condition" - selenium

i have a testNG xml which will execute a (one)test case 2 times. the difference between each time is the test level parameter. so i have to execute the test case with 2 different parameter (2 user).
Now i will be creating new xml (suite of xml) from which i will call the existing xml. i will define a new parameter in suite xml for the User.
Expectation is if suite xml parameter="user2", then the child xml should execute the test case only once which has parameter as user2.
i tried beanshell scripting and found it useful for method-selector. but i want to make decision for test level and not method level.
Below is the testNG.xml which calls the test case 2 times with different user value. TestCase will be called first time with User="USER1" and second time with User="USER2".
<?xml version="1.0"?>
<suite name="TestLoad">
<test verbose="10" name="TestForUser1" preserve-order="true">
<parameter name="User" value="USER1"/>
<classes>
<class name="com.dummy.test.TestCase"/>
</classes>
</test>
<test verbose="10" name="TestForUser2" preserve-order="true">
<parameter name="User" value="USER2"/>
<classes>
<class name="com.dummy.test.TestCase"/>
</classes>
</test>
</suite>
Below is the Suite of xml which i will be newly creating to call many testng.xml described as above.
<?xml version="1.0"?>
<suite name="suiteOfXml">
<parameter name="User" value="USER1"/>
<suite-files>
<suite-file path="TestLoad.xml"/>
<suite-file path="TestStage.xml"/>
</suite-files>
</suite>
Expectation is something like:
if the suiteOfXml has User="USER1" then each testNG xml should run the TestCase only once with User=USER1.
if the suiteOfXml has User="USER2" then each testNG xml should run the TestCase only once with User=USER2.
if the suiteOfXml has User="ALL" then each testNG xml should run the TestCase twice. once with User=USER1 and next time with User=USER2.
I cannot make any changes to the TestCase (java class level). condition should be made at xml only.
Kindly provide a possible solution. Thanks in advance

Here's what you need to be doing to get past this issue (You dont need a suite of suites etc., )
Create an implementation of the TestNG listener interface org.testng.IAlterSuiteListener within which you basically read the value of the parameter and then based on it, you decide which <test> should be created and how it would look like etc.,
You create a suite xml file that only contains the parameter which would help you decide what <test> should be there and also a reference to the newly created listener from the previous step.
That should do. Using this approach you can basically do all the things that you have listed out.
Here's how a sample listener would look like (In this example, we are getting the packages to be executed via a JVM argument and then based on that, we are building our <test> tag)
package com.rationaleemotions.wordpress;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.Collections;
import java.util.List;
public class SimpleSuiteAlterer implements IAlterSuiteListener {
#Override
public void alter(List suites) {
XmlSuite suite = suites.get(0);
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName("CommandLine_Test");
String packages = System.getProperty("package", suite.getParameter("package"));
XmlPackage xmlPackage = new XmlPackage(packages);
xmlTest.setXmlPackages(Collections.singletonList(xmlPackage));
}
}
And here's how the suite xml would look like
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package-Suites" verbose="2">
<parameter name="package" value="com.rationaleemotions"/>
<listeners>
<listener class-name="com.rationaleemotions.wordpress.SimpleSuiteAlterer"/>
</listeners>
</suite>
For a more detailed explanation and some code samples, refer to my blog post https://rationaleemotions.com/building_dynamic_testng_suites/

Related

TestNG - Implementation of sequential and parallel test scripts

I was creating a demo framework for Open MRS Application using POM design. The credentials for the Application are "admin/Admin123"
I created the following 3 Classes:
1) Login tests: Which contains all the Login (1 +ve and 2 -ve scenarios) and Logout test (Total 4 #Tests)
2) Register Patient test: Which will register a patient and also verify if the patient has been registered successfully (Total 3 +ve #Tests)
3) Capture Vitals test: Which will Capture Vitals of the patient registered above and verify if the details captured are correct. (Total 2 +ve #Tests)
I'm planning to add a few more Classes which will contain tests related to Adding Visits, adding Allergies etc.
There is a dependency between the classes i.e. only execute the Register patient test if the #Test for Login passes. Similarly, execute the remaining tests like capture vitals, adding visits, adding allergies only if the Register patient test passes. However, there is no dependency between the capture vitals, adding visits and adding allergies and can run in parallel.
This is how it looks from a top-level:
class Login
#Test T_001_LoginTest
#Test T_002_LogoutTest
class RegisterPatient
#Test T_003_RegisterNewPatient
#Test T_004_ConfirmPatientRegisteredUsingPatientID (dependent on T_003)
#Test T_005_ConfirmPatientRegisteredUsingPatientName (dependent on T_003)
class CaptureVitalsTest
#Test T_006_CreateAVisitAndCaptureVitals (dependent on T_001 and T_003)
#Test T_007_VerifyVitals (dependent on T_006)
What I have done so far is added defined group "login" for class Login and group "registerpatient" for class Register. For class RegisterPatient, I've added dependsOnGroup "login". Similarly, for class CaptureVitals, I've added dependsOnGroups for both "login" and "registerpatient". I've also set alwaysRun = true for the #Test Methods which have "dependsOnMethods" dependency.
Below is my testng.xml file
<suite name="Open MRS Automation" parallel="tests">
<listeners>
<listener class-name="com.utils.CustomListener" />
</listeners>
<test thread-count="5" name="Login Test">
<classes>
<class name="com.testcases.LoginTest" />
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Register Patient Tests">
<classes>
<class name="com.testcases.RegisterPatientTest" />
</classes>
</test>
<test thread-count="5" name="Capture Vitals Tests">
<classes>
<class name="com.testcases.CaptureVitalsTest" />
</classes>
</test>
</suite>
What would be the right approach in setting initial sequential and then parallel execution of the tests? Any help would be appreciated! Thanks.
You should give Testcases method name by numbers.
you must be using excel sheet for data provider format that will help you run test cases according to Testcases number

Running specific test class inside a package first in TestNG

I want to run a specific test class before running other tests in the same package using testng xml.
test_package
-------------
testclass1
testclass2
testclass3
I want testclass3 to run first.
Selenium grid is being used. Currently the longer test runs at last and other nodes are idle making the test duration longer. How can i give first priority to longer test classes.
<test name="Tests">
<packages>
<package name="tests.*"/>
</packages>
</test>-->
There are multiple classes inside the package but i want selected test class to run ahead of all.
If you want to run the classes in order, you can specify the order in the xml as given below, By default testng, run the classes in order mentioned in xml.
<test name="Tests" preserve-order="true">
<classes>
<class name="tests.testclass3"/>
<class name="tests.testclass1"/>
<class name="tests.testclass2" />
</classes>
</test>
If you include
<package name="tests.*"/>
Then looks there is no way to say order of classes to execute.
You can try by providing classes with preserve order is testng.xml file or you can create xml file by coding like here.

TestNG test are not getting executed as per the priority

Below is my project structure in eclipse :
->testclasses
---->AccountTest(Priority of Methods from 1-6)
---->BillingTest(Priority of Methods from 7-13)
---->HomePageTest(Priority of the only method is 17)
---->SupportTest (Priority of Methods from 14-16)
All the test classes above have methods where priority is set in incremental order as shown above.
Now when I right click on testclasses package and run it as Testng. It starts the execution with HomePageTest.
I am setting the priority of my test methods as below :
#Test(priority=6, dataProvider="Setup")
I want the execution to be as per the priority defined for each method and Thus method with priority 1 should execute first irrespective of which class it is in.
The correct way to run all the tests present in multiple classes with priorities is to run those by a testng file. So write all the classes name in the testng.xml file and then run the testng file by right clicking on it from the package explorer--> Run As-->TestNG Suite. Your test cases will run according to the priorities set irrespective of the classes they belong to.
You testng.xml should look like:
<test name="TestSuiteName">
<classes>
//Insert the whole path of the classes here like
<class name="packageName.AccountTest" />
<class name="packageName.BillingTest" />
<class name="packageName.HomePageTest" />
<class name="packageName.SupportTest" />
</classes>
</test>
TestNG runs test cases in order of the priorities. If there is no priority for a testmethod then by default TestNG sets the priority to 0. In your case, in HomePageTest class there may be some method with priority 0. Either set priority of all the methods or use test methods in xml runner file to run in a given order
<test name="DummyTest">
<classes>
<class name="apitestset.inventory.Test">
<methods>
<include name="create"/>
<include name="update"/>
<include name="get"/>
<include name="check"/>
<include name="initiate"/>
<include name="confirm"/>
<include name="extend"/>
</methods>
</class>
<classes>
Here testclass has 7 methods and they run in the order given in xml runner file. Don't set the priority if you are using methods in xml runner file.

how to run test cases parallely using selenium + testng

I wanted to run test cases parallely with different parameters . My testng.xml would look like this
<suite name="Automation Testing" preserve-order="true">
<parameter name="browser" value="#BROWSER#"/>
<test name="Login">
<classes>
<class name="common.Login" > </class>
</classes>
</test>
</suite>
Java Class which accept parameters
#parameters({'browser'})
#beforesuite
public void login(String browser){
if(broswer.equals('ff')){
WebDriver driver = new FirefoxDriver()
}
}
I have gone through this link https://stackoverflow.com/questions/14625256/how-to-run-the-test-in-parallel-using-selenium-webdrivertestng ... But i have not anything out it ... please let me know how i can do it .....
If you want the same testcase to be run on different browsers, there are different ways.
Define multiple <test> in our xml file and have individual parameters. You would need to have a new name for each #BROWSER# i.e. #BROWSER1, #BROWSER2. Then run with parallel = tests which would trigger the same testcases on different browsers. However, the issue with this approach is you will have to always supply fixed number of browses to your ant xml
Another approach can be, to supply a comma separated list to your #BROWSER# parameter. Use a dataprovider to do a split and create driver instances. Set parallel= methods.
Set parallel= methods. Control browser based paralle runs from outside i.e Trigger various builds if you are using something like Jenkins with different parameter values for #BROWSER#

How to run two #test annotations parallel in two machines?

I have two #Test annotations and i want to run them in parallel one in machine1 and one in machine2 using TestNG.
For that i am using a testing.xml file which contains the below code
<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestSuite" verbose="3">
<test name="DriverScript" parallel="methods" thread-count="2">
<classes>
<class name="testscripts.DriverScript"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
When i am running in two different machines it is not running in parallel..it is running one by one...is there anything that i missed here?
Note: DriverScript is the class name where i have two test annotations which needs to be run in parallel...
Thanks,
You could swap this to threading and kick off both at the same time.
c# example:
Parallel.Invoke(
() => { //Test call goes here with parameters. },
() => { //Test call goes here with parameters. }
);
Of course this would be custom code and not relying on the TestNG framework specifically, but it would work and provide you control.