Is there anyway to run cucumber scenarios using the TestNG runner by right clicking individual scenarios in the feature files rather than using the command file or running the TestNG runner file directly?
I'm using Intellij to run cucumber scenarios in a maven testing framework. In the POM.xml file I have the Surefire plugin referencing the testNG.xml file which points to the TestNG runner class.
When I run "mvn test" from terminal it calls the TestNG testrunner,but when I right click on the scenario in the feature file to select run, it runs the io.cucumber.core.cli.main class that calls the io.cucumber.core.runner.Runner class testrunner.
I can't edit Run/Debug configuration to use the TestNG runner because the TestNG runner doesn't have a Main method.
you can create a runner class and add tags you need into CucumberOptions.
#CucumberOptions(plugin = {"pretty"}, strict = true, tags = {"#yourTag"})
public class RunTestNGTest extends AbstractTestNGCucumberTests {
}
If anybone runs into the same issue, I managed to get around the issue by creating another class with a main method and calling the mvn command to run the test from there. something like this:
String mvnCommand = "mvn test ";
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", mvnCommand + args);
processBuilder.start();
Not sure if there's a better solution, but I can't seem to find any other option with Cucumber for Java plugin.
Related
I tried to run only one test class from Gradle. I used these commands:
gradle -Dtest.single=Junit5Test test
gradle test --tests ru.tests.runner.Junit5Test
But it runs all of the project's tests. I need to run only one test class. Here is my class:
public class Junit5Test {
public static void main(String[] args) {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectPackage("ru.tests.api")
)
.build();
Launcher launcher = LauncherFactory.create();
launcher.execute(request, new TestExecutionListener());
}
}
It works perfect by right-click run. I use
IntellijIDEA 2017.1.5,
Gradle,
junitPlatformVersion = '1.0.0-M6',
junitJupiterVersion = '5.0.0-M6'
For starters, your Junit5Test class is not a "test class".
Rather, it is a stand-alone application (i.e., a Java class with a main() method).
Thus, attempting to execute it as a "test" with Gradle will never work.
If you would like to run JUnit Jupiter tests (or any other tests that run on the JUnit Platform) with Gradle, you should use the JUnit Platform Gradle Plugin (that's a link to the appropriate section of the JUnit 5 User Guide).
I resolved it by replacing
gradle -Dtest.single=Junit5Test test
with
gradle -DjunitPlatformTest.single=Junit5Test test
How does TestNG work when it is looking for its test classes?
I created a separate project from my TestNG project using JavaFX to create a GUI, when the GUI is run it enables you to select an XML file and then the path to the testNG XML is saved in a variable.
When I run the following code:
String xmlFileName = selectedFile.getAbsolutePath();
TestNG testng = new TestNG();
List<String> testFilesList = new ArrayList<String>();
testFilesList.add(xmlFileName); //test suite resides in the working directory's root folder
testng.setTestSuites(testFilesList); //you can addd multiple suites either here by adding multiple files or include all suites needed in the testng.xml file
testng.setUseDefaultListeners(false);
testng.addListener(htmlRep);
testng.run();
I get an error saying that it cannot find my test classes inside the XML.
[TestNG] [ERROR]
Cannot find class in classpath: com.emc.qe.u360.tests.LogoutTests
I then decided to make a java class within my TestNG project, and copy the code over and it still gives the same error message.
Where is TestNG looking for the test classes when the above code is run?
What difference is the code above from manually running the XML from eclipse itself? The code seems to be working, as from what I can see if the code for running the XML was broken or not working, it wouldn't be able to give me that error as it wouldn't be able to determine what is specified in the XML.
Any help would be appreciated, thank you.
EDIT: Just for background information, currently I am using Jenkins to run the tests, what I'm trying to do with the GUI is to create an alternative solution to running the tests outside of the Project/Framework, that can be used universally, a user can just select their XML and then the tests will be executed.
It's configurable if you use the Maven Surefire or the Gradle TestNG runner, but by default I think TestNG looks in src/test/java for your class names. Additionally, I think if your running TestNG from code, as a standalone run, you might be able to adjust the path-base where it looks for classes.
I was trying to automate an Eclipse-based standalone application. I recorded a script using Silk4J and I am also able to run the script separately.
Under the same package I have created one more .java file which invokes the script. But I am not able to run the .java file as "Silk4j Test". The option is not available.
What changes should I make in order to run the .java file which will eventually run the script?
A Silk4J test is actually a JUnit test. Until today I could not find a real difference running a test as Silk4J test or as JUnit test.
If the option "Run as Silk4J test" and "Run as JUnit test" are not available, the most likely reason is that your method is not marked as a JUnit test.
You need the #Test annotation:
import org.junit.Test;
[...]
#Test
public void test()
{
protected Desktop desktop = new Desktop();
BaseState baseState = new BaseState();
baseState.execute(desktop);
[...]
}
I have a file with file extension .feature. HOw do run this from the command line?
In order to make a batch file for each feature. I am using Cucumber-JVM with Java and Selenium.
Cucumber-JVM is based on JUnit so its just like running any unit tests from the command line
java -cp /path/to/junit.jar org.junit.runner.JUnitCore [test class name]
where test class name is annotated with #CucumberOptions whose features
refer to the feature file. If youre using Maven you could use
mvn test
If you are using Maven, you can run your .feature file this way:
mvn -Dcucumber.options="from/the/root/of/module/to/the/feature/MyFeature.feature" test
This will override the #CucumberOptions in the test runner class.
You can run any test runner or override default test runner by using the command line below. This means you only want to run the test named SmokeTestRunner. By the way, you are free to set surefire plugin in your pom.xml any way. For example, you can set up your surefire plugin in your pom.xml to run the regression test named RegressionTestRunner. It doesn't matter.
mvn -Dtest=**/SmokeTestRunner.java test
Your SmokeTestRunner java file should look like this. ->
#CucumberOptions(
features = "src/test/resources/features/Smoke.feature",
glue = {"stepdefinitions", "hooks"},
tags = "#chrome", // It runs only scenarios tagged with "#chrome"
plugin = {"pretty"/*you can add more cucumber plugins here if you want.*/},
monochrome = true)
public class SmokeTestRunner extends AbstractTestNGCucumberTests {}
For more details, have a look at the maven surefire plugin docs. (https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html)
Can some one tell me how can i run the java file which was exported from selenium IDE through command prompt.
I have used the following command: "java -jar selenium-server.jar -htmlSuite "*firefox" "http://www.google.com" "C:\mytestsuite\mytestsuite.html" "C:\mytestsuite\results.html"
Able to launch selenium functional test runner but nothing is executed there.
The converted tests are JUnit tests. So you should have two processes:
Your selenium server process:
java -jar lib/selenium-server-standalone-2.28.0.jar
Your JUnit test runner
"java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] "
If you have several test classes, it might be better to create a TestSuite with the Suite annotation:
#RunWith(Suite.class)
#SuiteClasses({
MyTestClass1.class,
MyTestClass2.class})
public class TestSuite {
...
If you are using Spring, you can setup config containing selenium server address, browser, ...
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:my/package/seleniumConfig.xml"})
public abstract class SeleniumTestSuite {
The java test cases exported don't compile because they require at least the Selenium library to compile and may need junit or TestNG as well to actually run. I really suggest that you do this from within eclipse.
You can get eclipse 32 or 64 bit here. Then create a new Java project by going File-> New -> Java Project. You can get the Selenium client zip here. You need to get the client jar (called selenium-java-2.31.0.jar) out of this zip and put it in the lib directory of your new Java project in eclipse. You may have to create the lib directory and then right-click the jar file in the lib directory and "add to build path".
Put the java code that was generated by the Selenium IDE into the src directory of the new Java project in eclipse. (you may need to create the appropriate packages under src etc). Then right-click the test case java file that you want and select "Run As ... Junit". This should run it for you. If you get compile or run errors you can update your question above.
This is the solution:
Test is your Class mande by selenium
import org.junit.runner.JUnitCore;
import com.example.tests;
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}