自动化测试

自动化测试

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"));

    }
相关推荐
ClouGence2 小时前
2026 年 AI 自动化测试工具盘点,哪些真正值得用?
测试
LabVIEW开发3 天前
CTM-3 红外测温仪 + LabVIEW 温度怎么测?
算法·labview·测试·labview程序
用户766918404483 天前
告别 F12 与 Charles:如何打造一款「懂业务」的浏览器原生抓包利器?
测试
LabVIEW开发3 天前
CMT 矢量网络分析仪 + LabVIEW 测S参数?
labview·测试·labview程序
flash俊杰4 天前
pdf.js detach 血案:structuredClone transfer 如何吃掉你的 ArrayBuffer
测试
狂师4 天前
GitHub上开源真机测试工具mobile-mcp,说句话就能让AI在手机上跑用例!
人工智能·开源·测试
三翼鸟数字化技术团队5 天前
测试人员代码阅读实战指南
测试
得物技术5 天前
Jev 实战:为AI工作流引入轻量决策层 x10倍提速|得物技术
python·llm·测试
挖掘狂人5 天前
AI 代码上线前,真正的瓶颈不在 "写",而在 "验"
ai编程·测试·代码规范