[英]List<Map<String,String>> to iterate through WebElement variables
我正在使用一個黃瓜數據表:
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 /編碼的新手,對代碼的任何評論都將有所幫助。
@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
我會做類似下面的事情。 為此,您將必須使用輸入元素的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將進行駱駝包裝,並自動調用一個名為setEmailAddress()的方法。因此,如果標題列稱為“電子郵件地址”,則Cucumber將調用一個值為jblogs@gmail.com
setEmailAddress
方法。
這將為您提供以下實現簽名
@When("^I populate a field with a new value$")
public void iPopulateAFieldWithANewValue(List<Person> persons) throws Throwable {
for (Person p : persons) {
...
}
}
如您所見,這很容易允許許多數據行。 哪種情況最適合您,完全取決於您,我只是想向您展示沒有詳細記錄的另一種選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.