JAVA+Docker部署分布式 UI 自动化测试环境

部署分布式 UI 自动化测试环境

pom.xml 依赖

    <properties>
        <!-- selenium版本选3.141.59 -->
        <selenium.version>3.141.59</selenium.version>
        <junit.jupiter.version>5.8.1</junit.jupiter.version>
    </properties>
        <dependencyManagement>
        <dependencies>
            <!--当使用 Gradle 或 Maven 引用多个 JUnit 工件时,此物料清单 POM 可用于简化依赖项管理。不再需要在添加依赖时设置版本-->
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit.jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
        <dependencies>
        <!-- junit5 -->
        <!-- 创建 Junit5 测试用例的 API-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <!--对应添加的依赖的作用范围-->
            <scope>test</scope>
        </dependency>
        <!-- 兼容 JUnit4 版本的测试用例-->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </dependency>
        <!--suite套件依赖 -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>${selenium.version}</version>
        </dependency>
    </dependencies>

部署selenium-grid:

docker镜像版本选择: 4.1.1-20220121

docker run -d \
--name selenium_hub \
 --privileged=true \
-p 5442-5444:4442-4444 \
selenium/hub:4.1.1-20220121

docker run -d --name node_chrome --shm-size="2g" -p 5902:5900 \
-e SE_EVENT_BUS_HOST=192.168.3.11 \
-e SE_NODE_MAX_SESSIONS=20 \
-e NODE_MAX_INSTANCES=20 \
-e SE_NODE_OVERRIDE_MAX_SESSIONS=true \
-e SE_EVENT_BUS_PUBLISH_PORT=5442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=5443 \
-v ~/docker/shm:/dev/shm selenium/node-chrome:4.1.1-20220121

vnc 密码:secret

selenium 连接并启动chrome

package com.ceshiren;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * @Author chenqiang
 * @create 2023/11/16 23:35
 */
public class SeleniumGridDemoTest {

    public static void main(String[] args) {
/*        String webDriverName = "webdriver.chrome.driver";
        String webDriverPath ="/opt/selenium/chromedriver";
        System.setProperty(webDriverName, webDriverPath);*/
        String gridURL = "http://192.168.3.11:5444/wd/hub";

        DesiredCapabilities caps = DesiredCapabilities.chrome();
        caps.setCapability("gsg:customcap",true);
        caps.setCapability("se:recordVideo",false);
        caps.setBrowserName(BrowserType.CHROME);
        caps.setPlatform(Platform.LINUX);
        caps.setVersion("97.0");
        caps.setCapability("--headless",true);
        caps.setCapability("--remote-allow-origins=*",true);
        try {
            WebDriver driver = new RemoteWebDriver(new URL(gridURL), caps);
            driver.manage().window().maximize();

            driver.get("https://www.baidu.com");
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.quit();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

    }

}

selenium 连接并启动FireFox

// todo