列表<地图<字符串,字符串>&GT;迭代WebElement变量

时间:2016-03-10 05:20:00

标签: java for-loop selenium-webdriver datatable cucumber

我正在使用黄瓜数据表:

When I populate a field with a new value
      | FieldName  | FieldValue |
      | Name | Joe Blogs |
      | Email address | jblogs@gmail.com |
      | Phone 1 | 04 555 6666 |
      | Phone 2 | 0800 123 4567 |
      | SMS Phone | 023 222 333 |
      | Fax number | 09 888 9999 |
      | Location | Bermuda Triangle |

使用以下java类:

@When("^I populate a field with a new value$")
    public void ShouldPopulateFieldsWithValues(DataTable arg1) throws Throwable {
        List<Map<String,String>> data=arg1.asMaps(String.class,String.class);
        //Declare a string variable for NAME and assign it's value
        String profileNameTextboxValue = data.get(0).get("FieldValue");
        //Find the profile NAME text box
        WebElement profileNameTextbox = driver.findElement(By.id("name"));
        //Clear the value from the profile NAME text box
        profileNameTextbox.clear();
        //Send the string value to the profile NAME text box
        profileNameTextbox.sendKeys(profileNameTextboxValue);

        //Declare a string variable for EMAIL and assign it's value
        String profileEmailValue = data.get(1).get("FieldValue");
        //Find the profile EMAIL text box
        WebElement profileEmailTextbox = driver.findElement(By.id("email"));
        //Clear the value from the profile EMAIL text box
        profileEmailTextbox.clear();
        //Send the string value to the profile EMAIL text box
        profileEmailTextbox.sendKeys(profileEmailValue);

        //Declare a string variable for PHONE1 and assign it's value
        String profilePhone1Value = data.get(2).get("FieldValue");
        //Find the profile PHONE1 text box
        WebElement profilePhone1Textbox = driver.findElement(By.id("phone"));
        //Clear the value from the profile PHONE1 text box
        profilePhone1Textbox.clear();
        //Send the string value to the profile PHONE1 text box
        profilePhone1Textbox.sendKeys(profilePhone1Value);

        //Declare a string variable for PHONE2 and assign it's value
        String profilePhone2Value = data.get(3).get("FieldValue");
        //Find the profile PHONE2 text box
        WebElement profilePhone2Textbox = driver.findElement(By.id("phone2"));
        //Clear the value from the profile PHONE2 text box
        profilePhone2Textbox.clear();
        //Send the string value to the profile PHONE2 text box
        profilePhone2Textbox.sendKeys(profilePhone2Value);

        //Declare a string variable for SMS Phone and assign it's value
        String profileSMSValue = data.get(4).get("FieldValue");
        //Find the profile sms phone text box
        WebElement profileSMSTextbox = driver.findElement(By.id("sms_phone"));
        //Clear the value from the profile sms phone text box
        profileSMSTextbox.clear();
        //Send the string value to the profile sms phone text box
        profileSMSTextbox.sendKeys(profileSMSValue);

        //Declare a string variable for Fax Number and assign it's value
        String profileFaxValue = data.get(5).get("FieldValue");
        //Find the profile fax text box
        WebElement profileFaxTextbox = driver.findElement(By.id("fax"));
        //Clear the value from the profile fax text box
        profileFaxTextbox.clear();
        //Send the string value to the profile fax text box
        profileFaxTextbox.sendKeys(profileFaxValue);

        //Declare a string variable for Location and assign it's value
        String profileLocationValue = data.get(6).get("FieldValue");
        //Find the profile Location text box
        WebElement profileLocationTextbox = driver.findElement(By.id("location"));
        //Clear the value from the profile Location text box
        profileLocationTextbox.clear();
        //Send the string value to the profile Location text box
        profileLocationTextbox.sendKeys(profileLocationValue);
}

上面的代码有效,但是肯定有一种更有效的方法来编写java类吗?

我希望我能找到一种方法来迭代使用数据表中不同值的WebElement变量。反正有吗?

我找到了两个代码示例,我会尝试和改编,但不太确定如何将它们放在一起..

第一个例子:

for (Map.Entry<String, String> entry : countries.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        // ...
        }

