2016-10-27 46 views

回答

0

下面的答案是基於假設,參考在SendKeys函數值的JSON是以下格式:

{ 
    "testData": [{ 
     "userName": "test1", 
     "password": "password1" 
    }, { 
     "userName": "test2", 
     "password": "password2" 
    }] 
} 

我會建議使用傑克遜反序列化數據。您需要在您的類路徑中使用jackson-core,jackson-annotations,jackson-databind jar相同的版本。

創建如下類用戶:

public class User { 
    private String userName; 
    public String getUserName() { 
     return userName; 
    } 
    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    private String password; 
} 

創建類TESTDATA如下:

public class TestData { 
    List<User> testData; 

    public List<User> getTestData() { 
     return testData; 
    } 

    public void setTestData(List<User> testData) { 
     this.testData = testData; 
    } 
} 

反序列化將會按以下步驟進行:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 
     WebDriver driver = new ChromeDriver(); 
     // You can read the json from property file/service call and store to string. 
     String jsonString = "{\"testData\": [{\"userName\": \"test1\",\"password\": \"pwd1\"}, {\"userName\": \"test2\", \"password\": \"pwd2\" }]}"; 
     ObjectMapper mapper = new ObjectMapper(); 
     TestData obj = mapper.readValue(jsonString,TestData.class); 
     for (User data : obj.getTestData()) { 
      driver.findElement(By.id("usernameS")).sendKeys(data.getUserName()); 
      driver.findElement(By.id("passwordS")).sendKeys(data.getPassword()); 
     } 
} 
相關問題