Selenium - SessionNotCreated 无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败

问题描述 投票:0回答:1

我在将 selenium 与 docker 和 azure pipeline 集成时遇到此错误。

SessionNotCreated 无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败

Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: chrome, goog:chromeOptions: {args: [--disable-notifications], extensions: []}}]}]
Capabilities {acceptInsecureCerts: true, browserName: chrome, goog:chromeOptions: {args: [--disable-notifications], extensions: []}}
[ERROR]   SampleRun.setup:68 » SessionNotCreated Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Host info: host: 'fv-az369-95', ip: '10.1.0.4'
Build info: version: '4.19.1', revision: 'abe0ee07dc'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.5.0-1018-azure', java.version: '11.0.22'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: MicrosoftEdge, ms:edgeOptions: {args: [], extensions: []}}]}]
Capabilities {acceptInsecureCerts: true, browserName: MicrosoftEdge, ms:edgeOptions: {args: [], extensions: []}}
[INFO] 
[ERROR] Tests run: 6, Failures: 2, Errors: 0, Skipped: 4
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  47.179 s
[INFO] Finished at: 2024-04-25T02:15:53Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test (default-test) on project DTest: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/vsts/work/1/s/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

##[debug]Exit code 1 received from tool '/usr/bin/bash'
##[debug]STDIO streams have closed for tool '/usr/bin/bash'
##[error]Bash exited with code '1'.
##[debug]Processed: ##vso[task.issue type=error;source=TaskInternal;]Bash exited with code '1'.
##[debug]task result: Failed
##[debug]Processed: ##vso[task.complete result=Failed;done=true;]
Finishing: Maven clean install

Azure-pipeline.yml:

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'
  
variables:
  java.version: '16'

jobs:
- job: 'Setup'
  displayName: 'Setup Environment'
  steps:
  - script: |
      sudo apt-get update
      sudo apt-get install -y openjdk-$(java.version)-jdk
      echo "Java version:"
      java -version
    displayName: 'Set up Java'

  - script: |
      sudo apt-get remove containerd
    displayName: 'Remove conflicting packages'

  - script: |
      sudo apt-get update
      sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
      echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      sudo apt-get update
      sudo apt-get install -y docker-ce docker-ce-cli containerd.io
    displayName: 'Install Docker from official repository'

  - script: |
      sudo apt-get update
      sudo apt-get install -y maven
      mvn -version
    displayName: 'Install Maven'

  - script: |
      sudo apt-get update
      sudo apt-get install -y unzip
      wget https://selenium-release.storage.googleapis.com/4.18/selenium-java-4.18.1.zip
      unzip selenium-java-4.18.1.zip -d selenium-java
      cp selenium-java/libs/* .
      rm -rf selenium-java selenium-java-4.18.1.zip
    displayName: 'Install Selenium Java dependencies'

- job: 'Docker'
  displayName: 'Run Docker Containers'
  dependsOn: Setup
  steps:
  - script: |
      docker run -d -p 4442:4442 -p 4443:4443 --shm-size=2g \
        -e SE_EVENT_BUS_HOST=selenium-hub \
        -e SE_EVENT_BUS_PUBLISH_PORT=4442 \
        -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
        -e VNC_NO_PASSWORD=1 \
        -e SE_NODE_MAX_SESSIONS=2 \
        --name chrome selenium/node-chrome:4.18.1-20240224
    displayName: 'Run Chrome Node Docker container'

  - script: |
      docker run -d -p 4444:4444 -p 4445:4445 --shm-size=2g \
        -e SE_EVENT_BUS_HOST=selenium-hub \
        -e SE_EVENT_BUS_PUBLISH_PORT=4444 \
        -e SE_EVENT_BUS_SUBSCRIBE_PORT=4445 \
        -e VNC_NO_PASSWORD=1 \
        -e SE_NODE_MAX_SESSIONS=2 \
        --name edge selenium/node-edge:4.18.1-20240224
    displayName: 'Run Edge Node Docker container'

  - script: |
      docker run -d -p 4446:4446 -p 4447:4447 -p 4448:4448 \
        --name selenium-hub selenium/hub:4.18.1-20240224
    displayName: 'Run Selenium Hub Docker container'

- job: 'Tests'
  displayName: 'Run Tests'
  dependsOn: Docker
  steps:
  - script: |
      mvn clean install
    displayName: 'Maven clean install'

  - script: |
      mvn test
    displayName: 'Run Selenium tests'

主要硒文件:

public class SampleRun {
    

    private static ThreadLocal<WebDriver> driverThreadLocal = new ThreadLocal<>();

    public WebDriver getDriver() {
        return driverThreadLocal.get();
    }

    public void setDriver(WebDriver driver) {
        driverThreadLocal.set(driver);
    }

    public void quitDriver() {
        WebDriver driver = driverThreadLocal.get();
        if (driver != null) {
            driver.quit();
            driverThreadLocal.remove();
        }
    }

    @BeforeTest
    @Parameters({ "browser" })
    public void setup(String browser) throws MalformedURLException {

        URL hubUrl = new URL("http://localhost:4444/wd/hub");

        switch (browser.toLowerCase()) {
        case "chrome":
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--disable-notifications"); // Disable browser notifications
            chromeOptions.setAcceptInsecureCerts(true); // Accept insecure certificates
            setDriver(new RemoteWebDriver(hubUrl, chromeOptions));
            break;
        case "edge":
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions.setAcceptInsecureCerts(true);
            setDriver(new RemoteWebDriver(hubUrl, edgeOptions));
            break;
        default:
            throw new IllegalArgumentException("Invalid browser: " + browser);
        }

        if (getDriver() == null) {
            throw new IllegalStateException("WebDriver is not initialized properly.");
        }

        System.out.println("WebDriver initialized for " + browser);
    }

    @Test
    public void testScenario1() throws InterruptedException {
        getDriver().get("https://www.google.com");
        Thread.sleep(2000);
        System.out.println("Page title: " + getDriver().getTitle());
    }

    @Test
    public void testScenario2() throws InterruptedException {
        getDriver().get("https://www.facebook.com");
        Thread.sleep(2000);
        System.out.println("Page title: " + getDriver().getTitle());
    }

    @Test
    public void testScenario3() throws InterruptedException {
        getDriver().get("https://www.reddit.com");
        Thread.sleep(2000);
        System.out.println("Page title: " + getDriver().getTitle());
    }

    @AfterTest
    public void teardown() {
        quitDriver();
    }
}

如果需要更改,请告诉我。

selenium-webdriver docker-compose selenium-chromedriver azure-pipelines selenium-grid
1个回答
0
投票

根据官方文档代理池工作

使用 Microsoft 托管的代理时,管道中的每个作业都会获得一个新的代理。

对于您的“测试”作业,它在全新的 ubuntu 最新代理上运行,而不是在“Docker”作业中设置的 docker 容器上运行。尝试在一项作业中运行这些步骤。

© www.soinside.com 2019 - 2024. All rights reserved.