我试图在不同的浏览器下运行我的黄瓜测试,所以我有test.xml
(是的,目前只有一个浏览器)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SelfCare" verbose="1" thread-count="1" parallel="tests" configfailurepolicy="continue">
<parameter name="environment" value="TEST" />
<test name="Chrome">
<parameter name="browser" value="chrome" />
<parameter name="width" value="1024" />
<parameter name="height" value="768" />
<classes>
<class name="TestNGRunner">
<methods>
<include name="scenario"/>
</methods>
</class>
</classes>
</test>
</suite>
而这个TestNG跑步者类,我能够从这个跑步者类中访问参数browser
,但是我需要将这个变量传递给Cucumber,在那里根据传递的变量创建浏览器实例。到目前为止,我使用它没有TestNG,我通过环境变量传递这些变量,但我认为如果我在不同的浏览器上并行运行测试,这将无法工作。
import cucumber.api.CucumberOptions;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.PickleEventWrapper;
import cucumber.api.testng.TestNGCucumberRunner;
import org.testng.annotations.*;
@CucumberOptions(
plugin = {"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"junit:target/cucumber-results.xml"},
strict = true,
extraGlue = {"src/test/resources/Hooks.java"},
features = {"src/test/resources/Header.feature"}
)
public class TestNGRunner {
private TestNGCucumberRunner testNGCucumberRunner;
private String browser;
@BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
@Parameters("browser")
@BeforeTest
public void beforeSuite(String browser) {
System.out.println("Browser for this test set is " + browser);
this.browser = browser;
}
@Test(groups = "Cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper cucumberFeature) throws Throwable {
testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}
@DataProvider
public Object[][] scenarios() {
return testNGCucumberRunner.provideScenarios();
}
@AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
testNGCucumberRunner.finish();
}
}
任何想法如何实现?
显然,我可以使用qazxsw poi从任何地方访问参数
步骤1. Runner Class应扩展AbstractTestNGCucumberTests并实现@Parameters和@BeforeTest注释。
Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("browser")
注意 - 我在BaseSteps Class下有浏览器实现方法。您可以根据需要实施。
步骤2.下面是我们如何在TestNG.xml中定义参数名称和值
package com.jacksparrow.automation.suite.runner;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import com.jacksparrow.automation.steps_definitions.functional.BaseSteps;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(features = "classpath:features/functional/",
glue = {"com.jacksparrow.automation.steps_definitions.functional" },
plugin = { "pretty","json:target/cucumber-json/cucumber.json",
"junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
tags = { "@BAMS_Submitted_State_Guest_User" },
junit ={ "--step-notifications"},
strict = false,
dryRun = false,
monochrome = true)
public class RunCukeTest extends AbstractTestNGCucumberTests {
@Parameters({ "browser" })
@BeforeTest
public static void setUpScenario(String browser) {
BaseSteps.getInstance().getBrowserInstantiation(browser);
}
}