当前位置: 首页 > 知识库问答 >
问题:

当我在step def方法中使用Cucumber(Scenary Scenario)参数时,用1个参数声明了错误显示。gherkin步骤有0个参数

庾鸿飞
2023-03-14

我编写了以下功能步骤,以便在失败的场景后进行屏幕快照并关闭浏览器。在Step Defs方法上,当我使用(scenema scenema)参数并运行它时,它会生成一条错误消息。

以下是我编写的功能步骤:

 And I close the browser

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

@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();
}
}
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();
}
<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>

共有1个答案

祖浩淼
2023-03-14

来自场景的Cucumber API(https://github.com/Cucumber/cucumber-jvm/blob/master/core/src/main/java/Cucumber/API/Scenario.java):

声明此类型参数的钩子之前或之后将接收此类的实例

请注意,它只说明了Before和After钩子--而不是步骤定义@and、@given、@when、@then。它们只需要行表达式中指定的参数。

 类似资料:
  • 我正在运行一个celery任务,它需要两个参数,如下所示: 和:

  • 我试图在Spring Data JPA中创建存储库方法,如: 但它给我错误: 表格: 我想用create(即创建日期)按排序顺序查找表中的所有数据。 Spring 数据查询参数将根据其位置进行替换。但这可能会出错,绑定可能容易出错。因此,建议在方法参数中使用@Param注释来绑定查询参数名称。在查询中,您需要使用:p aramName来指示相同的参数名称将与方法参数绑定

  • 问题内容: Java中的方法可以拥有的最大参数数量是什么?为什么? 我在64位Windows系统上使用Java 1.8。 关于StackOverflow的所有答案都表明,技术限制为255个参数,但未指定原因。 确切地说,静态方法为255,非静态方法为254(在这种情况下为第255)。 我认为这可以用某种规范来描述,并且只允许静态定义最大数量的参数。 但这仅对 所有4字节类型有效。我使用参数进行了一

  • Java中的一个方法可以拥有的最大参数数是多少?为什么? 我正在64位Windows系统上使用Java1.8。 准确地说,255用于静态,254用于非静态(在本例中将是第255个)方法。 我认为这可以在某种规范中描述,并且只需要静态定义允许的最大参数数。 但这只对和所有4字节类型有效。我用参数做了一些测试,在那种情况下我只能声明127个参数。 限制与方法的堆栈大小有关系吗? 注意:我真的不打算在任

  • 问题内容: 在Java / MariaDb中使用prepareStatement,如以下函数所示 我收到错误消息: java.sql.SQLException:参数索引超出范围(1>参数数量,为0)。 错误行号指向行 问题答案: 正如评论中提到的,您应该删除?中的引号。 另外,在中,应为正。从1开始循环计数。 总结一下:

  • 我试图在我的services.yaml中为ImageManager.php获取一个参数,但它不起作用,我无法解决此错误。 错误是这样的: 类型错误:参数太少,无法运行App\Manager\ImageManager::__construct(),0在C:\wamp64\www\SymfonyAPI\var\ache\dev\ContainerZxFSS5S\getImageManagerServi

  • 问题内容: 我正在尝试在同一控制器中管理2个动作,一切正常!唯一的是doRender方法。我正在尝试对两个操作使用相同的渲染方法,如下所示: 但是显然这是行不通的。服务器日志显示的内容如下: 有什么帮助吗? 问题答案: 唯一的方法是定义多个方法。