• 【测试开发之路】Java & Selenium自动化


    一、概述与入门

    1、Selenium介绍

    ​ 使用前需要下载浏览器对应的Driver,Selenium提供了EdgeDriver和ChromiumDriver两种驱动类。需要安装与本机浏览器版本相同的驱动。

    ​ EdgeDriver下载地址:Microsoft Edge WebDriver - Microsoft Edge Developer

    ​ ChromiumDriver下载地址:CNPM Binaries Mirror (npmmirror.com)

    2、导入Maven库

    (1)Selenium
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.4.0</version>
    </dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    (2)TestNG[用于单元测试]
    
    <dependency>
        <groupId>org.testnggroupId>
        <artifactId>testngartifactId>
        <version>7.4.0version>
        <scope>testscope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、TestNG的使用

    ​ 参考博客:https://blog.csdn.net/lovedingd/article/details/106784561

    ​ TestNG是一个java中的开源自动化测试框架,其灵感来自JUnit和NUnit,TestNG还涵盖了JUnit4整个核心的功能,但引入了一些新的功能,使其功能更强大,使用更方便。

    ​ 其常用注解有:BeforeTest、Test、AfterTest、BeforeClass、AfterClass等

    4、第一个Selenium的程序

    ​ 我们编写了一个单元测试类,通过@Test注解来标注测试方法。

    public class TestCase {
        @Test
        public void openURL(){
            //获取driver路径
            Path driverPath = Paths.get("src","driver","msedgedriver.exe");
            //设置程序环境
            System.setProperty("webdriver.edge.driver",driverPath.toAbsolutePath().toString());
            WebDriver driver = new EdgeDriver();
            driver.get("https://www.baidu.com");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ​ 这里要注意msedgedriver.exe的位置是src/driver/msedgedriver.exe。

    二、元素定位方式

    ​ 通过EdgeDriver的实例化对象的findElement()方法来获取相应的元素,并执行操作。

    1、id属性定位

    ​ 这里通过By.id(“string”)的方式来获取元素,并执行了sendKeys(“文本内容”)和click()两种操作。

    @Test
    public void getElementById(){
    	webDriver.get("https://www.baidu.com");
    	webDriver.findElement(By.id("kw")).sendKeys("未来村村长");
    	webDriver.findElement(By.id("su")).click();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、name属性定位

    webDriver.findElement(By.name("wd")).sendKeys("未来村村长");
    
    • 1

    3、class属性定位

    webDriver.findElement(By.className("s_ipt")).sendKeys("未来村村长");
    
    • 1

    4、XPath定位⭐

    ​ XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。其常用表达式如下:

    表达式描述
    nodename选取此节点的所有子节点
    /从根节点选取
    //从任意位置进行匹配
    @选取属性
    []表示谓语,用于查询某个指定值的节点
    *匹配任何元素节点
    @*匹配任何属性节点
    webDriver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("未来村村长");
    
    • 1

    ​ 浏览器检查-选中元素标签-右键-复制-复制xpath可快速获取XPath路径。

    三、事件

    1、鼠标滑动

    ​ 首先需要实例化Actions对象,然后传入WebDriver对象。先找到相应的元素,存储到WebElement对象中,然后通过actions实例化对象去模拟鼠标事件[moveToElement],再执行相应动作,最后需要perform来开始执行动作。

    public void getElementById(){
    	webDriver.get("https://www.baidu.com");
    	Actions actions = new Actions(webDriver);
    	WebElement element1 = webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/div/a"));
    	actions.moveToElement(element1);
    	WebElement element2 = webDriver.findElement(By.xpath("//*[@id=\"s-top-more\"]/div[3]/a[3]/img"));
    	actions.moveToElement(element2).click();
    	actions.perform();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、浏览器窗口切换

    ​ 浏览器窗口切换首先需要通过webDriver的getWindowHandle()方法获取窗口定位字符串,然后通过webDriver的switchTo().window(定位字符串)进行切换。

    @Test
    public void getElementById(){
    	webDriver.get("https://www.baidu.com");
    	String baidu = webDriver.getWindowHandle();
    	webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[3]")).click();
    	Set<String> windows = webDriver.getWindowHandles();
    	webDriver.switchTo().window(baidu);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、iframe窗口切换

    ​ 当网页内嵌了iframe时,需要先切换到相应的iframe才能执行操作。iframe页面也是一个WebElement元素,可以通过findElement()方法获取,然后通过switchTo().frame(iframe)进行切换。

    WebElement iframe = webDriver.findElement(By.id("iframe-id"));
    webDriver.switchTo().frame(iframe);
    
    • 1
    • 2

    4、设置等待时间

    (1)最长等待时间

    ​ 当页面加载较慢时,我们可以先进行等待,该等待使后序操作执行回旋,直到执行成功则放弃等待时间。

    webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    
    • 1
    (2)确定等待时间
    WebDriverWait wait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
    WebElement element = wait.until(ExceptedConditions.presenceOfElementLocated(By.id("xxx")));
    
    • 1
    • 2
  • 相关阅读:
    uni-app 企业微信开发导入jweixin-1.2.0.js文件
    【VS Code 神奇小插件】Code Runner
    基于Python的爬虫演示示例-以电影网站为例
    js knex基本用法
    【Mac】破解死循环,成功安装 Homebrew、curl、wget,快速配置 zsh
    Edge浏览器崩溃解决方案
    [附源码]计算机毕业设计市场摊位管理系统Springboot程序
    serialVersionUID的重要性,及Idea自动生成 serialVersionUID的设置
    Hbase容灾与备份
    Ansible 自动化运维工具 --- playbook 剧本
  • 原文地址:https://blog.csdn.net/apple_51976307/article/details/126686268