[英]How to pass Scenario name from the Cucumber feature file as a parameter in the Cucumber steps?
[英]TestNG & Cucumber: Pass in single feature file name as parameter
我想要一個testNG / CucumberRunner和一個testng @Test,它允許我將單個功能文件的名稱作為testng參數(使用@Parameter)傳遞並運行它。 我想要一個運行時解決方案。
我已經使用TestNG框架編寫了許多非Cucumber測試,我也想在其中使用Cucumber代碼。
有誰想出一些聰明的東西?
弄清楚了如何將包含要素文件名的自定義黃瓜選項發送到跑步者類。 這將允許從testng.xml運行黃瓜測試和非黃瓜測試。
下面的文字基於“ Cucumber for Java”一書中的詳細信息。
黃瓜檢查@CucumberOptions注釋是否提供了任何選項替代。 從上到下檢查以下內容,找到任何一個后停止:
在override中找到的值將替換除plugin參數之外的所有設置值。 插件參數將被追加。 未覆蓋的參數將不受影響。
testng.xml
<suite name="Default suite">
<test name="Cucumber Mix">
<classes>
<class name="cucumtestng.test.RunAbstractSampleTest"></class>
<class name="cucumtestng.test.NormalTest"></class>
</classes>
</test>
</suite>
@CucumberOptions(features="",glue="cucumtestng.test.stepdefs",snippets=SnippetType.CAMELCASE,
plugin={"pretty", "html:report", "json:reports.json"})
public class RunAbstractSampleTest extends AbstractTestNGCucumberTests {
}
public class NormalTest {
@Test
public void f() {
System.out.println("NORMAL TESTNG CLASS");
}
}
您還可以使用不擴展AbstractTestNgCucumberTests而是使用composition的testng黃瓜類。
此“設置”代碼可以解決問題。 它為我提供了我感興趣的黃瓜功能。 我也會看看其他建議。
@Parameters({"featurename"})
@BeforeTest(alwaysRun = true)
public void setUpTest(String featureName) throws Exception {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
List<CucumberFeature> featureList = testNGCucumberRunner.getFeatures();
for (CucumberFeature feature : featureList){
if (featureName.equalsIgnoreCase(feature.getPath())){
runFeature = feature;
break;
}
}
}
另一種技術是在實例化運行器之前,使用反射來實時修改CucumberOptions批注(這要歸功於一些較早的帖子):
@Parameters({"featurePath"})
@BeforeTest(alwaysRun = true)
public void setUpTest(@Optional("src/main/java/cucumberFeatureFiles/Testcase.feature") String featurePath) throws Exception {
Class<?> testClass = this.getClass();
changeCucumberAnnotation(testClass, "features", new String [] {featurePath});
testNGCucumberRunner = new WaltersTestngCucumberRunner(testClass);
}
private static void changeCucumberAnnotation(Class<?> clazz, String key, Object newValue) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Annotation options = clazz.getAnnotation(CucumberOptions.class); //get the CucumberOptions annotation
InvocationHandler proxyHandler = Proxy.getInvocationHandler(options); //setup handler so we can update Annotation using reflection. Basically creates a proxy for the Cucumber Options class
Field f = proxyHandler.getClass().getDeclaredField("memberValues"); //the annotaton key/values are stored in the memberValues field
f.setAccessible(true); //suppress any access issues when looking at f
Map<String, Object> memberValues = (Map<String, Object>) f.get(proxyHandler); //get the key-value map for the proxy
memberValues.remove(key); //renove the key entry...don't worry, we'll add it back
memberValues.put(key,newValue); //add the new key-value pair. The annotation is now updated.
}//end method
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.