Łukasz Gil
Łukasz Gil

Reputation: 59

Selenium Pytest GitLab CI - pytest problem

I have a problem with 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. I admit that I still do not know anything about topics such as Docker

This is my .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:

  1. Install all plugins (Pytest, selenium, chrome, allure) 2.Perform the test in Pytest

Upvotes: 2

Views: 2088

Answers (3)

Lucas Mendes
Lucas Mendes

Reputation: 1

When you declare services in gitlab-ci, the pipeline is bringing up a auxiliar container to help the main container of the job.

This auxiliar container is in the same runtime internal network and you must call him in the main container.

So you don't need to worry with with install webdriver or webdriver manager.

You must use a remote webdriver pointing the command executor to selenium/standalone service.

Example using selenium/standalone-firefox as service:

.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

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. The reference for all this is here (https://docs.gitlab.com/ee/ci/services/#accessing-the-services)

Upvotes: 0

Łukasz Gil
Łukasz Gil

Reputation: 59

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

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

Upvotes: -1

Drop
Drop

Reputation: 450

The problem is with two things:

  • selected image/service does not have python3 installed,
  • setting for webdriver run on linux machine are little more different than in yours configuration.

Please look below at my conftest.py

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

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:

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...

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

Upvotes: 2

Related Questions