【问题标题】:Java Cucumber Using Keys From Property File In Examples TableJava Cucumber 在示例表中使用属性文件中的键
【发布时间】:2019-01-11 08:47:18
【问题描述】:

我已经很久没有使用 Scenario Outlines 了。我想要实现的是从我的示例表中的 config_data.properties 文件中引用键,这样我就有了一次用于测试的内容/数据的真实来源。

我在需要从属性文件中获取数据的步骤中遇到错误,并且在我运行测试时输入到名字文本框中的值是 firstName1 而不是属性文件中的值。有问题的错误:

[31morg.openqa.selenium.WebDriverException: unknown error: keys should be a 
string

这是我所拥有的:

功能文件:

@new_test
Scenario Outline: User fills out the Personal Info Form With Valid Data
And I enter a first name as "<first_name>"
And I enter a middle name as "<middle_name>"
And I enter a last name as "<last_name>"



Examples:

|first_name | middle_name | last_name | 
|firstName1 | middlename1 | lastname1 |

步骤定义:(我认为这就是问题所在)

public class PersonalInfoFormSteps {

    private PersonalInfoFormPage personalInfo;

    private DataReader data;

    @When("^I enter a first name as \"([^\"]*)\"$")
    public void i_enter_a_first_name_as(String first_name) throws Throwable {
    personalInfo.getFirstNameField().click();
    data.loadData().getProperty(first_name);
    personalInfo.getFirstNameField().sendKeys(first_name);

    }
}

DataReader.class(这适用于获取不涉及场景大纲的数据)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import net.serenitybdd.core.pages.PageObject;

public class DataReader extends PageObject {

WebDriverWait wait = null;
private WebDriver driver;

String result = "";
InputStream inputStream;
File file = new File(
        "file path to properties file goes here");

public DataReader(WebDriver driver) {
    super();
}

public Properties loadData() throws IOException {
    Properties prop = new Properties();
    FileInputStream fileInput = null;
    try {
        fileInput = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // load properties file
    try {
        prop.load(fileInput);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return prop;
}

}

属性文件数据:

firstName1: Mickey
middleName1: M
lastName1: Mouse

【问题讨论】:

    标签: java selenium automation cucumber properties-file


    【解决方案1】:

    哈哈,想通了。我更改了步骤定义中的代码:

    @And("^I enter a first name as \"([^\"]*)\"$")
    public void i_enter_a_first_name_as(String first_name) throws Throwable {
        personalInfo.getFirstNameField().click();
    
        personalInfo.getFirstNameField()
                .sendKeys(data.loadData().getProperty(first_name));
    
    }
    

    【讨论】:

      【解决方案2】:

      您的步骤定义错误, 替换这一行:

      @When("^I enter a first name as \"([^\"]*)\"$")
      

      这一行

      @And("^I enter a first name as \"([^\"]*)\"$")
      

      或者替换这一行:

      And I enter a first name as "<first_name>"
      

      这一行

      When I enter a first name as "<first_name>"
      

      【讨论】:

      • 这并没有解决问题,它不关心从属性文件中读取数据时的And或When。为了澄清我的问题,在示例表中,我想在示例表中的属性文件中传递一个键,而不是硬编码“John”作为名字。
      • Cucumber 确实关心 And,When 和 Then 如果没有匹配你的场景的方法,cucumber 会告诉你一个场景不存在
      • 这不是这里的问题,不,它并没有告诉我该场景不存在。正如你在上面看到的,我解决了我的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多