复用浏览器
简介
在 Web 自动化测试中,浏览器复用是指将已打开的浏览器实例用于多个测试用例。这可以显著提高测试效率和性能,并减少资源消耗。浏览器复用通常与浏览器驱动程序(如 Selenium WebDriver)一起使用,以便更好地管理浏览器窗口和标签页。常见的浏览器复用场景如下:
复用浏览器应用场景
- 在运行 Selenium 自动化时,通常要求在成功扫码登陆后才能执行后续操作。为了提高效率,可以在脚本运行之前先进行扫码登录,并在运行脚本时复用已经打开的浏览器窗口。 
- 当调试了某个步骤很多的测试用例,前面的 N-1 步骤已经成功执行,只需调试第 N 步。为了避免重新运行整个脚本造成耗时过多,这时我们可以直接复用浏览器只操作第 N 步。 
- 复用浏览器的特点在于, webdriver 在启动时不会创建新的浏览器窗口,而是重用已打开的浏览器的当前页面,使得可以对元素进行进一步的操作。这种方式可以显著提高测试脚本的执行效率。 
浏览器复用的优点
- 节省时间:启动和关闭浏览器通常需要一定的时间。通过复用浏览器,可以减少这些开销,从而更快地执行测试用例。 
- 资源优化:每个浏览器实例都需要占用计算机资源,包括内存。通过复用浏览器,可以降低资源消耗。 
- 更高效的内存管理:浏览器复用有助于更有效地管理浏览器的内存,因为每次启动浏览器时,它会加载并初始化一个新的浏览器进程。 
使用和未使用复用浏览器流程如图所示:
复用已有浏览器-配置步骤
- 需要退出当前所有的谷歌浏览器(特别注意)。 
- 输入启动命令,通过命令启动谷歌浏览器 
- 找到 chrome 的启动路径 
- 配置环境变量 
- windows:chrome --remote-debugging-port=9222 
- mac:Google\ Chrome --remote-debugging-port=9222 
- 验证是否启动成功 
- 访问浏览器查看浏览器是否启动 http://localhost:9222/ 
windows 关闭谷歌浏览器进程
windows 环境变量配置
1. 获取启动路径
2. 配置环境变量
3. 重启命令行
4. 验证
访问 http://localhost:9222/
Mac 环境变量配置
- 获取启动路径(注意:使用 tab 键,不要手动输入)。 
- 将启动路径配置到环境变量中。 
 # 举例,不要生搬硬套exportPATH=$PATH:/Applications/Google\ Chrome.app/Contents/MacOS
   复制代码
 复用已有浏览器-代码设置
Python 实现
 from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.by import By
option = Options()option.debugger_address = "localhost:9222"driver = webdriver.Chrome(options=option)driver.implicitly_wait(10)driver.get("https://work.weixin.qq.com/wework_admin/frame")# 人工扫码time.sleep(10)# 点击通讯录driver.find_element(By.XPATH,'//*[text()="通讯录"]').click()
   复制代码
 Java 实现
 importorg.junit.jupiter.api.AfterAll;importorg.junit.jupiter.api.BeforeAll;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.chrome.ChromeOptions;publicclass web_useAgainTest{staticWebDriverdriver;@BeforeAllstaticvoidsetup(){ChromeOptionschromeOptions=newChromeOptions();chromeOptions.setExperimentalOption("debuggerAddress","localhost:9222");driver=newChromeDriver(chromeOptions);
}@AfterAllstaticvoidteardown(){driver.quit();
}
@Testvoidremote2()throwsInterruptedException{
driver.get("https://work.weixin.qq.com/wework_admin/frame");//人工扫码Thread.sleep(30000);
WebElementelement=driver.findElement(By.xpath("//*[@class ='index_service_cnt_itemWrap']"));element.click();Thread.sleep(1000);}
}
   复制代码
 使用复用浏览器,只需要扫码登陆一次,只要浏览器窗口不关闭,就可以一直使用,从而避免每次打开都需要扫码。
调试代码
Python 实现
 from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.by import By
option = Options()option.debugger_address = "localhost:9222"driver = webdriver.Chrome(options=option)driver.implicitly_wait(10)# driver.get("https://work.weixin.qq.com/wework_admin/frame")# 人工扫码# time.sleep(10)# driver.find_element(By.XPATH,'//*[text()="通讯录"]').click()# 点击添加成员driver.find_elements(By.XPATH,'//*[text()="添加成员"]')[1].click()
   复制代码
 Java 实现
 importorg.junit.jupiter.api.AfterAll;importorg.junit.jupiter.api.BeforeAll;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.chrome.ChromeOptions;staticWebDriverdriver;@BeforeAllstaticvoidsetup(){ChromeOptionschromeOptions=newChromeOptions();chromeOptions.setExperimentalOption("debuggerAddress","localhost:9222");
}@AfterAllstaticvoidteardown(){driver.quit();
}
@Testvoidremote2()throwsInterruptedException{
driver=newChromeDriver(chromeOptions);
WebElementelement=driver.findElement(By.xpath("//*[text()='添加成员'][1]"));
element.click();Thread.sleep(1000);}
   复制代码
 如果需要在通讯录页面继续进行点击添加成员的操作,可以将打开界面和点击通讯录的操作注释,编写要进行的操作。
总结
复用浏览器是指在启动 selenium 程序时,浏览器不另外打开一个新的页面,而是直接使用现有的浏览器页面,并进行操作。
评论