当我在步骤def方法中使用Cucumber(方案方案)参数时,显示的错误声明了1个参数.小黄瓜步骤有0个参数 [英] When I use Cucumber (Scenario scenario) parameter in step def method,error displayed declared with 1 parameters. the gherkin step has 0 arguments

查看:86
本文介绍了当我在步骤def方法中使用Cucumber(方案方案)参数时,显示的错误声明了1个参数.小黄瓜步骤有0个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下功能步骤,以在失败的情况后进行屏幕截图并关闭浏览器.当我使用场景方案"参数并运行它时,在步骤Defs"方法上,但它会生成一条错误消息.

I wrote the following feature step to take screen shot after failed scenario and close the browser. On the Step Defs method when I use (Scenario scenario) parameter and Run it But it generates an error message.

遵循我编写的功能步骤

 And I close the browser

以下是我在步骤定义"文件中为该步骤编写的代码:

Following is the code I have written in Step Definition file for that step:

@And("^I close the broswer$")
public void i_Close_The_Broswer(Scenario scenario  ) throws Exception {
        if (scenario.isFailed()) {
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png");

    }

运行后显示错误消息:

 cucumber.runtime.CucumberException: Arity mismatch: Step Definition 
'stepDefinations.LoginPageStepDefs.i_Close_The_Broswer(Scenario) in 
 file:/C:/Users/thaider/eclipse-workspace/AcWs_Automation/target/test- 
 classes/' with pattern [^I close the broswer$] is declared with 1 
 parameters. However, the gherkin step has 0 arguments [].

测试基类:

package utilities;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;

public class TestBase {

public static WebDriver driver;
public static Properties prop;


public TestBase() {
    try {
        prop = new Properties();
        FileInputStream fis = new FileInputStream(
                "C:/Users/thaider/eclipse-workspace/AcWs_Automation/src/test/java/config/config.properties");
        prop.load(fis);

    } catch (IOException e) {
        e.getMessage();
    }


}


public static void initialization() {
    String browserName = prop.getProperty("browser");


if(browserName.equals("chrome")){
    //System.setProperty("webdriver.chrome.driver", "src/main/resources/Driver/chromedriver.exe");

    System.setProperty("webdriver.chrome.driver", prop.getProperty("webdriver.chrome.driver"));
   // System.setProperty("log4j.configurationFile", prop.getProperty("log4j2ConfigFile"));
    driver = new ChromeDriver();
}

driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));


}
public static void closeSession() {
driver.quit();
}
}

登录页面步骤定义文件:

Login Page Step Definition File:

package stepDefinations;

import java.awt.Window;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriverException;
import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageObject_OR.AcceptContinue;
import pageObject_OR.LoginPage;
import utilities.TestBase;
import utilities.WindowsHandle;
import org.openqa.selenium.WebElement;

public class LoginPageStepDefs extends TestBase {
AcceptContinue acceptPage;
LoginPage loginPage;
WindowsHandle windowHandle;

@Given("^I want to open a browser$")
public void i_want_to_open_a_browser() throws Exception{
TestBase.initialization();



}



@Then("^I click on Accept & Continue$")
public void i_click_on_Accept_Continue() throws Exception{
    acceptPage = new AcceptContinue();
    acceptPage.clickAcceptContinue();
}


@Then("^I validate seal logo is displayed$")
public void i_validate_seal_logo_is_displayed() throws Exception {
    loginPage = new LoginPage();
    Thread.sleep(3000);
    loginPage.validateCrmImage();


}




@Then("^I am in the ACWS login page as a CS User$")
public void i_am_in_the_ACWS_login_page_as_a_CS_User() throws Exception {
// Write code here that turns the phrase above into concrete actions
loginPage = new LoginPage();
loginPage.loginToHomePage("Testcs", "test123");
}



@And("^I close the broswer$")
public void i_Close_The_Broswer(Scenario scenario  ) throws Exception {
if (scenario.isFailed()) {
byte[] screenshot = ((TakesScreenshot) 
driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");

}


TestBase.closeSession();
}

pom.xml文件:

pom.xml File:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AcWs_Automation</groupId>
<artifactId>AcWs_Automation</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- REPORTING DEPENDENCIES ARE BELOW -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <testFailureIgnore>true </testFailureIgnore>
            </configuration>
        </plugin>

        <plugin>
            <groupId>net.masterthought</groupId>
            <artifactId>maven-cucumber-reporting</artifactId>
            <version>3.15.0</version>
            <executions>
                <execution>
                    <id>execution</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <projectName>ExecuteAutomation</projectName>
                        <outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory>
                        <cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>



<!-- Project Selenium, Junit, Apache POI Dependecy Below -->

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version> 3.12.0</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.directory.studio</groupId>
        <artifactId>org.apache.commons.io</artifactId>
        <version>2.4</version>
    </dependency>





    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
    </dependency>




    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.16-beta2</version>
    </dependency>


    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>openxml4j</artifactId>
        <version>1.0-beta</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core 
        <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> 
        <version>2.11.2</version> </dependency> -->



    <!-- BELOW DEPENDECIES HAS BEEN COMMENTED OUT FOR DUPLIATION -->
    <!-- <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> 
        <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> 
        <artifactId>poi-ooxml-schemas</artifactId> <version>3.9</version> </dependency> 
        <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> 
        <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> 
        <artifactId>ooxml-schemas</artifactId> <version>1.1</version> </dependency> 
        <dependency> <groupId>org.apache.poi</groupId> <artifactId>openxml4j</artifactId> 
        <version>1.0-beta</version> </dependency> -->

</dependencies>

项目结构和功能文件

推荐答案

来自针对该场景的Cucumber API(

From the Cucumber API for Scenario (https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/api/Scenario.java) :

在声明此类型参数的Hook之前或之后,将收到此类的实例

Before or After Hooks that declare a parameter of this type will receive an instance of this class

请注意,它仅显示前"和后"钩子-而不是步骤"定义@ And,@ Given,@ When,@ Then.那些只希望 在行表达式中指定的参数.

Note that it says only the Before and After hooks - not the Step definitions @And, @Given, @When, @Then. Those expect only the arguments specified in the line expression.

这篇关于当我在步骤def方法中使用Cucumber(方案方案)参数时,显示的错误声明了1个参数.小黄瓜步骤有0个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