Skip to content

Commit

Permalink
Merge branch 'v2.1.0' into 'master'
Browse files Browse the repository at this point in the history
v2.1.0 Add local execution on localhost selenium hub. Add Readme.

See merge request aleksandr-kotlyar/python-gitlabci-selenium-example!4
  • Loading branch information
aleksandr-kotlyar committed Mar 9, 2020
2 parents 93f6808 + bde29d9 commit 12a4769
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ variables:
before_script:
- pip3 install -r requirements.txt
script:
- pytest --browser=$BROWSER
- pytest --browser=$BROWSER --local='false'

e2e:remote:chrome:
extends: .job_template
Expand Down
8 changes: 6 additions & 2 deletions CHANGELOG.MD → CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Changelog

## 2.0.0
## 2.1.0 (Released 09.03.2020)
- Added execution on localhost selenium hub.
- Added README

## 2.0.0 (Released 08.03.2020)
- Added LICENSE for using.
- Added Firefox job.
- Added --browser option for selecting chrome or firefox to run tests in. Default is chrome.
- Renamed fixture 'browser' to 'remote_browser'.

## 1.0.0
## 1.0.0 (Released 08.03.2020)
- Bump version for release.

## 0.1.1
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Selenium with Docker and GitLab
## About
This is one example of how you can implement automation on a project with Selenium and GitLab.
#### Browsers
- Chrome (default)
- Firefox
## GitLab Usage
- To run job with Chrome just run pipeline with one of the Triggers
- To run job with Firefox set GitLab variable BROWSER=firefox and run pipeline with one of the Triggers
#### Triggers
- **Manual** "Run pipeline" from WebUI
- **Schedule** to start pipeline by cron
- **Push** commit to gitlab and pipeline will start automatically
- **Trigger** API endpoint to start pipeline
## Local Usage
Prepare
```shell script
docker run -d -p 4444:4444 --net grid --name selenium-hub selenium/hub:3.141.59
docker run -d --net grid -e HUB_HOST=selenium-hub --name chrome -v /dev/shm:/dev/shm selenium/node-chrome
docker run -d --net grid -e HUB_HOST=selenium-hub --name firefox -v /dev/shm:/dev/shm selenium/node-firefox
```
Run tests
```shell script
# tests on Chrome
pytest
# or
pytest --browser=chrome
# tests on Firefox
pytest --browser=firefox
```
End of work
```shell script
docker stop selenium-hub chrome firefox
```
61 changes: 44 additions & 17 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,56 @@ def pytest_addoption(parser):
parser.addoption('--browser',
help='Which test browser?',
default='chrome')
parser.addoption('--local',
help='local or CI?',
choices=['true', 'false'],
default='true')


@pytest.fixture(scope='session')
def test_browser(request):
return request.config.getoption('--browser')


@pytest.fixture(scope='function')
def remote_browser(test_browser) -> Remote:
""" Select configuration depends on browser """
if test_browser == 'firefox':
driver = webdriver.Remote(
options=webdriver.FirefoxOptions(),
command_executor='http://selenium__standalone-firefox:4444/wd/hub'
)
return driver

elif test_browser == 'chrome':
driver = webdriver.Remote(
options=webdriver.ChromeOptions(),
command_executor='http://selenium__standalone-chrome:4444/wd/hub'
)
return driver
@pytest.fixture(scope='session')
def local(request):
return request.config.getoption('--local')


@pytest.fixture(scope='function')
def remote_browser(test_browser, local) -> Remote:
""" Select configuration depends on browser and host
"""
if local == 'false':
if test_browser == 'firefox':
driver = webdriver.Remote(
options=webdriver.FirefoxOptions(),
command_executor='http://selenium__standalone-firefox'
':4444/wd/hub')
elif test_browser == 'chrome':
driver = webdriver.Remote(
options=webdriver.ChromeOptions(),
command_executor='http://selenium__standalone-chrome'
':4444/wd/hub')
else:
raise ValueError(f'--browser="{test_browser}" '
f'is not chrome or firefox')
elif local == 'true':
if test_browser == 'firefox':
driver = webdriver.Remote(
options=webdriver.FirefoxOptions(),
command_executor='http://localhost:4444/wd/hub')
elif test_browser == 'chrome':
driver = webdriver.Remote(
options=webdriver.ChromeOptions(),
command_executor='http://localhost:4444/wd/hub')
else:
raise ValueError(f'--browser="{test_browser}" '
f'is not chrome or firefox')
else:
raise ValueError(f'--browser={test_browser} is not chrome or firefox')
raise ValueError(f'--local={local}". Driver could not be setup.\n'
'pass option --local="true" if local execute\n'
'pass option --local="false" if use CI service')

yield driver
driver.quit()

0 comments on commit 12a4769

Please sign in to comment.