docker selenium / standalone-chrome无法连接到docker Web服务器

时间:2018-08-24 12:46:59

标签: docker selenium-webdriver docker-compose codeception

我正在尝试使用代码接收对使用docker容器开发的php网站运行测试。 我在Web容器中创建了一个测试文件夹,并在其中放置了codecept.phar。

这是项目的设置:

  • docker-compose.yml:

    version: '3'
      services:
        db:
          image: mariadb
          restart: always
          volumes:
            - ./db:/var/lib/mysql
          ports:
            - '3306:3306'
          environment:
            MYSQL_ROOT_PASSWORD: root
        web:
          build: .
          restart: always
          tty: true
          volumes:
            - ./src:/var/www
            - ./build/php.ini:/usr/local/etc/php/php.ini
          ports:
            - '80:80'
          depends_on:
            - db
        chrome:
          image: selenium/standalone-chrome
          restart: always
          ports:
            - '4444:4444'
            - '5900:5900'
          depends_on:
            - web
    
  • acceptance.suite.yml

    actor: AcceptanceTester
    modules:
      enabled:
        - WebDriver:
            url: web
            host: chrome
            browser: chrome
            wait: 15
            window_size: false
        - \Helper\Acceptance
    

我以以下内容启动容器:

docker-compose up

然后将shell附加到Web容器并使用以下命令运行测试:

php codecept.phar build && php codecept.phar run --steps

我正在运行一个简单的测试,该测试基本上试图检查元素是否存在并拍摄屏幕截图:  -test1.php:

$I = new AcceptanceTester($scenario);
$I->amOnUrl('http://127.0.0.1');
$I->makeScreenshot();
$I->waitForElement(".modal");

但是测试无法正常运行,因为Chrome容器无法连接到Web容器。屏幕截图显示了一个页面,显示:

This site can't be reached
127.0.0.1 refused to connect

这是附件的运行代码接收的shell中显示的错误:

[Facebook\WebDriver\Exception\NoSuchElementException] no such element: Unable to locate element: {"method":"css selector","selector":".modal"}
(Session info: chrome=68.0.3440.84)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.9.0-7-amd64 x86_64)

尽管我可以连接到http://127.0.0.1:4444/wd/hub

这是我从运行“ docker-compose up”的外壳程序获得的输出:

chrome_1 | INFO [GridLauncherV3.launch] - Selenium build info: version: '3.14.0', revision: 'aacccce0'
chrome_1 | INFO [GridLauncherV3$1.launch] - Launching a standalone Selenium Server on port 4444
chrome_1 | INFO::main: Logging initialized @286ms to org.seleniumhq.jetty9.util.log.StdErrLog
chrome_1 | INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
chrome_1 | INFO [ActiveSessionFactory.apply] - Capabilities are: {"browserName": "chrome"}
chrome_1 | INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | Starting ChromeDriver 2[.14513.5517089780809 .(328f61]e[dS5EfV9E3R4E3]c:1 3bfi7n3d1(4)4 5r3e8tfu1r5nce0d0 ba3n7 0eerdrao6r7,0 6e) onr rpnoor=t9 91:7 2C9a4n
chrome_1 | Onnloyt  laoscsailg nc ornenqeucetsitoends  aadrder easlsl o(w9e9d).
chrome_1 | INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
chrome_1 | INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session ed60fb03c497f98a7e23bdede05c4bb9 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | INFO [ActiveSessions$1.onStop] - Removing session ed60fb03c497f98a7e23bdede05c4bb9 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | INFO [ActiveSessionFactory.apply] - Capabilities are: {"browserName": "chrome"}
chrome_1 | INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 17381
chrome_1 | Only local connections are allowed.
chrome_1 | [1535110012.491][SEVERE]: bind() returned an error, errno=99: Cannot assign requested address (99)
chrome_1 | INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
chrome_1 | INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session 3c49c360624e02460995193c50f43bd3 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | INFO [ActiveSessions$1.onStop] - Removing session 3c49c360624e02460995193c50f43bd3 (org.openqa.selenium.chrome.ChromeDriverService)

我认为为docker-compose容器建立网络应该可以解决问题。 我尝试遵循docker文档(Network configuration reference)将网络设置为“ host”,但是它似乎已经过时了,因为版本3中不允许使用名称。

还尝试设置从chrome到Web的链接,并在不撰写内容的情况下运行chrome(docker run --net = host selenium / standalone-chrome),但这没有任何改变。

您知道一种使这项工作有效的方法吗? 感谢您的帮助!

4 个答案:

答案 0 :(得分:0)

您是否在docker-compose中尝试过network_mode: "host"

答案 1 :(得分:0)

尝试通过设置JAVA_OPTS

允许非本地主机连接到chrome
    chrome:
      image: selenium/standalone-chrome
      restart: always
      ports:
        - '4444:4444'
        - '5900:5900'
      environment:
        - JAVA_OPTS=-Dwebdriver.chrome.whitelistedIps=
      depends_on:
        - web

答案 2 :(得分:0)

解决方案很简单。

请参阅此Using selenium/standalone-chrome in docker-compose connecting with Python's selenium

您需要像这样设置网络

version: '3.8'

networks:
    web:
      external: true
      driver:  bridge

services:
    chrome:
        image: selenium/standalone-chrome:latest
        hostname: chrome
        networks:
          - web
        privileged: true
        shm_size: 2g

    framework:
        build: .
        networks:
            - web
        depends_on: 
            - chrome

根据您的情况,可以根据需要进行修改。请记住,当您在docker中运行代码时,只能在http://chrome:4444/进行连接。

答案 3 :(得分:0)

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>


#define NUMTHRDS 2
pthread_t t [ NUMTHRDS];
pthread_mutex_t m1, m2; 
int x = 0; 

void *thread1(void *arg){
        pthread_mutex_lock(&m1);
        x = (rand() % 10) + 1; // generates a random number between 1 and 10    
        printf("generator >> x = %d\n" , x);
        // 2- unlock the critical section of the printing thread
        pthread_mutex_unlock(&m2);
}


void * thread2(void * arg){
        pthread_mutex_lock(&m2);
        printf("printer >> x = %d\n" , x);
        pthread_mutex_unlock(&m1);
}

int main(void) 
{   
    srand(time(NULL));

    pthread_mutex_init(&m1, NULL);
    pthread_mutex_init(&m2, NULL);
    // 1- locking the critical section of the printing thread
    pthread_mutex_lock(&m2);
    for(int i = 0 ; i < 5 ; i++) {
        pthread_create(&t[1], NULL, thread1, NULL);
        pthread_create(&t[0], NULL, thread2, NULL);
    }

    pthread_exit(NULL);

    pthread_mutex_destroy(&m1);
    pthread_mutex_destroy(&m2);

    return 0;

}

硒无法到达您的Web服务器的原因是它在其他容器上而不是在同一容器上,因此要解决此问题,您需要提供特定容器和端口的名称,例如:

This site can't be reached
127.0.0.1 refused to connect

但是我仍然持怀疑态度,因为您已经映射了主机端口80和您的容器端口80,因此,如果上述方法不起作用,请尝试

$I->amOnUrl('http://web:80'); 

此处docker.host.internal将引用您的主机系统端口80,因此既然您将其映射到Web容器,它肯定可以工作。让我知道这是否适合您