Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java Selenium如何验证文本的值_Java_Html_Selenium_Automation - Fatal编程技术网

java Selenium如何验证文本的值

java Selenium如何验证文本的值,java,html,selenium,automation,Java,Html,Selenium,Automation,如何检查表中文本的值。我正在使用java和selenium类 我想验证td中的值是否为“响应时间” 下面是html代码 <a tabindex="0" role="button" data-toggle="popover" data-trigger="focus" data-placement="auto" data-content="" data-original-title="" title=""></a><td style="">timeToRespon

如何检查表中文本的值。我正在使用java和selenium类

我想验证td中的值是否为“响应时间”

下面是html代码

<a tabindex="0" role="button" data-toggle="popover" data-trigger="focus" data-placement="auto" data-content="" data-original-title="" title=""></a><td style="">timeToResponse</td>
timeto响应
  • 对找到的元素使用getText()方法查找文本
  • 使用java equals方法验证它是否为预期文本
  • 您还可以使用testNG断言来验证值

      WebElement element = 
        driver.findElement(By.xpath("//*[@id='IncidentSearch']/tbody/tr/td[33]"));
        if (element.getText().equals("timeToResponse")))
         System.out.println("Match found");
        else 
         System.out.println("Match Not found");
       Assert.assertEquals(element.getText(), "timeToResponse");
    

  • 您使用了绝对xpath,这在自动化脚本中是不推荐的。因为它包含HTML索引,并且如果DOM随着结构的改变而改变,那么这个xpath就不能再工作了。您应该使用相对xpath

    节点的相对xpath:
    //此属性的[@title='title值']//td

    与wise一样,您可以使用
    标记的任何属性并定位

    您可以通过Java equal方法进行检查

    String retrieveText = driver.findElement(By.xpath("//a[@title='Title value of this attribute']//td")).getText();
    
    if(retrieveText.equals("timeToResponse")) {
    //User defined message on console
    System.out.println("Value match as Expected");
    }
    

    如果此测试是通过JUnit执行的,那么您可以使用:

    void org.junit.Assert.assertEquals(字符串消息,预期对象,实际对象)

    i、 e

    String retrieveText = driver.findElement(By.xpath("//a[@title='Title value of this attribute']//td")).getText();
    
    if(retrieveText.equals("timeToResponse")) {
    //User defined message on console
    System.out.println("Value match as Expected");
    }
    
    assertEquals("timeToResponse Check ....","timeToResponse",driver.findElement(By.xpath("//a[@title='Title value of this attribute']//td")).getText());