运行 Gitlab 中的硒测试 CI

run selenium testing in Gitlab CI

使用gitlab pipeline,我可以实现以下功能吗?

stages:
 - run-dvwa       # where I launch a web target using image of https://hub.docker.com/r/vulnerables/web-dvwa
 - run-selenium   # where I launch selenium using image selenium/standalone-firefox:latest
 - run-python     # where I run some py script to crawl dvwa end points

我可以运行按顺序执行上述操作吗,这也意味着容器 'run-dvwa' 和 'run-selenium' 在进入阶段 'run-python' 时不能退出?

感谢您的建议或样品!

我的第一个版本 (2021.07.26.v1)


python-test:
  services:
    - name: registry.gitlab.com/xxxx-yyy-demo/zzzzz-demo/dvwa-devops-demo
      alias: dvwa
      entrypoint: ["/main.sh"]
    - name: selenium/standalone-firefox:latest
      alias: selenium
  stage: run
  image: python:3
  script:
    - curl http://dvwa:80/login.php
    - curl http://selenium:4444/wd/hub
    - pip install selenium
    - python tests.py

我的test.py

driver = webdriver.Remote("http://selenium:4444/wd/hub", DesiredCapabilities.FIREFOX)
server = 'http://dvwa:80'
driver.get(server + '/login.php')

从我的测试来看,一切都通过了 driver.get(...) 行,我得到了

Traceback (most recent call last):
  File "/builds/xxxx-yyy-demo/zzzzz-demo/dvwa-devops-demo/xxxx-yyy-demo/tests.py", line 42, in <module>
    driver.get(server + '/login.php')
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=dnsNotFound&u=http%3A//dvwa/login.php&c=UTF-8&d=We%20can%E2%80%99t%20connect%20to%20the%20server%20at%20dvwa.

在 GitLab CI 中,阶段意味着按顺序执行。只有前一阶段成功完成才能进入下一阶段。

您的用例听起来更像是您可以从 services 中受益,例如像这样:

python-test:
  stage: test
  image: python:3
  variables:
    FF_NETWORK_PER_BUILD: 1
  services:
    - name: vulnerables/web-dvwa:latest
      alias: dvwa
    - name: selenium/standalone-firefox:latest
      alias: selenium
  before_script:
    - pip install selenium
  script:
    - python tests.py

请注意,您必须启用 network per-build feature (FF_NETWORK_PER_BUILD feature flag),以便 selenium 容器可以连接到 dvwa 容器。