简体   繁体   English

从另一个Docker容器运行Zalenium的Selenium命令

[英]Run Selenium Commands For Zalenium from another Docker Container

Due to my circumstances for the current project I need to use 2 docker images together for selenium testing. 由于我当前项目的情况,我需要一起使用2个docker映像进行硒测试。

One is called Zalenium. 一种叫做Zalenium。 I have it up and working via the docker-compose demo. 我准备好了,并通过docker-compose演示进行了工作。 Its basically like the selenium docker repo except that this can record video and show you live progress. 除了可以录制视频并向您显示实时进度外,其基本类似于硒泊坞窗回购。

zalenium:
        image: "dosel/zalenium"
        container_name: zalenium
        hostname: zalenium
        tty: true
        volumes:
            - /Users/josephastrahan/seluser/videos:/home/seluser/videos
            - /var/run/docker.sock:/var/run/docker.sock
            - /usr/bin/docker:/usr/bin/docker
        ports:
            - 4444:4444
        command: >
          start --chromeContainers 1
                --firefoxContainers 1
                --maxDockerSeleniumContainers 8
                --screenWidth 800 --screenHeight 600
                --timeZone "Europe/Berlin"
                --videoRecordingEnabled true
                --sauceLabsEnabled false
                --browserStackEnabled false
                --testingBotEnabled false
                --startTunnel false
        environment:
          - HOST_UID
          - HOST_GID
          - SAUCE_USERNAME
          - SAUCE_ACCESS_KEY
          - BROWSER_STACK_USER
          - BROWSER_STACK_KEY
          - TESTINGBOT_KEY
          - TESTINGBOT_SECRET

