When I run my testng tests using command line and with tests defined in a testng.xml file, they run in the order they are specified in the testng.xml
<suite name="WorkDepot Tests">
<test name="Submit work-result test">
<classes>
<class name="workdepot.test.SubmitWorkTest"/>
<class name="workdepot.test.WorkResultTest"/>
<class name="workdepot.test.SubmitWorkTest"/>
<class name="workdepot.test.WorkErrorTest"/>
<class name="workdepot.test.SubmitWorkTest"/>
<class name="workdepot.test.MultipleHasTest"/>
<class name="workdepot.test.WorkResultTest"/>
<class name="workdepot.test.CallbackTest"/>
<class name="workdepot.test.NegCallBackTest"/>
<class name="workdepot.test.NegSubmitWork"/>
<class name="workdepot.test.NegGetWork"/>
<class name="workdepot.test.NegHasWork"/>
<class name="workdepot.test.NegSubmitResult"/>
<class name="workdepot.test.NegSubmitError"/>
<class name="workdepot.test.NegReadResult"/>
<class name="workdepot.test.NegMultipleHas"/>
</classes>
</test>
</suite>
Now I want to run those using the gradle Test task type and I use include pattern for the same. But the order of execution is now changed (arbitrary).
task integTest(type: Test){
useTestNG()
testClassDir = file("build/classes/integTest")
include "workdepot/test/SubmitWorkTest*"
include "workdepot/test/WorkResultTest*"
include "workdepot/test/SubmitWorkTest*"
include "workdepot/test/WorkErrorTest*"
include "workdepot/test/SubmitWorkTest*"
include "workdepot/test/MultipleHasTest*"
include "workdepot/test/WorkResultTest*"
include "workdepot/test/CallbackTest*"
include "workdepot/test/NegCallBackTest*"
include "workdepot/test/NegSubmitWork*"
include "workdepot/test/NegGetWork*"
include "workdepot/test/NegHasWork*"
include "workdepot/test/NegSubmitResult*"
include "workdepot/test/NegSubmitError*"
include "workdepot/test/NegReadResult*"
include "workdepot/test/NegMultipleHas*"
}
How can I ensure the tests are run in the order as in the testng.xml without changing the test code (i.e., adding dependencies between tests)
It cannot be controlled from gradle level both for JUnit and TestNG. As you mentioned test sources can be modified. For JUnit a test suite can be defined to gain control over test execution order. If TestNG has it's equivalent of such test suite it may be the way to go.
By default, TestNG will run your tests in the order they are found in the XML file. Otherwise gradle test will not guarantee any order,
you can intercept the methods before they run but that is of no use in this case
Related
I am working on web whatsapp automation for practice using selenium-maven-cucumber-testng . I have below issue.
Expected: I have many feature files and I want to run all the feature files one after another as below :
take first feature file -> open browser ->run steps mentioned in feature file -> close browser. take second feature file -> open browser - > run steps mentioned in feature file -> close browser and so on.
Result : Currently all the feature files are getting run and passing. But the browser is opening only once i.e.,
Open browser -> run feature file 1, feature file2, etc -> Close browser.
I want to run as explained in "Expected".
My Testng :
testng.xml
My RunnerClass :
Runner.java
My Hook :
Hook.java
BeforeTest and AfterTest will run only once as there is only one test block in the testng.xml. One way would be to create separate runners for each feature file and have separate test blocks.
<test name="Feature_File_1">
<classes>
<class name="Runner1"/>
</classes>
</test>
<test name="Feature_File_2">
<classes>
<class name="Runner2"/>>
</classes>
</test>
Also in the current runner class remove junit annotation #RunWith.
You need to change your TestNG hooks to Cucumber hooks.
Why is that?
Cuz you want to close the browser after each scenario as you elaborated in your question.
Please see below:
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class Hooks {
#Before
public void beforeScenario(){
System.out.println("This will run before the Scenario");
}
#After
public void afterScenario(){
System.out.println("This will run after the Scenario");
}
}
I am running appium test using testng
want to pass app path to desired capabilities as parameter to testng.xml file
how can i do this from command line with maven ?
Lets say you have a suite xml file which looks like below
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="sample_suite" verbose="1" parallel="false" thread-count="2">
<test name="sample_test">
<parameter name="name" value="Krishnan"/>
<classes>
<class name="ParameterisedSampleTestClass" />
</classes>
</test>
</suite>
And you would like to change the value of the parameter name to a different value other than Krishnan (which is what is defined in the suite xml file)
You basically do this by passing the JVM argument -Dname=John.
TestNG by default supports changing parameter values and accepts values at run via JVM arguments.
You just need to use the same name as your parameter name, for the JVM argument.
You can find more details in my blog post here
You can achieve this by providing JVM argument as Krishnan mention in post bellow and nice blog in link:
mvn -Dbrowser="chrome" test
and gather them in your code (eg. java) via
String broswser = System.getProperty(browser);
and then turn into desired capabilities afterwards:
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapability.setBrowserName(browser);
I'm using the maven-failsafe plugin to trigger testng suites with configuration similar to
<suiteXmlFiles>
<file>src/test/resources/suites/somesuite.xml</file>
<file>src/test/resources/suites/anothersuite.xml</file>
<file>src/test/resources/suites/yetanothersuite.xml</file>
</suiteXmlFiles>
but the suites or the tests within them are not getting executed in the correct order. Is there a way to specifiy that the suites should be executed in the below order
somesuite.xml
anothersuite.xml
yet another suite.xml
I don't care about the order in which the tests within a suite is executed, but would like to execute one suite only after the previous one has completed. Is there some configuration which I could use to achieve the same?
Create a seperate testng.xml file and add something like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<suite-files>
<suite-file
path="path-to\suite1.xml" />
<suite-file
path="path-to\suite2.xml" />
</suite-files>
</suite>
And then add this testng.xml file to your maven suite
below is my problem.
i have RunTest(main class), which is generating testng xml file dynamically i.e. virtual testng.xml.
In my gradle.build file i have following test which uses the static testng.xml file, not the virtual one:
test {
useTestNG() {
// runlist to executed. path is relative to current folder
suites 'testng.xml'
}
}
To my knowledge, when running command gradle test it looks for testng.xml file and executes the test classes defined in the testng.xml file.
But i want to run the main class i. e. RunTest when i fire command gradle test, so that my main method dynamically (virtual one) generates testng.xml and runs tests instead of static testng.xml file.
can anyone please help me in calling and running the main class from build.gradle.
If you main method is actually executing the tests, just define a task called test of type JavaExec with override set to true and it will replace the pre-existing test task. If your main class just generates the dynamic testng.xml file, just define a task generateTestNgXML of type JavaExec that that generates your testng.xml file and make the test task depend on your task, then configure the test task to use your generated config file.
In testng.xml, I have included couple of test classes for grouping. Inside Test Classes I pass some parameters and group name:
#Parameters({"excelName", "excelTabName"})
#BeforeClass(alwaysRun=true)
public void setup(){/*Setup code here*/}
#Test(groups={"security"})
public void searchUser() {/*Test code here*/}
If I disable all the test class names apart from one which I would like to run, I use maven command like mvn -Dgroup=groupName test. It fires up the test class which is not commented in testng.xml
I would like to keep all those test classes enabled in testng.xml and still would like to run only one test when required. Any suggestions would be very much appreciated.
Looks like all you need, is to specify which methods to execute when running
<test name="MyTest">
<classes>
<class name="TestClass">
<methods>
<include name="searchUser" />
</methods>
</class>
</classes>
</test>
This will load only the test method searchUser() for execution.