• 13.4web自动化测试(Selenium3+Java)


    一.定义

     用来做web自动化测试的框架.

    二.特点

    1.支持各种浏览器.

    2.支持各种平台(操作系统).

    3.支持各种编程语言.

    4.有丰富的api.

    三.工作原理

    四.搭环境

    1.对照Chrome浏览器版本号,下载ChromeDriver,配置环境变量,我直接把.exe文件放在了jdk安装路径的bin文件夹下了(jdk配置了环境变量).

    2.创建mavem项目,在pom.xml文件中引入Selenium依赖.

    1. <dependency>
    2. <groupId>org.seleniumhq.seleniumgroupId>
    3. <artifactId>selenium-javaartifactId>
    4. <version>4.7.2version>
    5. dependency>

    3.创建启动类,用百度进行测试.

    1. import org.openqa.selenium.WebDriver;
    2. import org.openqa.selenium.chrome.ChromeDriver;
    3. import org.openqa.selenium.chrome.ChromeOptions;
    4. public class Main {
    5. public static void main(String[] args) {
    6. ChromeOptions options = new ChromeOptions();
    7. options.addArguments("--remote-allow-origins=*");
    8. WebDriver webDriver = new ChromeDriver(options);
    9. webDriver.get("https://www.baidu.com");
    10. }
    11. }

    如果正常运行,则环境搭配好了.

    五.css选择器

    1.id选择器: #id

    2.类选择器: .class

    3.标签选择器: 标签

    4.后代选择器: 父级选择器, 子级选择器.

    注意:两种选择器,建议使用CSS选择器,因为效率高.

    六.Xpath选择器

    1.绝对路径: /html/......(效率低,不常用).

    2.相对路径: //......

    a.相对路径+索引

    //form/span[1]/input

    注意: 数组下标从1开始.

    b.相对路径+属性值

    //input[@class="s_ipt"]

    c.相对路径+通配符

    //*[@*="s_ipt"]

    d.相对路径+文本匹配

     //a[text()="新闻"]

    七.WebDriver的常用方法

    1.click: 点击.

    2.sendKeys: 在对象上模拟键盘输入.

    3.clear: 清除对象输入的文本内容.

    4.(不推荐使用)submit: 提交,和click作用一样,但是有弊端,如果点击的元素放在非form标签中,此时submit会报错(比如超链接(a标签)).

    5.text: 用于获取元素的文本信息.

    6.getAttribute: 获取属性值.

    以上所有内容的代码练习

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.WebElement;
    4. import org.openqa.selenium.chrome.ChromeDriver;
    5. import org.openqa.selenium.chrome.ChromeOptions;
    6. import java.util.List;
    7. import static java.lang.Thread.sleep;
    8. public class Main {
    9. public static void main(String[] args) throws InterruptedException {
    10. // 测试是否通过的标致
    11. boolean flag = false;
    12. ChromeOptions options = new ChromeOptions();
    13. //允许所有请求
    14. options.addArguments("--remote-allow-origins=*");
    15. WebDriver webDriver = new ChromeDriver(options);
    16. // 1.打开百度首页
    17. webDriver.get("https://www.baidu.com/");
    18. String title = webDriver.getTitle();
    19. String url = webDriver.getCurrentUrl();
    20. if (url.equals("https://www.baidu.com/") && title.equals("百度一下,你就知道")) {
    21. System.out.println("title和url正确");
    22. } else {
    23. System.out.println("title和url不正确");
    24. }
    25. // 2.两种定位元素的方式: 1.cssSelector 2.Xpath
    26. // 使用浏览器,按F12,选中要测试的位置,在代码中拷贝.
    27. // 找到百度搜索输入框
    28. // 第一种: cssSelector
    29. WebElement element = webDriver.findElement(By.cssSelector("#kw"));
    30. // 第二种: Xpath
    31. //WebElement Element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
    32. // 3.输入信息
    33. element.sendKeys("别克君越艾维亚");
    34. // 4.找到百度一下按钮
    35. // 5.点击按钮
    36. webDriver.findElement(By.cssSelector("#su")).click();
    37. sleep(2000);
    38. // 6.校验
    39. List elements = webDriver.findElements(By.cssSelector("a"));
    40. for (int i = 0; i < elements.size(); ++i) {
    41. if(elements.get(i).getText().contains("别克君越") || elements.get(i).getText().contains("艾维亚")) {
    42. System.out.println("测试通过");
    43. flag = true;
    44. break;
    45. }
    46. }
    47. if (!flag) {
    48. System.out.println("测试不通过");
    49. }
    50. // 清空输入框
    51. element.clear();
    52. sleep(1500);
    53. // 在输入框中重新输入内容
    54. element.sendKeys("别克威朗");
    55. webDriver.findElement(By.cssSelector("#su")).submit();
    56. // 获取属性值:百度一下
    57. System.out.println(webDriver.findElement(By.cssSelector("#su")).getAttribute("value"));
    58. }
    59. }

    八.等待

    1.强制等待(sleep): 一直等待到规定时间.

    2.智能等待: 设置的等待时间是最长的等待时间,如果完成了任务,会停止.

    a.隐式等待(webDriver.manage().timeouts().implicitlyWait())

    b.显示等待: 指定某个任务进行等待.

    区别: 隐式等待是等待页面上所有因素加载进来,如果规定时间内没有加载进来,就会报错.而显示等待并不关心是否加载进来所有的元素,只要在规定时间内,在所有被加载进来的元素中包含指定的元素,就不会报错.

    3.代码练习

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.chrome.ChromeDriver;
    4. import org.openqa.selenium.chrome.ChromeOptions;
    5. import org.openqa.selenium.support.ui.ExpectedConditions;
    6. import org.openqa.selenium.support.ui.WebDriverWait;
    7. import java.time.Duration;
    8. public class Main2 {
    9. public static void main(String[] args) {
    10. ChromeOptions options = new ChromeOptions();
    11. options.addArguments("--remote-allow-origins=*");
    12. // 创建驱动
    13. WebDriver driver = new ChromeDriver(options);
    14. // 连接百度
    15. driver.get("https://www.baidu.com/");
    16. // 判断元素是否可以被点击
    17. // 隐式等待
    18. // driver.manage().timeouts().implicitlyWait(Duration.ofDays(5));
    19. // 显示等待
    20. WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(3000));
    21. wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(7) > a")));
    22. }
    23. }

    九.浏览器操作

    1.后退: webdriver.navigate().back();

    2.刷新: webdriver.navigate().refresh();

    3.前进: webdriver.navigate().forward();

    4.滚动条操作: 使用js脚本

    划到最底端: 

    ((JavascriptExecutor)driver).executeScript("document.documentElement.scrollTop=10000");

    5.最大化: driver.manage().window().maximize();

    6.全屏: driver.manage().window().fullscreen();

    7.设置长宽: driver.manage().window().setSize(new Dimension(600, 800));

    8.代码

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.Dimension;
    3. import org.openqa.selenium.JavascriptExecutor;
    4. import org.openqa.selenium.WebDriver;
    5. import org.openqa.selenium.chrome.ChromeDriver;
    6. import org.openqa.selenium.chrome.ChromeOptions;
    7. import static java.lang.Thread.sleep;
    8. public class Main3 {
    9. public static void main(String[] args) throws InterruptedException {
    10. ChromeOptions options = new ChromeOptions();
    11. options.addArguments("--remote-allow-origins=*");
    12. // 创建驱动
    13. WebDriver driver = new ChromeDriver(options);
    14. // 连接百度
    15. driver.get("https://www.baidu.com/");
    16. driver.findElement(By.cssSelector("#kw")).sendKeys("君越艾维亚");
    17. driver.findElement(By.cssSelector("#su")).click();
    18. sleep(1500);
    19. // 回退
    20. driver.navigate().back();
    21. sleep(1500);
    22. // 刷新
    23. driver.navigate().refresh();
    24. sleep(1500);
    25. // 前进
    26. driver.navigate().forward();
    27. sleep(1500);
    28. // 滚动,使用js脚本
    29. // 划到最底端
    30. ((JavascriptExecutor)driver).executeScript("document.documentElement.scrollTop=10000");
    31. sleep(1500);
    32. // 最大化
    33. driver.manage().window().maximize();
    34. sleep(1500);
    35. // 全屏
    36. driver.manage().window().fullscreen();
    37. sleep(1500);
    38. // 最小化
    39. driver.manage().window().minimize();
    40. // 设置长宽
    41. driver.manage().window().setSize(new Dimension(600, 800));
    42. }
    43. }

    十.键盘

    1.control + a: 
    driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");

    2.代码

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.Keys;
    3. import org.openqa.selenium.WebDriver;
    4. import org.openqa.selenium.chrome.ChromeDriver;
    5. import org.openqa.selenium.chrome.ChromeOptions;
    6. import static java.lang.Thread.sleep;
    7. public class Main4 {
    8. public static void main(String[] args) throws InterruptedException {
    9. ChromeOptions options = new ChromeOptions();
    10. options.addArguments("--remote-allow-origins=*");
    11. // 创建驱动
    12. WebDriver driver = new ChromeDriver(options);
    13. // 连接百度
    14. driver.get("https://www.baidu.com/");
    15. driver.findElement(By.cssSelector("#kw")).sendKeys("君越艾维亚");
    16. // 键盘操作
    17. // control + A
    18. driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
    19. sleep(1500);
    20. // control + X
    21. driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
    22. sleep(1500);
    23. // control + V
    24. driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
    25. sleep(1500);
    26. }
    27. }

    十一.鼠标

    代码:

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.WebElement;
    4. import org.openqa.selenium.chrome.ChromeDriver;
    5. import org.openqa.selenium.chrome.ChromeOptions;
    6. import org.openqa.selenium.interactions.Action;
    7. import org.openqa.selenium.interactions.Actions;
    8. import static java.lang.Thread.sleep;
    9. public class Main5 {
    10. public static void main(String[] args) throws InterruptedException {
    11. ChromeOptions options = new ChromeOptions();
    12. options.addArguments("--remote-allow-origins=*");
    13. // 创建驱动
    14. WebDriver driver = new ChromeDriver(options);
    15. // 连接百度
    16. driver.get("https://www.baidu.com/");
    17. driver.findElement(By.cssSelector("#kw")).sendKeys("君越艾维亚");
    18. driver.findElement(By.cssSelector("#su")).click();
    19. sleep(1500);
    20. // 鼠标操作
    21. WebElement element = driver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
    22. Actions actions = new Actions(driver);
    23. sleep(1500);
    24. actions.moveToElement(element).contextClick().perform();
    25. }
    26. }

    十二.特殊场景

    1.定位一组元素: 

    勾选复选框

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.WebElement;
    4. import org.openqa.selenium.chrome.ChromeDriver;
    5. import org.openqa.selenium.chrome.ChromeOptions;
    6. import java.util.List;
    7. public class Main6 {
    8. public static void main(String[] args) {
    9. ChromeOptions options = new ChromeOptions();
    10. options.addArguments("--remote-allow-origins=*");
    11. // 创建驱动
    12. WebDriver driver = new ChromeDriver(options);
    13. // 连接
    14. driver.get("???");
    15. List elements = driver.findElements(By.cssSelector("input"));
    16. // 遍历elements,如果vtype值是checkbox就点击
    17. // 使用
    18. for (int i = 0; i < elements.size(); ++i) {
    19. if (elements.get(i).getAttribute("type").contains("checkbox")) {
    20. elements.get(i).click();
    21. }
    22. }
    23. }
    24. }

    2.多框架定位: 在iframe中的a标签使用常规方法无法定位

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.chrome.ChromeDriver;
    4. import org.openqa.selenium.chrome.ChromeOptions;
    5. public class Main7 {
    6. public static void main(String[] args) {
    7. ChromeOptions options = new ChromeOptions();
    8. options.addArguments("--remote-allow-origins=*");
    9. // 创建驱动
    10. WebDriver driver = new ChromeDriver(options);
    11. // 连接
    12. driver.get("???");
    13. // 对iframe底下的a标签进行操作,不能直接定位,需要先切换
    14. // 输入id号,找到指定的iframe
    15. driver.switchTo().frame("f1");
    16. driver.findElement(By.cssSelector("???")).click();
    17. }
    18. }

    3.下拉框处理:

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.WebElement;
    4. import org.openqa.selenium.chrome.ChromeDriver;
    5. import org.openqa.selenium.chrome.ChromeOptions;
    6. import org.openqa.selenium.support.ui.Select;
    7. public class Main8 {
    8. public static void main(String[] args) {
    9. ChromeOptions options = new ChromeOptions();
    10. options.addArguments("--remote-allow-origins=*");
    11. // 创建驱动
    12. WebDriver driver = new ChromeDriver(options);
    13. // 连接
    14. driver.get("???");
    15. // 获取下拉框的元素
    16. WebElement element = driver.findElement(By.cssSelector("???"));
    17. Select select = new Select(element);
    18. // 可以通过多种方式定位,常用以下两种
    19. // 1.下标定位(下标从0开始计数)
    20. select.deselectByIndex(0);
    21. // 2.根据value值定位
    22. select.deselectByValue("???");
    23. }
    24. }

    4.弹窗处理: 针对alert

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.chrome.ChromeDriver;
    4. import org.openqa.selenium.chrome.ChromeOptions;
    5. public class Main9 {
    6. public static void main(String[] args) {
    7. ChromeOptions options = new ChromeOptions();
    8. options.addArguments("--remote-allow-origins=*");
    9. // 创建驱动
    10. WebDriver driver = new ChromeDriver(options);
    11. // 连接
    12. driver.get("???");
    13. // 点击弹窗
    14. driver.findElement(By.cssSelector("button")).click();
    15. // 取消弹窗
    16. driver.switchTo().alert().dismiss();
    17. // 点击弹窗
    18. driver.findElement(By.cssSelector("button")).click();
    19. // 输入内容
    20. driver.switchTo().alert().sendKeys("张三");
    21. // 点击确认
    22. driver.switchTo().alert().accept();
    23. }
    24. }

    5.上传文件

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.chrome.ChromeDriver;
    4. import org.openqa.selenium.chrome.ChromeOptions;
    5. public class Main10 {
    6. public static void main(String[] args) {
    7. ChromeOptions options = new ChromeOptions();
    8. options.addArguments("--remote-allow-origins=*");
    9. // 创建驱动
    10. WebDriver driver = new ChromeDriver(options);
    11. // 连接
    12. driver.get("???");
    13. // 上传文件
    14. driver.findElement(By.cssSelector("???")).sendKeys("此处填写文件路径");
    15. }
    16. }

    十三.补充

    1.关闭浏览器

    a)driver.quit();

    退出浏览器,清空缓存(如cookie).

    b)driver.close();

    关闭当前正在操作的页面(不是最新的页面,要看当前正在操作的页面)

    c)代码

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.chrome.ChromeDriver;
    4. import org.openqa.selenium.chrome.ChromeOptions;
    5. import static java.lang.Thread.sleep;
    6. public class Main11 {
    7. public static void main(String[] args) throws InterruptedException {
    8. ChromeOptions options = new ChromeOptions();
    9. options.addArguments("--remote-allow-origins=*");
    10. // 创建驱动
    11. WebDriver driver = new ChromeDriver(options);
    12. // 连接百度
    13. driver.get("https://www.baidu.com/");
    14. driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
    15. sleep(1500);
    16. //driver.close();
    17. driver.quit();
    18. }
    19. }

    2.切换窗口

    a)driver.getWindowHandle();

    获取页面句柄,不是最新的页面,是当前正在操作的页面.

    b)Set windowHandles = driver.getWindowHandles();

    获取所有页面的局部,最后一个就是最新页面的句柄.

    c)代码:

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.chrome.ChromeDriver;
    4. import org.openqa.selenium.chrome.ChromeOptions;
    5. import java.util.Set;
    6. public class Main12 {
    7. public static void main(String[] args) {
    8. ChromeOptions options = new ChromeOptions();
    9. options.addArguments("--remote-allow-origins=*");
    10. // 创建驱动
    11. WebDriver driver = new ChromeDriver(options);
    12. // 连接百度
    13. driver.get("https://www.baidu.com/");
    14. // 点击新的页面
    15. driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
    16. System.out.println(driver.getWindowHandle());
    17. String handle = null;
    18. Set windowHandles = driver.getWindowHandles();
    19. for (String str : windowHandles) {
    20. handle = str;
    21. System.out.println(str);
    22. }
    23. }
    24. }

    运行结果: 

    3.截图

    a)去maven中央仓库找common-io依赖(Apache Commons IO)

    1. <dependency>
    2. <groupId>commons-iogroupId>
    3. <artifactId>commons-ioartifactId>
    4. <version>2.11.0version>
    5. dependency>

    b) File screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotAs, new File("D://picture/123.png"));

    c)代码

    1. import org.apache.commons.io.FileUtils;
    2. import org.openqa.selenium.By;
    3. import org.openqa.selenium.OutputType;
    4. import org.openqa.selenium.TakesScreenshot;
    5. import org.openqa.selenium.WebDriver;
    6. import org.openqa.selenium.chrome.ChromeDriver;
    7. import org.openqa.selenium.chrome.ChromeOptions;
    8. import java.io.File;
    9. import java.io.IOException;
    10. import static java.lang.Thread.sleep;
    11. public class Main13 {
    12. public static void main(String[] args) throws IOException, InterruptedException {
    13. ChromeOptions options = new ChromeOptions();
    14. options.addArguments("--remote-allow-origins=*");
    15. // 创建驱动
    16. WebDriver driver = new ChromeDriver(options);
    17. // 连接百度
    18. driver.get("https://www.baidu.com/");
    19. driver.findElement(By.cssSelector("#kw")).sendKeys("别克君越艾维亚");
    20. driver.findElement(By.cssSelector("#su")).click();
    21. sleep(1500);
    22. File screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    23. FileUtils.copyFile(screenshotAs, new File("D://picture/123.png"));
    24. }
    25. }
  • 相关阅读:
    【LeetCode-中等题】513. 找树左下角的值
    P3387 【模板】缩点 Tarjan强连通分量/树上dp
    SpringBoot SpringBoot 基础篇(第一篇) 第1章 SpringBoot 入门 1.4 HelloWorld 执行分析
    onbuy买家号下单教程,自养买家号测评环境资源和核心优势!
    设计一个支持百万用户的系统
    GM/T 0005《随机性检测规范》2012版和2021版对比
    Nginx安装nginx-rtmp-module模块
    BUUCTF题解之[极客大挑战 2019]Havefun 1
    顶顶通呼叫中心中间件-如何使处于机器人话术中的通话手动转接到坐席分机上讲解(mod_cti基于FreeSWITCH)
    Apache Dubbo线程监控
  • 原文地址:https://blog.csdn.net/m0_73345579/article/details/133962234