首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium Pytest GitLab CI - pytest问题

Selenium Pytest GitLab CI - pytest问题
EN

Stack Overflow用户
提问于 2021-03-17 10:31:32
回答 3查看 1.1K关注 0票数 2

我对Gitlab Ci有意见。我对此完全不感兴趣。我在gitlab中放置了Ci之后,在本地运行了一个很好的测试。(Python3.8)--不幸的是,它已经不再那么丰富多彩了。我承认我对诸如Docker这样的话题一无所知。

这是我的..gitlab ci.yml:

代码语言:javascript
复制
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

我得到了错误:

代码语言:javascript
复制
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)

这是原稿:

代码语言:javascript
复制
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、selenium、chrome、allure) 2.在Pytest

中进行测试

EN

回答 3

Stack Overflow用户

发布于 2021-03-17 15:58:41

问题在于两件事:

选定的映像/服务没有安装configuration.,在linux计算机上运行的

  • 设置与您的
  • 中的
  • 设置没有什么不同

请看下面我的conftest.py

我的conftest.py

代码语言:javascript
复制
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):

代码语言:javascript
复制
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

代码语言:javascript
复制
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'

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

现在commit+push给您GitLab存储库并等待结果..。

这将给我们带来失败作为一个结果-不用担心!我们期待它,因为Google页面标题与预期不同(但现在我们确信测试确实有效):

代码语言:javascript
复制
_____________________ 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
EN

Stack Overflow用户

发布于 2021-09-24 02:08:30

当您在gitlab-ci中声明服务时,管道将提供一个辅助容器来帮助作业的主容器。

这个auxiliar容器位于同一个运行时内部网络中,您必须在主容器中调用它。

因此,您不需要担心安装webdriver或webdriver。

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

示例使用selenium/独立-firefox作为服务:

..gitlab ci.yml

代码语言:javascript
复制
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

测试/测试_selenium.py

代码语言:javascript
复制
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)

票数 0
EN

Stack Overflow用户

发布于 2021-03-17 23:38:41

requirements.txt

代码语言:javascript
复制
pytest==6.2.2  
selenium==3.141.0 
webdriver_manager==3.3.0 
allure-pytest==2.8.36

gitlab-ci.yml

代码语言:javascript
复制
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

阶段错误:测试

代码语言:javascript
复制
 $ python3 -m pytest Tvn24_Tests/Login_By_Facebook_Test.py
    --alluredir ./Report/Allure/Login_By_FB
 /usr/bin/python3: No module named pytest
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66671263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档