Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c1fb60

Browse files
first commit
1 parent 3b4e404 commit 1c1fb60

File tree

5 files changed

+151
-1
lines changed

5 files changed

+151
-1
lines changed

‎README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
# python-selenium-docker-tests
1+
# Python + Selenium + Docker
2+
This sample project demonstrates how we can utilize docker container for running python selenium ui tests.
3+
4+
Script does the Docker Container start before the test gets executed and once execution is completed it will get stopped/removed.
5+
6+
Test script file `ui-test.py` has `setUpClass()` which instantiate the selenium container and `tearDownClass()` stop/remove the selenium container.
7+
8+
## Requirements
9+
### Mac OS
10+
11+
Docker is installed and running.
12+
13+
### Lunux/Windows OS
14+
15+
* Docker is installed and running.
16+
* docker-compose is installed and running.
17+
18+
## pip
19+
20+
```
21+
pip install subprocess.run
22+
pip install selenium
23+
```
24+
25+
## Test Run
26+
27+
```
28+
python ui-test.py
29+
```
30+
31+
## Output
32+
33+
```
34+
Creating network "ui_test_default" with the default driver
35+
Creating ui_test_chrome_standalone_1 ... done
36+
test_case_1 (__main__.TestTemplate)
37+
Find and click top-right button ... ok
38+
test_case_2 (__main__.TestTemplate)
39+
Find and click Learn more button ... ok
40+
Removing ui_test_chrome_standalone_1...
41+
removed...
42+
43+
----------------------------------------------------------------------
44+
Ran 2 tests in 15.363s
45+
46+
OK
47+
```

‎clean.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
if [[ $(docker ps -aq) ]]; then
3+
echo "Removing $(docker ps --format "{{.Names}}")..."
4+
running_container="$(docker ps --format "{{.Names}}")"
5+
docker rm -f $running_container | echo "removed..."
6+
else
7+
echo "nothing to stop/remove..."
8+
fi

‎docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
services:
3+
chrome_standalone:
4+
restart: always
5+
image: selenium/standalone-chrome-debug:latest
6+
volumes:
7+
- /dev/shm:/dev/shm
8+
ports:
9+
- 1111:4444
10+
- "5900"
11+
stdin_open: true
12+

‎sample-docker-start-stop.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os.path
2+
import subprocess
3+
from subprocess import PIPE
4+
5+
import time
6+
7+
def start():
8+
p = subprocess.Popen(['docker-compose', '-p ui_test', 'up', '-d'])
9+
time.sleep(5)
10+
p.wait()
11+
print("docker started....")
12+
13+
def stop():
14+
p = subprocess.Popen(['sh', './clean.sh'])
15+
time.sleep(5)
16+
p.wait()
17+
18+
start_docker()
19+
clean_docker_container()
20+

‎ui-test.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
A simple selenium test example demonstrating docker container usage.
3+
Thanks to @joyzoursky for the test script https://github.com/joyzoursky/docker-python-chromedriver
4+
"""
5+
6+
import unittest
7+
from selenium import webdriver
8+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
9+
from selenium.common.exceptions import NoSuchElementException
10+
11+
import os.path
12+
import subprocess
13+
from subprocess import PIPE
14+
15+
import time
16+
17+
class TestTemplate(unittest.TestCase):
18+
19+
@classmethod
20+
def setUpClass(self):
21+
"""Start web driver and start the docker container"""
22+
p = subprocess.Popen(['docker-compose', '-p ui_test', 'up', '-d'])
23+
time.sleep(5)
24+
p.wait()
25+
chrome_options = webdriver.ChromeOptions()
26+
chrome_options.add_argument('--no-sandbox')
27+
chrome_options.add_argument("--disable-setuid-sandbox")
28+
"""uncomment below for running in headless mode"""
29+
"""chrome_options.add_argument('--headless')"""
30+
chrome_options.add_argument('--disable-gpu')
31+
chrome_options.add_argument("--disable-extensions")
32+
"""selenium docker container is set to run on port 1111"""
33+
self.driver = webdriver.Remote(desired_capabilities=chrome_options.to_capabilities(), command_executor='http://localhost:1111/wd/hub')
34+
self.driver.implicitly_wait(10)
35+
36+
@classmethod
37+
def tearDownClass(self):
38+
"""Stop web driver and remove the docker container"""
39+
self.driver.quit()
40+
p = subprocess.Popen(['sh', './clean.sh'])
41+
time.sleep(5)
42+
p.wait()
43+
44+
def test_case_1(self):
45+
"""Find and click top-right button"""
46+
try:
47+
self.driver.get('https://www.oursky.com/')
48+
el = self.driver.find_element_by_class_name('btn-header')
49+
el.click()
50+
except NoSuchElementException as ex:
51+
self.fail(ex.msg)
52+
53+
def test_case_2(self):
54+
"""Find and click Learn more button"""
55+
try:
56+
self.driver.get('https://www.oursky.com/')
57+
el = self.driver.find_element_by_xpath(".//*[@id='tag-line-wrap']/span/a")
58+
el.click()
59+
except NoSuchElementException as ex:
60+
self.fail(ex.msg)
61+
62+
if __name__ == '__main__':
63+
suite = unittest.TestLoader().loadTestsFromTestCase(TestTemplate)
64+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /