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);
}
}
Код для чтения WebElements:
//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 для путей и всех локаторов элементов?