0

我正在尝试使用 Testng 和 cucumber 在 2 个浏览器中运行并行测试。

得到以下异常,

cucumber.runtime.CucumberException:当一个钩子声明一个参数时,它必须是 cucumber.api.Scenario 类型。公共 void com.sample.data_republic.sample_ebay.EbayTest.loadBrowser(java.lang.String) 在 cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:52) 在 cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java: 224)

下面给出的代码示例。

import cucumber.api.java.After;
import cucumber.api.java.Before;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Parameters;

public class EbayTest extends EbayPageObjects {

    public WebDriver driver;
    Properties propertyObj;

    @Before
    @Parameters("browser")
    public void loadBrowser(String browser) {
        // If the browser is Firefox, then do this
        if (browser.equalsIgnoreCase("firefox")) {
            System.setProperty("webdriver.gecko.driver", "src/test/resources/drivers/geckodriver");
            driver = new FirefoxDriver();
        } else if (browser.equalsIgnoreCase("chrome")) {
            System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver");
            driver = new ChromeDriver();
        }

        propertyObj = readPropertyFile();
        driver.get(propertyObj.getProperty("url"));
    }
4

1 回答 1

1

Before hook是一个黄瓜方法,而不是一个testNg方法,它只与Scenario对象一起注入。所以不能使用@Parameters它上面的注解来传递参数值。您需要在钩子之前使用,如下所示。

@Before
public void beforeScenario(Scenario scenario) {

或没有场景对象

 @Before
    public void beforeScenario() {

您可以将浏览器值存储在属性文件中并在挂钩之前访问它。或者在runner类的testNg的BeforeClass或BeforeMethod中实例化驱动,可以使用参数注解。

@BeforeClass
@Parameters("browser")
public void loadBrowser(String browser) {
于 2018-03-16T13:47:16.523 回答