我为网站编写自动化测试。我正在使用Cucumber-jvm和Selenium Webdriver。 我想在我的项目中添加类似config的内容。
例如,假设我要测试两个网站的授权。这两个网站上的一切都相同。 例如在我的功能中,我有两种情况-成功的自动归类和“用户名”字段的边界检查。
想象一下我的功能文件如下:
Scenario: User enters valid parameters ....
Given User is on main page
When User enters "username" as "username"
When User enters "Test@123" as "password"
#Then do something
Scenario: User tries to enter more than 20 characters in username field
Given User is on main page
When User enters "30" characters as "username"
Then text length in the "username" field should be "20"
但是网站的用户名和密码不同。用户名字段的最大长度也不同。我想要以下内容:
Scenario: User enters valid parameters
Given User is on main page
When User enters "@username" as "username"
When User enters "@password" as "password"
#Then do something
Scenario: User tries to enter more than 20 characters in username field
Given User is on main page
When User enters "30" characters as "username"
Then text length in the "username" field should be "@max_length_username"
当我更改网站时,我只想更改定义了全局变量的某些文件。
例如,在我的代码中,我有@given方法
public void user_is_on_Registration_page() throws Exception {
driver.get("https://website.test");
username.sendKeys("username");
password.sendKeys("Test123");
}
我想将其更改为方法:
public void user_is_on_Registration_page() throws Exception {
driver.get(website);
username.sendKeys(username);
password.sendKeys(password);
}
我想要一个文件,可以在其中设置所有配置和变量,并在我的.java和.feature文件中使用它们。 我该如何实现?
答案 0 :(得分:0)
我找到了答案,这不是最好的方法,但是可行。
我有类变量。
public class Variables {
public String valid_username = "someusername";
public String valid_password = "Test@123";
public Map<String,String> variables = new HashMap<String,String>();
public Variables(){
variables.put("valid_username",valid_username);
variables.put("valid_password",valid_password);
}
public String getVariable(String key){
return variables.get(key);
}
}
它是配置。 在功能中,我有步骤:
When User enters "valid_username" as username
And User enters "valid_password" as password
仅此而已。然后在步骤definitons中,我只调用
variables.getVariable(arg1); //arg1 is "valid_username" from feature file