第二个例子:

List<Map<String, Object>> list; // this is what you have already

    for (Map<String, Object> map : list) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    }
    }

注意:我是java / coding的新手,对代码的任何批评都会有所帮助..

@homaxto,接受指令,我创建了以下bean类Person ..

public class Person implements java.io.Serializable {

    // Properties
        private String Username;
        private String Name;
        private String Email;
        private String Phone1;
        private String Phone2;
        private String SMS;
        private String Fax;
        private String Location;
        private String Address;
        private String Zip;
        private String PayPal;

    // Getters
        public String getUsername() { return Username; }
        public String getName() { return Name; }
        public String getEmail() { return Email; }
        public String getPhone1() { return Phone1; }
        public String getPhone2() { return Phone2; }
        public String  getSMS() { return SMS; }
        public String getFax() { return Fax; }
        public String getLocation() {return Location; }
        public String getAddress() { return Address; }
        public String getZip() { return Zip; }
        public String getPaypalEmail() { return PayPal; }

    // Setters
    public void setUsername() { this.Username = Username; }
    public void setName() { this.Name = Name; }
    public void setEmail() { this.Email = Email; }
    public void setPhone1() { this.Phone1 = Phone1; }
    public void setPhone2() { this.Phone2 = Phone2; }
    public void setSMS() { this.SMS = SMS; }
    public void setFax() { this.Fax = Fax; }
    public void setLocation() {this.Location = Location; }
    public void setAddress() { this.Address = Address; }
    public void setZip() { this.Zip = Zip; }
    public void setPaypalEmail() { this.PayPal = PayPal; }
}

我的数据表如下:

Feature: Update my personal details
  As a Property Manager
  I want to update my personal details
  So that I can be reached by my customers

  @wip
  Scenario: Update my personal details
    Given I am logged in to my account
    Given I have navigated to the change profile tab
    When I populate a field with a new value
    | Username | Name        | Email         | Phone1     | Phone2       | SMS   | Fax  | Location         | Address        | Zip  | PayPal |
    | testy    | Test Logger | testerslog1@gmail.com | 04 555 6666 | 0800 123 4567 | 029 295 495 | 04 888 9999 | Bermuda Triangle | 5 Pokemon Lane | 9999 | testersLog1@gmail.com |
    Then I click save

1 个答案:

答案 0 :(得分:0)

我会做以下事情。为此,您必须使用输入元素的ID作为第一列中的值。 如果必须在数据表中具有较少的依赖关系,则可以在步骤实现中对其进行映射,并在使用之前进行查找。 此外,您不需要标题行,并且只需要跳过第一行。

@When("^I populate a field with a new value$")
    public void ShouldPopulateFieldsWithValues(DataTable dataTable) throws Throwable {
        List<List<String>> rows = dataTable.asLists(String.class);
        // Either quit having a header in your datatable or remove the first row
        rows.remove(0);
        for (List<String> row : rows) {
            String fieldName = row.get(0);
            String fieldValue = row.get(1);

            // Use the IDs as name in your datatable
            WebElement profileNameTextbox = webdriver.findElement(By.id(fieldName));
            profileNameTextbox.clear();
            profileNameTextbox.sendKeys(fieldValue);
        }
    }

绕过这个问题的另一种方法可能是将具有字段名称的表作为列名转换。

When I populate a field with a new value
  | Name      | Email address    | Phone 1     | Phone 2       | SMS Phone   | Fax number  | Location         |
  | Joe Blogs | jblogs@gmail.com | 04 555 6666 | 0800 123 4567 | 023 222 333 | 09 888 9999 | Bermuda Triangle |

使用此设计,您可以为每列创建一个包含getter和setter的bean类“Person”。 Cucumber将执行camel-casing并自动调用名为setEmailAddress()等的方法。因此,如果标题列被称为“电子邮件地址”,那么Cucumber将调用名为setEmailAddress的方法,其值为{{1} }。
这将为您提供以下实现签名

jblogs@gmail.com

正如您可能看到的,这很容易允许多行数据。 哪一个最适合你的情况完全取决于你,我只想展示你所拥有的另一个选项,而这个选项并没有详细记录。