自动化测试

自动化测试

1、quit() 和 close()的区别

1、quit() 是关闭整个浏览器;而close() 是关闭当前的页面;

2、quit() 操作会清空缓存;close() 不会清空缓存;

2、窗口切换

java 复制代码
 private static void test07() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();//新闻超链接
        //跳转到新页面了
        sleep(3000);

        // 需要先获取所有页面的句柄  否则没找到元素  是因为 默认是在get 打开的页面找元素
        //getWindowHandles()获取所有窗口的句柄      getWindowHandle() 获取get打开的页面的句柄
        Set<String> handles = webDriver.getWindowHandles();
        String targetHandle = "";
        for (String handle : handles) {
            targetHandle = handle;
        }
        webDriver.switchTo().window(targetHandle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("你好");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

3、截图操作

添加依赖:

java 复制代码
<dependency>
	    <groupId>commons-io</groupId>
	    <artifactId>commons-io</artifactId>
	    <version>2.11.0</version>
</dependency>
java 复制代码
 private static void test08() throws InterruptedException, IOException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("F:/1111.png"));

    }
相关推荐
阁老5 小时前
pytest中集成allure打造更好的自动化测试报告
测试
软件测试同学6 小时前
软件测试术语分享: 验收测试驱动开发 | Acceptance Test Driven Development
测试
用户962377954483 天前
VulnHub DC-1 靶机渗透测试笔记
笔记·测试
数据组小组4 天前
免费数据库管理工具深度横评:NineData 社区版、Bytebase 社区版、Archery,2026 年开发者该选哪个?
数据库·测试·数据库管理工具·数据复制·迁移工具·ninedata社区版·naivicat平替
Apifox5 天前
【测试套件】当用户说“我只想跑 P0 用例”时,我们到底在说什么
单元测试·测试·ab测试
阁老7 天前
pytest测试框架:如何确保登录模块先执行并共享登录状态
测试
_志哥_8 天前
Superpowers 技术指南:让 AI 编程助手拥有超能力
人工智能·ai编程·测试
FliPPeDround11 天前
浏览器扩展 E2E 测试的救星:vitest-environment-web-ext 让你告别繁琐配置
e2e·浏览器·测试
Apifox11 天前
Apifox 2 月更新|MCP Client 调试体验优化、测试套件持续升级、支持公用测试数据、测试报告优化
前端·后端·测试
infiniteWei11 天前
Skills、MCP、Agent 的边界与商业化定位(附项目筛选表)
人工智能·aigc·测试