java - 当我在步骤 def 方法中使用 Cucumber(场景场景)参数时,显示使用 1 个参数声明的错误。小 cucumber 步骤有 0 个参数

标签 java selenium-webdriver automated-tests cucumber cucumber-jvm

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

我写的以下功能步骤:

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

pom.xml 文件:

<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>

项目结构和功能文件

Project Structure and Feature File

最佳答案

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

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

请注意,它仅表示 Before 和 After Hook - 而不是步骤定义 @And、@Given、@When、@Then。它们需要行表达式中指定的参数。

关于java - 当我在步骤 def 方法中使用 Cucumber(场景场景)参数时,显示使用 1 个参数声明的错误。小 cucumber 步骤有 0 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54876696/

相关文章:

java - Android:Textview 不显示以编程方式设置的值

java - Selenium webdriver 不支持的主次版本 51.0 - Java 版本问题

javascript - 是否可以进一步压缩 Base64 PNG 字符串?

testing - SPA 中的 Jmeter 负载测试

java - Spring Boot 开发者工具 IntelliJ

java - Java 中有 Perl 实现吗?

java - 异常 : The path to the driver executable must be set by the webdriver. chrome.driver 系统属性;使用远程 WebDriver

user-interface - Maveryx、FitNesse、Robot 在 Mac、Windows 和 Linux 上进行桌面 GUI 测试

testing - 在 Selenium IDE 中,如何获取基本 url 的值

java - useDelimiter 无法识别竖线