how to run test cases parallely using selenium + testng - selenium

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#

Related

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.

"Executing a test in testNG based on condition"

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/

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.

How to run selective tests from testng.xml

I have testng.xml in which I have large number of <test> tag. I want to execute some of them not all and I don't want to comment the rest
e.g In my testng.xml, there are three tests
<test name="testA">
</test>
<test name="testB">
</test>
<test name="testC">
</test>
I want to execute testA and testC only and i don't want to comment the testB
Any suggestion..?
Add a command line switch of: -testnames=testA,testC
I don't think that can be done. If there are specific tests you do not want to run then you can check out Iannotationtransformer to set the enabled value = false to set specific tests not to run.

How to ensure that the order specified in TestNG.xml is retained?

On using TestNG+Selenium , I'm not able to ensure the order of execution of classes.The order specified below (in testng.xml) is not working ->ClassTwo executes first and then ClassOne is executed.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ABC" parallel="">
<test verbose="2" name="xyz" annotations="JDK" preserve-order="true">
<classes>
<class name="script.ClassOne"/>
<class name="script.ClassTwo"/>
</classes>
</test>
</suite>
How can I ensure that the order specified in TestNG.xml is retained?
You just have to set parallel value to none
<suite name="ABC" parallel="none">
it works for me !
According to TestNG documentation:
By default, TestNG will run your tests in the order they are found in
the XML file. If you want the classes and methods listed in this file
to be run in an unpredictable order, set the preserve-order attribute
to false
I would suggest leaving the preserve-order attribute out, since it is set by default.
However, you have two other options to force specific order to the test methods/classes:
Invoke tests programmatically.
Implement method interceptor, that will order the list of the tests.
Did you try #Test( dependsOnGroups= { "dummyGroupToMakeTestNGTreatThisAsDependentClass" } ) in the class?
See this thread:
TestNG & Selenium: Separate tests into "groups", run ordered inside each group
Hope this helps!
....Quite a bit after the event but I had the same problem and found myself here.
In the end it was because the individual tests had been marked with a priority in the #Test annotation, so in my case but your example script.ClassTwo had a higher priority than script.ClassOne
In TestNG, the order of execution is based on alphabetical order so we could use a TestNG attribute Priority and there we could mention which class->methods you want to execute first.This is Priority annotation attribute you could give in the #Test annotation.
example: #Test(Priority=-1)
Lesser the number value the first it will execute.