简体   繁体   English

带有 selenium 和 docker 的 Github Action

[英]Github Action with selenium and docker

I am currently working on a CI pipeline for a project and just started setting up a Github Action for running integration tests but I can't get it to work.我目前正在为一个项目开发 CI 管道,刚刚开始设置一个 Github Action 来运行集成测试,但我无法让它工作。

My action looks like this:我的动作是这样的:

name: Integration Tests

on:
  push:
    branches: 
      - main

  workflow_dispatch:

jobs:
  integration-tests:
    runs-on: ubuntu-latest

    services:
      selenium:
        image: selenium/standalone-chrome:latest
        ports: 
          - 4444:4444
        options: --shm-size="2g"


    steps:
    - uses: actions/checkout@v2
    - name: Get IP Address
      run: echo "##[set-output name=ip;]$(ifconfig eth0 | grep 'inet [0-9\.]* ' -o | sed 's/[^0-9\.]//g')"
      id: ip_addr 
      
    - name: Setup Pyhon
      uses: actions/setup-python@v2
      with:
        python-version: '3.8'
        
    - name: Install Python dependencies
      uses: py-actions/py-dependency-install@v2
      with: /path/to/requirements

    - name: Run Tests
      run: python3 main.py --backend http://localhost:8080/ --frontend http://${{ steps.ip_addr.outputs.ip }}:4200 --selenium http://localhost:4444/wd/hub

main.py starts two docker containers (that expose their corresponding ports) and runs a suite of selenium tests. main.py启动两个 docker 容器(暴露它们相应的端口)并运行一套 selenium 测试。 It works on my local machine with a container I run using docker run -d -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:latest .它可以在我的本地机器上使用我使用docker run -d -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:latest运行的容器。 It takes the url for the backend and the one for the frontend, which gets used by selenium.它使用后端的 url 和前端的 url,由 selenium 使用。 I think I have to use the IP address of the runner so selenium can access the site (since it runs locally, localhost doesn't work), but it fails with the following error:我想我必须使用运行器的 IP 地址,以便 selenium 可以访问该站点(因为它在本地运行,localhost 不起作用),但它失败并出现以下错误:

  File "main.py", line 57, in <module>
    testcase.run()
  File "/home/runner/work/PROJECT_NAME/QualityAssurance/integration_tests/test_cases/t1.py", line 14, in run
    self.web_driver.accept_cookies()
  File "/home/runner/work/PROJECT_NAME/QualityAssurance/integration_tests/test_utils/parkview_webdriver.py", line 64, in accept_cookies
    self.wait_and_click(By.ID, 'confirmCookies')
  File "/home/runner/work/PROJECT_NAME/QualityAssurance/integration_tests/test_utils/parkview_webdriver.py", line 30, in wait_and_click
    WebDriverWait(self.driver, 10).until(
  File "/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

It seems to just not load the time, but I don't know to fix this.它似乎只是没有加载时间,但我不知道要解决这个问题。 I guess it could also be that I messed up the networking somehow?我想这也可能是我以某种方式搞砸了网络?

I think the service container is referenced by the service name instead of localhost.我认为服务容器是由服务名称而不是 localhost 引用的。 ie in your example:即在你的例子中:

--selenium http://localhost:4444/wd/hub

would be:将会:

--selenium http://selenium:4444/wd/hub

as you defined it:正如你定义的那样:

services:
  selenium:

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

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