简体   繁体   English

Selenium Pytest GitLab CI - pytest 问题

[英]Selenium Pytest GitLab CI - pytest problem

I have a problem with Gitlab Ci.我对 Gitlab Ci 有疑问。 I'm completely green at this.我对此完全陌生。 I have run a test that works fine locally.(python 3.8) After placing Ci in gitlab - unfortunately it is not so colorful anymore.我已经运行了一个在本地运行良好的测试。(python 3.8)将 Ci 放入 gitlab 之后 - 不幸的是它不再那么丰富多彩了。 I admit that I still do not know anything about topics such as Docker我承认我对Docker等话题还是一无所知

This is my.gitlab-ci.yml:这是我的.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

I get Error:我得到错误:

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)

Tis is original script:这是原始脚本:

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()

Question问题

Is there any simple instruction to create this yml file in the form:是否有任何简单的说明以以下形式创建此 yml 文件:

  1. Install all plugins (Pytest, selenium, chrome, allure) 2.Perform the test in Pytest安装所有插件(Pytest、selenium、chrome、allure) 2.在Pytest进行测试

The problem is with two things:问题在于两件事:

  • selected image/service does not have python3 installed,所选图像/服务未安装 python3,
  • setting for webdriver run on linux machine are little more different than in yours configuration.在 linux 机器上运行的 webdriver 设置与您的配置略有不同。

Please look below at my conftest.py请看下面我的conftest.py

My 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()

My .gitlab-ci.yml (please consider using python3 -m pip install -r requirements.txt instead of separate pip install ):我的.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

My 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'

All files are in project root directory.所有文件都在项目根目录中。

Now commit+push to You GitLab repository and wait for the results...现在提交+推送给你 GitLab 存储库并等待结果...

This will bring us failure as a result - no worries, We expect it;这将导致我们失败——不用担心,我们期待它; because Google page title is different than expected (but now we are sure that test really works:) ):因为谷歌页面标题与预期不同(但现在我们确信测试确实有效:)):

_____________________ 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

When you declare services in gitlab-ci, the pipeline is bringing up a auxiliar container to help the main container of the job.当您在 gitlab-ci 中声明服务时,管道会启动一个辅助容器来帮助作业的主容器。

This auxiliar container is in the same runtime internal.network and you must call him in the main container.这个辅助容器在同一个runtime internal.network 中,你必须在主容器中调用他。

So you don't need to worry with with install webdriver or webdriver manager.所以你不需要担心安装 webdriver 或 webdriver manager。

You must use a remote webdriver pointing the command executor to selenium/standalone service.您必须使用远程 webdriver 将命令执行器指向 selenium/standalone 服务。

Example using selenium/standalone-firefox as service:使用 selenium/standalone-firefox 作为服务的示例:

.gitlab-ci.yml .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

tests/test_selenium.py测试/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'

It's a little weird the way you access the service (selenium__standalone-firefox), but it is possible to define an alias to make the things better to understand.您访问服务的方式 (selenium__standalone-firefox) 有点奇怪,但是可以定义一个别名以使事情更好理解。 The reference for all this is here ( https://docs.gitlab.com/ee/ci/services/#accessing-the-services )所有这些的参考在这里( https://docs.gitlab.com/ee/ci/services/#accessing-the-services

requirements.txt要求.txt

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

gitlab-ci.yml 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

ERROR in stage: test阶段错误:测试

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

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

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