Last active
June 27, 2019 14:33
-
-
Save mig82/59058376833ecae171aa4bca62094774 to your computer and use it in GitHub Desktop.
How to run Cucumber TestNG tests in DeviceFarm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//From https://forums.aws.amazon.com/thread.jspa?messageID=798024#798024 | |
//Create a GenericRunner class as below: | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.DataProvider; | |
import org.testng.annotations.Test; | |
import cucumber.api.testng.CucumberFeatureWrapper; | |
import cucumber.api.testng.TestNGCucumberRunner; | |
import cucumber.runtime.model.CucumberFeature; | |
public class GenericRunner { | |
private TestNGCucumberRunner testNGCucumberRunner; | |
public GenericRunner(Class c) { | |
try { | |
setUpClass(c); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
private void setUpClass(Class c) throws Exception { | |
testNGCucumberRunner = new TestNGCucumberRunner(c); | |
} | |
public void feature() { | |
Object features = features(); | |
for (int i = 0; i < features.length; i++) { | |
for (int j = 0; j < features.length; j++) { | |
Object o = features[j]; | |
if (o instanceof CucumberFeatureWrapper) { | |
CucumberFeatureWrapper wrapper = (CucumberFeatureWrapper) o; | |
testNGCucumberRunner.runCucumber(wrapper.getCucumberFeature()); | |
} | |
} | |
} | |
try { | |
tearDownClass(); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
/** | |
* @return returns two dimensional array of {@link CucumberFeatureWrapper} objects. | |
*/ | |
private Object features() { | |
return testNGCucumberRunner.provideFeatures(); | |
} | |
private void tearDownClass() throws Exception { | |
testNGCucumberRunner.finish(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//And your TestNG Test Class: | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.DataProvider; | |
import org.testng.annotations.Test; | |
import cucumber.api.CucumberOptions; | |
import cucumber.api.testng.AbstractTestNGCucumberTests; | |
import cucumber.api.testng.CucumberFeatureWrapper; | |
import cucumber.api.testng.TestNGCucumberRunner; | |
@CucumberOptions( | |
features = "classpath:features/login.feature", | |
glue = "com.sample.glues", | |
plugin = { | |
"html:target/cucumber-html-report", | |
"json:target/cucumber-json-report.json" | |
} | |
) | |
public class LoginCucumberTest { | |
@Test(groups = "cucumber", description = "Runs Cucumber Feature") | |
public void feature() { | |
//testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature()); | |
GenericRunner runner = new GenericRunner(this.getClass()); | |
runner.feature(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment