我正在尝试验证以下site的错误验证消息。 单击提交验证后,将显示所有字段的错误消息。 我尝试了代码:
List<WebElement> errormsg = driver.findElements(By.className("errorValidation"));
System.out.println(errormsg);
它没有检索到错误消息,而是出错:
[[[FirefoxDriver: firefox on WINDOWS (c997bfe2-8c35-4b00-abf9-74a96437020c)] -> >class name: errorValidation], [[FirefoxDriver: firefox on WINDOWS (c997bfe2->8c35-4b00-abf9-74a96437020c)] -> class name: errorValidation], [[FirefoxDriver: firefox on WINDOWS (c997bfe2-8c35-4b00-abf9-74a96437020c)] -> class name: >errorValidation], [[FirefoxDriver: firefox on WINDOWS (c997bfe2-8c35-4b00-abf9->74a96437020c)] -> class name: errorValidation], [[FirefoxDriver:
答案 0 :(得分:0)
driver.findElements()
不返回错误消息字符串数组,它返回Selenium WebElements数组。它们中的每一个都有一个文本值,您必须从每个WebElement中获取该文本值:
System.out.println(errormsg[0].gettext());
System.out.println(errormsg[1].gettext());
...
(我在C#中进行Selenium测试,其中有一个名为“Text”的属性,但我认为在Java中你有这个名为gettext()的方法。)
答案 1 :(得分:0)
尝试这样: 我已经运行了这段代码并且工作得很好。
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
public class stackTest {
static WebDriver driver = null;
public static void main(String [] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("https://tenoapp.com/registerSchool");
Thread.sleep(4000);
driver.manage().window().maximize();
driver.findElement(By.cssSelector("#submitRegForm")).click();
Thread.sleep(4000);
String[] urerrormsg = { "School name can't be blank",
"Number of students can't be blank",
"Board can't be blank",
"First name can't be blank",
"Last name can't be blank",
"Designation can't be blank",
"Mobile number can't be blank",
"Password can't be blank",
"Confirm password can't be blank"};
List<WebElement> erromsgsize = driver.findElements(By.className("errorValidation"));
System.out.println("***** "+erromsgsize.size());
int i = 0;
for (WebElement errormsg : erromsgsize) {
System.out.println("**** "+errormsg.getText());
if(errormsg.getText().equals("")){
continue;
}
else
Assert.assertTrue(errormsg.getText().equals(urerrormsg[i]));
i++;
}
//Close the browser
driver.quit();
}
}
答案 2 :(得分:-1)
您可以使用以下代码:
List<WebElement> el = driver.findElements(By.xpath("//span[@class='errorValidation']")
User TestNg Assert函数如:
Assert.assertTrue(el.get(0).equals("School name can't be blank");
Assert.assertTrue(el.get(1).equals("Number of students can't be blank");
类似于您可以申请的其他字段
希望这会奏效。
由于 萨迪克