• 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)


    1.简介

       有些测试场景或者事件,Selenium根本就没有直接提供方法去操作,而且也不可能把各种测试场景都全面覆盖提供方法去操作。比如:就像鼠标悬停,一般测试场景鼠标悬停分两种常见,一种是鼠标悬停在某一个元素上方,然后会出现下拉子菜单,第二种就是在搜索输入过程,选择自动补全的字段。关于鼠标悬停,selenium把这个方法放在了Actions.java文件中,先来看看鼠标悬停出现下拉菜单的情况。

    2.鼠标悬停出现下拉菜单

      鼠标悬停出现下拉菜单,顾名思义就是:鼠标悬停在某一元素上出现下拉菜单。

     2.1项目实战

      宏哥这里用百度首页的更多元素,悬停出现拉来菜单,然后点击“音乐”为例进行实战。

    2.2代码设计

      代码设计如下:

      

    2.3参考代码

       参考代码如下:

    package lessons;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    /**
     * @author 北京-宏哥
     * 
     * 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)
     *
     * 2021年9月26日
     */
    public class ActionMusic {
    
        public static void main(String[] args) throws InterruptedException {
    
            System.setProperty("webdriver.gecko.driver",".\Tools\chromedriver.exe"); // 指定驱动路径
    
            WebDriver driver = new ChromeDriver();
            // 最大化窗口
            driver.manage().window().maximize();
            // 打开百度首页
            driver.get("http://wwww.baidu.com");
            // 声明一个Action对象
            Actions action = new Actions(driver);
            // 鼠标移动到 更多产品 上
            action.moveToElement(driver.findElement(By.xpath("//a[text()='更多']")))
                    .perform();
            // 显示等待时间10s 等 全部产品>> 出现
            WebDriverWait w = new WebDriverWait(driver, 10);
            w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By
                    .xpath("//a[text()='查看全部百度产品 >']")));
            // 等待的元素出现后点击 音乐
            WebElement cp = driver.findElement(By.xpath("//a/div[text()='音乐']"));
            cp.click();
        }
    }

    2.4运行代码

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

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

    3.搜索输入过程,选择自动补全的字段

      在搜索框输入关键词后,后提示相关内容,然后将其补全进行搜索。

    3.1项目实战

      宏哥这里就以百度搜索为例,进行实战。

    3.2代码设计

      代码设计如下:

    3.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;
    import org.openqa.selenium.interactions.Actions;
    
    /**
     * @author 北京-宏哥
     * 
     * 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)
     *
     * 2021年9月26日
     */
    public class SearchOpration {
        
        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);
    
            // 设置
            WebElement inputbox = driver.findElement(By.id("kw"));
            inputbox.sendKeys("selenium a");
    
            // 自动补全其中一个选择项
            WebElement auto_text = driver.findElement(By.xpath("//*[@id='form']/div/ul/li[@data-key='selenium appium']"));
    
            Actions action = new Actions(driver);
            action.moveToElement(auto_text).click().perform();
        }
    }

    3.4运行代码

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

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

    4.小结

       好了时间也不早了,Actions类中鼠标悬停方法就介绍到这里。提前祝大家、小伙伴和童鞋们国庆节快乐!!!



        感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
        如果您觉得阅读本文对您有帮助,请点一下左下角“推荐”按钮,您的将是我最大的写作动力!另外您也可以选择关注我,可以很方便找到我!
        本文版权归作者和博客园共有,来源网址:https://www.cnblogs.com/du-hong 欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!
    个人微信公众号 个人微信
  • 相关阅读:
    Laravel笔记
    Mysql函数大全
    nginx中文文档
    解析富文本框
    VSCode的C++环境配置,多cpp在同一文件夹(json方式)
    UltraISO光盘刻录
    plog日志库(c++)
    .NET Core安装
    Halcon深度学习——奇异值检测
    C++命名规范
  • 原文地址:https://www.cnblogs.com/du-hong/p/15337821.html
Copyright © 2020-2023  润新知