cucumber-jvm 和 selenium WebDriver 的全局环境变量

Global Enviromental Variables for cucumber-jvm & selenium WebDriver

我使用 cucumber-jvm 和 selenium webdriver 为 websites.I 编写自动化测试。 我想在我的项目中有类似配置的东西。

例如假设我想测试两个网站的授权。这两个网站上的一切都一样。 例如,在我的功能中,我有两种情况 - 用户名字段的成功授权和边界检查。

假设我的特征文件看起来像这样:

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" 

但是 websites.Also 用户名和密码不同 websites.Also 用户名字段的最大长度是 different.I 需要以下内容:

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 文件中使用它们。 我怎样才能做到这一点?

我找到了答案,这不是最好的方法,但它有效。

我有 class 个变量。

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

就是这样。然后在我的步骤定义中,我只调用

variables.getVariable(arg1);  //arg1 is "valid_username" from feature file