《手把手教你》系列技巧篇(四十七)java+ selenium自动化测试判断元素是否显示(详解教程) 北京

1.简介

webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelected表示查看元素是否被选中,一般用在勾选框中(多选或者单选),isDisplayed表示查看什么呢?

2.isDisplayed()源码

/**
   * Is this element displayed or not? This method avoids the problem of having to parse an
   * element's "style" attribute.
   *
   * @return Whether or not the element is displayed
   */
  boolean isDisplayed();

 从上边的源码中的注释可以看出isDisplay()方法是用来判断页面元素是否显示在页面。存在返回true,不存在返回false。

3.isDisplay()用法

List<WebElement> targetElement =  driver.findElements(By.xpath("xpath_your_expected_element"));

    try {

        if(targetElement>=1) {

            if(targetElement.isDisplayed()) {

                System.out.println("Element is present");

            }else {

                System.out.println("Element is found, but hidden on the page");
            
            }

        }else {

            System.out.println("Element not found on the page");

        }
    }catch (NoSuchElementException e) {

        System.out.println("Exception in finding the element:" + e.getMessage());

    }

4.项目实战

 在webdriver自动化测试中,我们经常需要进行判断的一个场景。例如,有些操作,我们做了之后,会触发一些提醒,有些是正确的提醒,有些是红色字体显示的错误提示。我们自动化里面如何去捕获这些字段,如果进行测试自动化判断呢。这里就要用到isDisplay()方法了。宏哥这里用度娘的首页登录举例,判断“请填写验证码”这个字段是否出现。

4.1测试用例(思路)

1.访问度娘首页

2.定位首页的登录按钮,然后点击

3.弹出登录框定位短信登录按钮,然后点击

4.定位手机号输入框,然后输入手机号

5.定位登录框的登录按钮,然后点击

6.定位出现的“请填写验证码”,然后判断。

4.2代码设计

根据测试用例进行代码设计如下:

4.3参考代码

package lessons;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author: 北京-宏哥
 * 
 * @公众号:北京宏哥
 * 
 * 《手把手教你》系列技巧篇(四十七)-java+ selenium自动化测试-判断元素是否存在(详解教程)
 *
 * 2021年11月19日
 */
public class testDisplay {
    
public static void main(String[] args) throws Exception {  
        
        System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");  
           
        WebDriver driver = new ChromeDriver();  
     
        driver.manage().window().maximize();  
       
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //访问度娘首页 
        driver.get("https://www.baidu.com/");  
       
        Thread.sleep(1000);
        
        //定位首页的登录按钮,然后点击登录
        driver.findElement(By.xpath("//*[@id='u1']/a[1]")).click();
        //弹出登录框定位短信登录按钮,然后点击
        driver.findElement(By.id("TANGRAM__PSP_11__changeSmsCodeItem")).click();
        //定位手机号输入框,然后输入手机号
        driver.findElement(By.id("TANGRAM__PSP_11__smsPhone")).sendKeys("13734294156");
        //定位登录框的登录按钮,然后点击
        driver.findElement(By.id("TANGRAM__PSP_11__smsSubmit")).click();
        Thread.sleep(2000);
        // 方法一
        WebElement error_message = driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_11__smsError' and text()='请填写验证码']"));
        if(error_message.isDisplayed()){
            System.out.println("宏哥!元素存在");
        }else{
            System.out.println("宏哥!元素不存在");
        }
        driver.quit();
    }  
} 

4.4运行代码

1.运行代码,右键Run AS->Java Appliance,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

5.方法二

第二个方法,就是先得到这个字符串用String变量保存下来,然后对两个字符串进行比较。其实这种方法前边已经用过,只不过是宏哥没有指出,就像前边文章中的toast元素,直接定位存储在变量里,然后将其的文本打印出,是不是啊各位小伙伴们或者童鞋们。

5.1代码设计

5.2参考代码

package lessons;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author: 北京-宏哥
 * 
 * @公众号:北京宏哥
 * 
 * 《手把手教你》系列技巧篇(四十七)-java+ selenium自动化测试-判断元素是否存在(详解教程)
 *
 * 2021年11月19日
 */
public class testDisplay {
    
public static void main(String[] args) throws Exception {  
        
        System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");  
           
        WebDriver driver = new ChromeDriver();  
     
        driver.manage().window().maximize();  
       
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //访问度娘首页 
        driver.get("https://www.baidu.com/");  
       
        Thread.sleep(1000);
        
        //定位首页的登录按钮,然后点击登录
        driver.findElement(By.xpath("//*[@id='u1']/a[1]")).click();
        //弹出登录框定位短信登录按钮,然后点击
        driver.findElement(By.id("TANGRAM__PSP_11__changeSmsCodeItem")).click();
        //定位手机号输入框,然后输入手机号
        driver.findElement(By.id("TANGRAM__PSP_11__smsPhone")).sendKeys("13734294156");
        //定位登录框的登录按钮,然后点击
        driver.findElement(By.id("TANGRAM__PSP_11__smsSubmit")).click();
        Thread.sleep(2000);
        // 方法二
        String error_message = driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_11__smsError' and text()='请填写验证码']")).getText();
        if(error_message.equals("请填写验证码")){
            System.out.println("宏哥!元素存在");
        }else{
            System.out.println("宏哥!元素不存在");
        }
        driver.quit();
    }  
}

5.3运行代码

1.运行代码,右键Run AS->Java Appliance,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

6.小结

1.isDisplayed()本身这个函数用于判断某个元素是否存在页面上(这里的存在不是肉眼看到的存在,而是html代码的存在。某些情况元素的visibility为hidden或者display属性为none,我们在页面看不到但是实际是存在HTML页面的一些元素)。

2.使用equals()和==,区别在于equals比较的是内容是否相等、==比较的是引用的变量地址是否相等。



    感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
    如果您觉得阅读本文对您有帮助,请点一下左下角“推荐”按钮,您的将是我最大的写作动力!另外您也可以选择关注我,可以很方便找到我!
    本文版权归作者和博客园共有,来源网址:https://www.cnblogs.com/du-hong 欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!
个人微信公众号 个人微信
原文地址:https://www.cnblogs.com/du-hong/p/15545187.html