0

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

这是我需要值的文本字段

输入类型="text" value="sadas" class="mdl-textfield__input" id="last_name" name="last_name" placeholder="输入姓氏"

我得到的错误,

TestNG 错误消息显示如下, java.lang.AssertionError: expected [6234] but found [] and nothing print for the console

我试过“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();
    }

我怎样才能解决这个问题?以及为什么当预期值与找到的值相同时出现此错误?(手动检查系统时)

4

1 回答 1

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");
于 2019-03-06T05:51:12.617 回答