java.lang.AssertionError:找到的值显示为null

时间:2019-03-06 05:04:39

标签: selenium-webdriver testng qa testng-eclipse testng.xml

我想检查文本字段值是否与我在代码中提到的期望值相同

这是我需要值的 文本字段

input type =“ text” value =“ sadas” class =“ mdl-textfield__input” id =“ last_name” name =“ last_name” placeholder =“输入姓氏”

错误 我得到了什么

TestNG错误消息显示如下, java.lang.AssertionError:预期为[6234],但发现为[] 也没有为控制台打印任何内容

我尝试使用“ Assert.assertTrue(lastName.equals(“ lastName:6234”));“也是

@Test
    public void tc001() {       
    driver.get(baseUrl);
    driver.findElement(By.xpath("//input[@name='email']")).click();
    driver.findElement(By.xpath("//input[@name='email']")).clear(); driver.findElement(By.xpath("//input[@name='email']")).sendKeys("x@gmail.com");
    driver.findElement(By.xpath("//input[@name='password']")).clear();driver.findElement(By.xpath("//input[@name='password']")).sendKeys("123456");
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and  normalize-space(.)='Forgot Your Password?']) [1]/preceding::button[1]")).click();
    driver.findElement(By.linkText("Nadee")).click();
    driver.findElement(By.linkText("Profile")).click();
    String lastName = driver.findElement(By.xpath("//input[@name='last_name']")).getText();
    Assert.assertEquals(lastName ,"6234");
    System.out.println(lastName);   
    driver.findElement(By.linkText("Log Out")).click();
    }

我该如何解决?以及为什么期望值与找到的期望值相同时出现此错误?(当手动检查系统时)

1 个答案:

答案 0 :(得分:1)

尝试添加获取姓氏字段值之前的等待。您在获得姓氏值之前单击链接,这可能是页面未完全加载。

driver.findElement(By.linkText("Profile")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='last_name']")));
String lastName = driver.findElement(By.xpath("//input[@name='last_name']")).getAttribute("value");
system.out.println("Last Name value is: "+ lastName); // check the output on console too
Assert.assertEquals(lastName ,"6234");