无法在Cucumber& Testng

时间:2018-03-16 11:33:44

标签: java cucumber testng parallel-testing

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

获得以下异常,

  

cucumber.runtime.CucumberException:当一个钩子声明一个参数时   它必须是cucumber.api.Scenario类型。公共无效   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"));
    }

1 个答案:

答案 0 :(得分: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) {
相关问题