Skip to content

复用浏览器

复用浏览器

简介

在 Web 自动化测试中,浏览器复用是指将已打开的浏览器实例用于多个测试用例。 这可以显著提高测试效率和性能,并减少资源消耗。浏览器复用通常与浏览器驱动程序(如 Selenium WebDriver)一起使用,以便更好地管理浏览器窗口和标签页。常见的浏览器复用场景如下:

  • 多个测试用例复用同一个浏览器实例: 在自动化测试中,你可以创建一个浏览器实例,然后在多个测试用例之间共享它,而不必为每个测试用例启动和关闭浏览器。这可以加速测试执行并减少资源消耗。
  • 多个标签页或窗口: 浏览器复用还可以用于在同一浏览器实例中打开多个标签页或窗口,并在它们之间切换。这在某些测试场景下非常有用,例如在一个标签页中执行登录,然后在另一个标签页中执行其他操作。

服用浏览器应用场景

  1. 在运行 Selenium 自动化时,通常要求在成功扫码登陆后才能执行后续操作。为了提高效率,可以在脚本运行之前先进行扫码登录,并在运行脚本时复用已经打开的浏览器窗口。
  2. 当调试了某个步骤很多的测试用例,前面的 N-1 步骤已经成功执行,只需调试第 N 步。为了避免重新运行整个脚本造成耗时过多,这时我们可以直接复用浏览器只操作第 N 步。
  3. 复用浏览器的特点在于, webdriver 在启动时不会创建新的浏览器窗口,而是重用已打开的浏览器的当前页面,使得可以对元素进行进一步的操作。这种方式可以显著提高测试脚本的执行效率。

浏览器复用的优点

  • 节省时间: 启动和关闭浏览器通常需要一定的时间。通过复用浏览器,可以减少这些开销,从而更快地执行测试用例。
  • 资源优化: 每个浏览器实例都需要占用计算机资源,包括内存。通过复用浏览器,可以降低资源消耗。
  • 更高效的内存管理: 浏览器复用有助于更有效地管理浏览器的内存,因为每次启动浏览器时,它会加载并初始化一个新的浏览器进程。

使用和未使用复用浏览器流程如图所示:

复用已有浏览器-配置步骤

  1. 需要退出当前所有的谷歌浏览器(特别注意)。

  2. 输入启动命令,通过命令启动谷歌浏览器

    • 找到 chrome 的启动路径
    • 配置环境变量
    • windows:chrome --remote-debugging-port=9222
    • mac:Google\ Chrome --remote-debugging-port=9222
  3. 验证是否启动成功

    • 访问浏览器查看浏览器是否启动 http://localhost:9222/

windows 关闭谷歌浏览器进程

img.png

windows 环境变量配置

1. 获取启动路径

img.png

2. 配置环境变量

img.png

3. 重启命令行

4. 验证

访问 http://localhost:9222/

Mac 环境变量配置

  1. 获取启动路径(注意:使用 tab 键,不要手动输入)。
  2. 将启动路径配置到环境变量中。
# 举例,不要生搬硬套
export PATH=$PATH:/Applications/Google\ Chrome.app/Contents/MacOS

复用已有浏览器-代码设置

Python 实现

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from 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 实现

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class web_useAgainTest {
    static WebDriver driver;
    @BeforeAll
    static void setup(){
        ChromeOptions chromeOptions =new ChromeOptions();
        chromeOptions.setExperimentalOption("debuggerAddress","localhost:9222");
        driver=new ChromeDriver(chromeOptions);



    }
    @AfterAll
    static void teardown(){
      driver.quit();

    }


    @Test
    void remote2() throws InterruptedException {

        driver.get("https://work.weixin.qq.com/wework_admin/frame");
        //人工扫码
        Thread.sleep(30000);

        WebElement element = driver.findElement(By.xpath("//*[@class ='index_service_cnt_itemWrap']"));
        element.click();
        Thread.sleep(1000);
    }

}

使用复用浏览器,只需要扫码登陆一次,只要浏览器窗口不关闭,就可以一直使用,从而避免每次打开都需要扫码。

调试代码

Python 实现

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from 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 实现

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 static WebDriver driver;
    @BeforeAll
    static void setup(){
      ChromeOptions chromeOptions =new ChromeOptions();
        chromeOptions.setExperimentalOption("debuggerAddress","localhost:9222");


    }
    @AfterAll
    static void teardown(){
      driver.quit();

    }


    @Test
    void remote2() throws InterruptedException {

        driver=new ChromeDriver(chromeOptions);

        WebElement element = driver.findElement(By.xpath("//*[text()='添加成员'][1]"));

        element.click();
        Thread.sleep(1000);
    }

如果需要在通讯录页面继续进行点击添加成员的操作,可以将打开界面和点击通讯录的操作注释,编写要进行的操作。

总结

复用浏览器是指在启动 selenium 程序时,浏览器不另外打开一个新的页面,而是直接使用现有的浏览器页面,并进行操作。