我目前正试图在一个方法中存储字段的文本值,然后在一个单独的方法中使用断言。
以下是我正在使用的方法,即存储文本(这是正常工作并正确打印到控制台)......
protected void selectLeadPassenger(String name) throws Throwable {
moveToElement("lead-dropdown-complete", name);
waitForElementToBeVisible("#lead-dropdown-complete > div > ul > li:nth-child(1) > div > h3");
String leadPassengerName = driver.findElement(By.xpath("//h3[contains(@text(), " + name + ")]")).getText(); //THIS IS THE FIELD I AM STORING
System.out.print(leadPassengerName); //THIS IS PRINTING OUT THE CORRECT VALUE WITHIN THE CONSOLE
driver.findElement(By.xpath("//h3[contains(@text(), " + name + ")]")).click();
System.out.println(name + " searched for and selected within the 'LEAD PASSENGER' field");
}
但是,我很难找到一种方法来使用' leadPassengerName'在一个单独的方法中断言,例如我尝试了以下......
protected void validateLeadPassengerName() throws Throwable {
String checkText = driver.findElement(By.xpath("//p[contains(@data-bind, 'vm.LeadPassengerName')]")).getText();
Assert.assertTrue("", checkText.contains(leadPassengerName));
}
我确定问题与我存储' leadPassengerName'的方式有关。因为在validateLeadPassengerName方法中它以红色显示,并显示以下错误消息"无法解析symbole' leadPassengerName'"。
答案 0 :(得分:2)
有多种方法可以解决这个问题。首先,它与selenium无关,它更多地是关于编程语言中的数据流
protected string validateLeadPassengerName(string leadPassengerName) throws Throwable {
String checkText = driver.findElement(By.xpath("//p[contains(@data-bind, 'vm.LeadPassengerName')]")).getText();
Assert.assertTrue("", checkText.contains(leadPassengerName));
return checkText;
}
所以当你可以调用你的函数时
String leadPassengerName = driver.findElement(By.xpath("//h3[contains(@text(), " + name + ")]")).getText();
validateLeadPassengerName(leadPassengerName)
同样,通过在类级别创建变量,您可以在函数之间共享驱动程序,您可以执行相同的操作来存储潜在客户名称