The other image is a NodeJS server pre-installed with Mocha and the nodejs (npm package) of selenium-webdriver so that I can run the commands to trigger browser automation. 另一个图像是预装Mocha和selenium-webdriver的nodejs(npm软件包)的NodeJS服务器,以便我可以运行命令来触发浏览器自动化。 Details on this here ( https://github.com/SeleniumHQ/selenium/tree/master/javascript/node/selenium-webdriver ) and here ( https://hub.docker.com/r/qmu1/selenium-webdriver-node/ ) 此处( https://github.com/SeleniumHQ/selenium/tree/master/javascript/node/selenium-webdriver )和此处( https://hub.docker.com/r/qmu1/selenium-webdriver-节点/

You can see Zalenium running from docker compose just fine here. 您可以在此处看到从docker运行的Zalenium编写得很好。 I can see the time updating every second correctly so its definitely showing a live view. 我可以看到时间正确地每秒更新,因此绝对可以显示实时视图。

在此处输入图片说明

According to the documentation for the other docker container I'm using which is here ( https://hub.docker.com/r/qmu1/selenium-webdriver-node/ ), I should be able to run tests simply with a docker command like this. 根据我正在使用的其他Docker容器的文档( https://hub.docker.com/r/qmu1/selenium-webdriver-node/ ),我应该能够仅使用Docker运行测试这样的命令。

HERE=$(pwd)

echo ""
echo "----------------------------"
echo "Running an example"
echo "----------------------------"
echo ""

docker run -it --rm \
    -v $HERE:/workspace \
    qmu1/selenium-webdriver-node:latest /bin/sh -c "
        mocha sample.js --timeout=10000
"

I changed this command to fit my needs until it seemed liked it worked. 我更改了此命令以适应我的需要,直到看起来好像它起作用为止。

docker run -it --rm --net=distributiontech_default -v $HERE:/workspace qmu1/selenium-webdriver-node:latest /bin/sh -c "mocha tests/seleniumtest.js --timeout=10000"

I got the response: 我得到了回应:

all modules are ready!


  0 passing (1ms)

Problem is... on Zalenium I didn't see anything happen on the viewer to verify it was even working? 问题是...在Zalenium上,我没有看到查看器上发生任何事情以验证其是否正常运行?

My selenium script looks like below. 我的硒脚本如下所示。

//Run using this project (https://github.com/qmu/dockerfiles/blob/master/src/selenium-webdriver-node/example/bin/run)

"use strict";

const webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until,
    test = require('selenium-webdriver/testing');

const expect = require('expect.js');
const assert = require('assert');

// var driver = new webdriver.Builder()
//    .withCapabilities(webdriver.Capabilities.chrome())
//    .usingServer('http://localhost:4444/wd/hub')
//    .build();

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .usingServer('http://zalenium:4444/wd/hub')
    .build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('simple programmer');
driver.findElement(webdriver.By.name('btnG')).click();
//driver.quit();

console.log('all modules are ready!');

I tried without the --net command and with... and no luck. 我尝试了没有--net命令并且没有运气。 Just so you can see the network details and containers running. 这样您就可以看到网络详细信息和正在运行的容器。 You can see that zalenium is added to the distributiontech_default network. 您可以看到zalenium已添加到distributiontech_default网络。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

How do I connect the docker container running the selenium code to the docker hub running Zalenium? 如何将运行Selenium代码的Docker容器连接到运行Zalenium的Docker Hub?

I tried changing this to... 我尝试将其更改为...

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .usingServer('http://localhost:4444/wd/hub')
    .build();

but no luck either. 但也没有运气。 I can put any fake address I want where it says localhost and it doesn't seem to throw any errors or anything either oddly enough. 我可以将任何我想要的假地址放在它表示本地主机的地方,它似乎不会引发任何错误或任何奇怪的事情。

Well I have no idea what I did exactly, but one of these steps got it to work all the sudden. 好吧,我不知道我到底做了什么,但是其中一个步骤使它突然起作用。 I think it was the way I was writing the script. 我认为这是我编写脚本的方式。

First to do the mocha test with the docker container here is the command I used. 首先使用docker容器执行mocha测试,这是我使用的命令。

docker run -it --rm --net=distributiontech_main -v $HERE:/workspace qmu1/selenium-webdriver-node:latest /bin/sh -c "mocha tests/seleniumtest.js --timeout=10000"

Note that you will want to be on the correct --net (network) and volume the correct location and use correct .js filepath for yourself. 请注意, 您将希望位于正确的--net(网络)上,为其设置正确的位置,并使用正确的.js文件路径。

My composer file I was using I added to the very bottom 我使用的作曲家文件已添加到最底部

networks:
    main:

Then for each service I put... 然后我为每项服务...

networks:
            - main

The network name will default to the name your project is in with an _ and the name you define in the composer file. 网络名称将默认为项目所在的名称(带有_)和您在作曲家文件中定义的名称。 Use the command docker network ls to view your current networks. 使用命令docker network ls查看当前网络。 You can clear old unused ones with docker network rm networknamehere , just don't remove bridge, host or none as they are defaults. 您可以在docker network rm networknamehere使用docker network rm networknamehere清除旧的未使用的,只是不要删除网桥,主机或都不删除,因为它们是默认设置。

This step to put on the networks might not be necessary since one is created for you by default from docker compose. 由于默认情况下会从docker compose为您创建一个网络,因此可能无需执行这一步骤。 You can see it by using the docker network ls command. 您可以使用docker network ls命令查看它。

An Example selenium file that worked for me was this... 一个对我有用的示例硒文件是这个...

"use strict";

var webdriver = require('selenium-webdriver'),
        { describe, it, after, before } = require('selenium-webdriver/testing'),
        By = webdriver.By,
        until = webdriver.until;
        var driver;

     describe('Login Test', function () {
            this.timeout(50000);//50 seconds

            beforeEach(function(){
                driver = new webdriver.Builder()
                     .withCapabilities({'browserName': 'chrome','name':'Chrome Test','tz':'America/Los_Angeles','build':'Chrome Build','idleTimeout':'60'})
                     .usingServer('http://zalenium:4444/wd/hub')
                     .build();

                driver.get('http://distribution.tech.sel');

            });

            afterEach(function(){
                    driver.quit();
            });

            //this.timeout(20000);

          it('Should show welcome message after entering credentials', function () {
                driver.findElement(By.name('_username')).sendKeys('**');
                driver.findElement(By.name('_password')).sendKeys('**');
                driver.findElement(By.css("button")).click();

          });

            it('The title should say something interesting', function () {

          });

            it('And this thing too...', function () {

          });
});

Notice the key here is I refer to zalenium which is not on the same network, .usingServer('http://zalenium:4444/wd/hub') , this resolves the ip address automatically and the script is able to find the zalenium server easily. 注意这里的关键是我指的是不在同一网络上的.usingServer('http://zalenium:4444/wd/hub') ,这会自动解析ip地址,并且脚本能够找到zalenium服务器轻松。

The other thing is since I have my website on another container I added this to make accessing it easier in the composer file. 另一件事是,因为我的网站位于另一个容器中,所以我添加了它以使在composer文件中访问起来更加容易。

networks:
            main:
                aliases:
                    - distribution.tech.sel

This replaces 取代

networks:
-main

It creates an alias so you can use a domain name to refer to the project when accessing it. 它创建了一个别名,因此您可以在访问项目时使用域名来引用该项目。 Technically in my composer file I called the service 'web' and I could access it that way, but my site uses the domain name information to decide if its in beta or dev mode so it was important to give a proper domain when connecting to it in my case. 从技术上讲,在我的作曲家文件中,我将服务称为“ web”,并且可以通过这种方式访问​​它,但是我的网站使用域名信息来确定其是以beta还是dev模式,因此在连接到它时提供适当的域非常重要就我而言。

Hope this helps someone. 希望这对某人有帮助。

Example entire dockercompose.yml file 示例整个dockercompose.yml文件

version: "3"
services:
    dblive:
        image: mysql:5.5.52
        volumes:
            - ./db_data_live:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: **
            MYSQL_DATABASE: **
            MYSQL_USER: **
            MYSQL_PASSWORD: **
        networks:
            - main

    dbdev:
        image: mysql:5.5.52
        volumes:
            - ./db_data_dev:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: **
            MYSQL_DATABASE: **
            MYSQL_USER: **
            MYSQL_PASSWORD: **
        networks:
            - main

    phpmyadmin:
        depends_on:
            - dblive
            - dbdev
        image: phpmyadmin/phpmyadmin
        environment:
            PMA_ARBITRARY : 1
        restart: always
        ports:
            - "8081:80"
        networks:
            - main

    zalenium:
        image: "dosel/zalenium"
        container_name: zalenium
        hostname: zalenium
        tty: true
        volumes:
            - /Users/josephastrahan/seluser/videos:/home/seluser/videos
            - /var/run/docker.sock:/var/run/docker.sock
            - /usr/bin/docker:/usr/bin/docker
        ports:
            - 4444:4444
        command: >
          start --chromeContainers 2
                --firefoxContainers 2
                --maxDockerSeleniumContainers 8
                --screenWidth 800 --screenHeight 600
                --timeZone "America/Los_Angeles"
                --videoRecordingEnabled true
                --sauceLabsEnabled false
                --browserStackEnabled false
                --testingBotEnabled false
                --startTunnel false
        environment:
          - HOST_UID
          - HOST_GID
          - SAUCE_USERNAME
          - SAUCE_ACCESS_KEY
          - BROWSER_STACK_USER
          - BROWSER_STACK_KEY
          - TESTINGBOT_KEY
          - TESTINGBOT_SECRET
        networks:
            main:
                aliases:
                    - zalenium.test

    web:
        #build: ./
        depends_on:
            - dblive
            - dbdev
            - zalenium
        image: **
        volumes:
            - ./web:/var/www
            - ./config/custom.php.ini:/etc/php5/apache2/conf.d/custom.php.ini
            - ./logs/apache_error.log:/var/log/apache2/error.log
            - ./logs/apache_access.log:/var/log/apache2/access.log
            - ./config/apache_default.conf:/etc/apache2/sites-available/000-default.conf
            - ./config/apache_default-ssl.conf:/etc/apache2/sites-available/default-ssl.conf
            - ./config/ssl/apache.key:/etc/apache2/ssl/apache.key
            - ./config/ssl/apache.crt:/etc/apache2/ssl/apache.crt
            - ./web/public_html/livesite:/app
            - ./web/public_html/devsite:/appdev
        restart: always
        ports: 
            #Standard HTTP Port
            - "80:80"
            #Below allows access to local computer from outside ports
            - "8080:80"
            #Standard SSH Port
            - "443:443"
            #Below allows access to local computer from outside ports for SSH Testing
            - "4443:443"
        extra_hosts:
            - "distribution.tech:127.0.0.1"
            - "dev.distribution.tech:127.0.0.1"
            - "distribution.tech.local:127.0.0.1"
            - "dev.distribution.tech.local:127.0.0.1"
        networks:
            main:
                aliases:
                    - distribution.tech.sel
        # external_links:
        #     - web:dev.distribution.tech
        #     - web:distribution.tech
networks:
    main:

我有一个类似的问题,我发现我可以使用--link命令,以便将一个命名的Zalenium网格实例与执行我的测试的docker实例链接起来:

docker run --privileged --name stg-selenium-client --rm=true --link zalenium:hub hub.platformservices.io/sbg_core_automation/core-test-runner-with-maven-dependencies:0.12 <command to execute tests>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM