【发布时间】: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