【问题标题】:run selenium testing in Gitlab CI在 Gitlab CI 中运行硒测试
【发布时间】:2021-09-28 21:15:08
【问题描述】:

使用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

我的测试.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.

【问题讨论】:

    标签: selenium security gitlab gitlab-ci penetration-testing


    【解决方案1】:

    在 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 功能(FF_NETWORK_PER_BUILDfeature flag),以便selenium 容器可以连接到dvwa 容器。

    【讨论】:

    • 感谢您的提示!我正在朝着这个方向努力,但我遇到了一些错误。我已经更新了我的问题。你能帮忙再看看吗?我是遇到硒兼容性问题还是这里的程序逻辑有问题?谢谢!
    • 为了让 selenium 服务能够访问 dvwa 服务,您必须启用 network per-build 功能 (FF_NETWORK_PER_BUILD feature flag)。我刚刚相应地更新了我的答案。
    • 再次感谢您的提示!效果很好!
    猜你喜欢
    • 2015-10-24
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2016-01-01
    相关资源
    最近更新 更多