Customer.json具有以下内容:
[{
"firstName": "Test",
"lastName": "Testing",
"age": 35,
"emailAddress": "test @gmail.com",
"path": "xpath, //input[id='Login']",
}]
客户POJO类如下:
package data;
import org.openqa.selenium.WebElement;
import static automationFramework.PageActions.clickElement;
public class Customer {
public String firstName;
public String lastName;
public int age;
public String emailAddress;
public WebElement path;
public void Test() throws InterruptedException {
clickElement(path);
}
}
读取WebElement的代码:
//Code to read Webelements
Gson gson = new Gson();
BufferedReader bufferReader = null;
try {
//CustomerFilePath is JSON path
bufferReader = new BufferedReader(new FileReader(customerFilePath));
Customer[] customers = gson.fromJson(bufferReader, Customer[].class);
return Arrays.asList(customers);
}catch(FileNotFoundException e) {
throw new RuntimeException("Json file not found at path : " + customerFilePath);
}finally {
try { if(bufferReader != null) bufferReader.close();}
catch (IOException ignore) {}
}
当前我正在使用字符串,这工作正常,但是我打算用WebElement(上面的路径)替换字符串,我看到以下错误:
java.lang.RuntimeException: Unable to invoke no-args constructor for interface org.openqa.selenium.WebElement. Registering an InstanceCreator with Gson for this type may fix this problem.
at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
如何在WebElement上进行此项工作。目标是将JSON用于路径和所有元素定位符?