Reputation: 1
I m trying an Api in Enterprise messaging solution(msg91) : Get Balance api Where balance is displayed :
Response :
[{"balance":1000,"route":"4","type":"SMS"}]
I want to get text from Balance . How can i fetch it using web-driver .
Upvotes: 0
Views: 604
Reputation: 23
Create a Balance Class:
@Getter
@Setter
public class Balance {
private String balance;
private String route;
private String type;
}
Create a BalancePage class to navigate the page using webDriver, read the response and parse it to json:
public class BalancePage extends Page {
private List<Balance> balances;
@FindBy(xpath = "html/body")
WebElement response;
BalanceConfig config = create(BalanceConfig.class);
protected BalancePage(TestWebDriver driver) {
super(driver);
testWebDriver.setBaseURL(config.url() + "?authkey=" + config.authKey());
PageFactory.initElements(new AjaxElementLocatorFactory(testWebDriver, 30), this);
}
private String getBalanceString() {
return response.getText();
}
public void getCurrentBalance() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
balances = objectMapper.readValue(getBalanceString(), objectMapper.getTypeFactory().constructCollectionType(List.class, Balance.class));
}
}
public abstract class Page {
public WebDriver testWebDriver;
protected Page(WebDriver driver) {
this.testWebDriver = driver;
}
}
Upvotes: 1
Reputation: 2703
simple string
string[] res = response.Split(",")
string balance = new String(res[0].ToCharArray().Where(c => Char.IsDigit(c)).ToArray());
Upvotes: 0