【问题标题】:Selenium Pytest GitLab CI - pytest problemSelenium Pytest GitLab CI - pytest 问题
【发布时间】:2021-03-17 10:31:32
【问题描述】:

我对 Gitlab Ci 有疑问。 我对此完全是绿色的。 我已经运行了一个在本地运行良好的测试。(python 3.8) 将 Ci 放入 gitlab 后 - 不幸的是它不再那么丰富多彩了。 我承认我对Docker之类的话题还是一窍不通

这是我的 .gitlab-ci.yml :

stages:
  - test

e2e:chrome:
  services:
    - selenium/standalone-chrome

  before_script:
    - python -V
    - python3 -m pip install pytest
    - python3 -m pip install selenium pytest
    - python3 -m pip install webdriver_manager
    - python3 -m pip install allure-pytest

  script:
    - python -m pytest Tvn24_Tests/Login_By_Facebook_Test.py

我得到错误:

ERROR at setup of Test_Log_in.test_Facebook_login_method_Passed ________
request = <SubRequest 'setup' for <Function test_Facebook_login_method_Passed>>
    @pytest.fixture()
    def setup(request):
        options = Options()
        options.page_load_strategy = 'normal'
    
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
Tvn24_Tests/conftest.py:13: 

/usr/local/lib/python3.9/site-packages/webdriver_manager/chrome.py:25: in __init__
    self.driver = ChromeDriver(name=name,
/usr/local/lib/python3.9/site-packages/webdriver_manager/driver.py:54: in __init__
    self.browser_version = chrome_version(chrome_type)

这是原始脚本:

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import allure

@pytest.fixture()
def setup(request):
    options = Options()
    options.page_load_strategy = 'normal'

    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    request.cls.driver = driver
    driver.maximize_window()
    yield
    driver.quit()

问题

是否有任何简单的指令来创建这个 yml 文件的形式:

  1. 安装所有插件 (Pytest、硒、铬、诱惑) 2.在Pytest中进行测试

【问题讨论】:

  • 我不确定这是否是一个错字,但您要安装pytest-selenium 还是selenium pytest

标签: python selenium gitlab-ci


【解决方案1】:

问题在于两件事:

  • 所选图像/服务未安装 python3,
  • 在 linux 机器上运行的 webdriver 设置与您的配置略有不同。

请看下面我的conftest.py

我的conftest.py

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import allure

@pytest.fixture()
def setup(request):
    options = Options()
    options.page_load_strategy = 'normal'
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')

    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    request.cls.driver = driver
    driver.maximize_window()
    yield
    driver.quit()

我的 .gitlab-ci.yml(请考虑使用python3 -m pip install -r requirements.txt 而不是单独的pip install):

stages:
  - test

test:e2e:
  stage: test
  image: jaktestowac/python-chromedriver:3.6-xvfb

  before_script:
    - python3 -V
    - python3 -m pip install pytest
    - python3 -m pip install selenium pytest
    - python3 -m pip install webdriver_manager
    - python3 -m pip install allure-pytest

  script:
    - export PYTHONUNBUFFERED=1
    - python3 -m pytest sample_tests.py

我的sample_tests.py

import pytest

@pytest.mark.usefixtures("setup")
class SampleTestClass:
    def test_google_title(self):
        self.driver.get('https://google.com')
        title = self.driver.title
        print(f'Page title: {title}')
        assert title == 'google'

所有文件都在项目根目录中。

现在提交+推送到你的 GitLab 存储库并等待结果...

这会给我们带来失败——不用担心!我们期待它,因为 Google 页面标题与预期不同(但现在我们确信该测试确实有效;)):

_____________________ TestClassWithSetup.test_google_title _____________________
self = <sample_tests.TestClassWithSetup object at 0x7f33c9b053c8>
    def test_google_title(self):
        self.driver.get('https://google.com')
        title = self.driver.title
        print(f'Page title: {title}')
>       assert title == 'google'
E       AssertionError: assert 'Google' == 'google'
E         - google
E         ? ^
E         + Google
E         ? ^
sample_tests.py:9: AssertionError

【讨论】:

    【解决方案2】:

    当你在 gitlab-ci 中声明服务时,管道会启动一个辅助容器来帮助工作的主容器。

    这个辅助容器在同一个运行时内网,你必须在主容器中调用他。

    因此您无需担心安装 webdriver 或 webdriver manager。

    您必须使用远程 webdriver 将命令执行器指向 selenium/独立服务。

    使用 selenium/standalone-firefox 作为服务的示例:

    .gitlab-ci.yml

    test selenium:
      stage: test
      services:
        - selenium/standalone-firefox
      image: python:3.9-slim
      script:
        - pip install selenium pytest
        - pytest -v tests/test_selenium.py
      artifacts:
        when: always
        paths:
          - ./selenium.png
    

    测试/test_selenium.py

    from selenium.webdriver import Remote
    
    
    def test_google():
        driver = Remote(
            command_executor='http://selenium__standalone-firefox:4444/wd/hub',
            desired_capabilities={'browserName': 'firefox'}
        )
        driver.get('http://www.google.com')
        driver.save_screenshot('selenium.png')
        assert driver.title == 'Google'
    

    访问服务的方式有点奇怪(selenium__standalone-firefox),但是可以定义一个别名来让事情更好理解。所有这些的参考都在这里 (https://docs.gitlab.com/ee/ci/services/#accessing-the-services)

    【讨论】:

      【解决方案3】:

      requirements.txt

      pytest==6.2.2  
      selenium==3.141.0 
      webdriver_manager==3.3.0 
      allure-pytest==2.8.36
      

      gitlab-ci.yml

      stages:
        - build
        - test
      
      build:
        stage: build
        image: jaktestowac/python-chromedriver:3.6-xvfb
        before_script:
          - python3 -V
          - pip install --upgrade pip
        script:
          - python3 -m pip install -r requirements.txt
      
      test:e2e:
        stage: test
        script:
          - python3 -m pytest Tvn24_Tests/Login_By_Facebook_Test.py --alluredir ./Report/Allure/Login_By_FB
      

      阶段错误:测试

       $ python3 -m pytest Tvn24_Tests/Login_By_Facebook_Test.py
          --alluredir ./Report/Allure/Login_By_FB
       /usr/bin/python3: No module named pytest
      

      【讨论】:

        猜你喜欢
        • 2021-12-26
        • 2022-11-04
        • 2021-10-21
        • 1970-01-01
        • 2022-01-12
        • 2019-02-08
        • 2020-07-29
        • 1970-01-01
        • 2012-05-02
        相关资源
        最近更新 更多