Selenium for Python Tutorial
Getting Started with Applitools
Want to learn more about the Applitools platform and how it works? First get started with one of the following:
Running Tests with Applitools
Prerequisites
- A free Applitools account and Applitools API KEY
Tip: Unsure how to set up your API key?
Learn how to get started at Setting Up
Note: Installing `git` is optional
Installing git is used to clone the demo project from the Github repository. Instead of installing git
, you can simply download the Zip file from the repository. Further, if you are Mac, you already have git
.
Google Chrome Browser https://www.google.com/chrome/
ChromeDriver https://chromedriver.chromium.org/getting-started
ChromeDriver must be installed and in your `PATH`
Below are some resources from the internet that'll help you
On Mac, place the chromedriver
executable in /usr/local/bin
folder so Eclipse and IntelliJ can find it.
Option 1 - Run With The Ultrafast Grid
- Clone or download the repository and navigate to that folder
git clone https://github.com/applitools/tutorial-selenium-python-ultrafastgrid.git
cd tutorial-selenium-python-ultrafastgrid
2
Note: you can alternatively download the project as a Zip file and extract it
- Install the dependencies
pip3 install -r requirements.txt
- Run the example test
APPLITOOLS_API_KEY="[Your API Key]" pytest ultrafastgrid_tutorial.py
This will first set your APPLITOOLS_API_KEY
into the node process then run pytest ultrafastgrid_tutorial.py
.
Running from an IDE
Start PyCharm, open just cloned project, set project interpreter by File > Settings > Project: > Project Interpreter choose interpreter by dropdown box; tap Run and choose ultrafastgrid_demo
.
Adding Applitools Eyes to an Existing Project
Example Test
import os
import pytest
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from applitools.selenium import (
VisualGridRunner,
Eyes,
Target,
BatchInfo,
BrowserType,
DeviceName,
)
@pytest.fixture(scope="module")
def batch_info():
"""
Use one BatchInfo for all tests inside module
"""
return BatchInfo("Demo Batch - Selenium for Python - Ultrafast")
@pytest.fixture(name="driver", scope="function")
def driver_setup():
"""
New browser instance per test and quite.
"""
# Set chrome driver to headless when running on the CI
options = webdriver.ChromeOptions()
options.headless = (os.getenv('CI', 'False') == 'true')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
yield driver
# Close the browser.
driver.quit()
@pytest.fixture(name="runner", scope="session")
def runner_setup():
"""
One test runner for all tests. Print test results in the end of execution.
"""
runner = VisualGridRunner()
yield runner
all_test_results = runner.get_all_test_results()
print(all_test_results)
@pytest.fixture(name="eyes", scope="function")
def eyes_setup(runner, batch_info):
"""
Basic Eyes setup. It'll abort test if wasn't closed properly.
"""
eyes = Eyes(runner)
# Initialize the eyes SDK and set your private API key.
eyes.api_key = os.environ["APPLITOOLS_API_KEY"]
eyes.configure.batch = batch_info
# Add browsers with different viewports
# Add mobile emulation devices in Portrait mode
(
eyes.configure.add_browser(800, 600, BrowserType.CHROME)
.add_browser(700, 500, BrowserType.FIREFOX)
.add_browser(1600, 1200, BrowserType.IE_11)
.add_browser(1024, 768, BrowserType.EDGE_CHROMIUM)
.add_browser(800, 600, BrowserType.SAFARI)
.add_device_emulation(DeviceName.iPhone_X)
.add_device_emulation(DeviceName.Pixel_2)
)
yield eyes
# If the test was aborted before eyes.close was called, ends the test as aborted.
eyes.abort_if_not_closed()
def test_ultra_fast(eyes, driver):
# Navigate to the url we want to test
driver.get("https://demo.applitools.com")
# Call Open on eyes to initialize a test session
eyes.open(driver, "Demo App - Selenium for Python - Ultrafast", "Smoke Test - Selenium for Python - Ultrafast", {"width": 800, "height": 600})
# check the login page with fluent api, see more info here
# https://applitools.com/docs/topics/sdk/the-eyes-sdk-check-fluent-api.html
eyes.check("", Target.window().fully().with_name("Login page"))
driver.find_element_by_id("log-in").click()
# Check the app page
eyes.check("", Target.window().fully().with_name("App page"))
# Call Close on eyes to let the server know it should display the results
eyes.close(False)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Option 2 - Run Locally
- Clone or download the repository and navigate to that folder
git clone https://github.com/applitools/tutorial-selenium-python.git
cd tutorial-selenium-python
2
Note: you can alternatively download the project as a Zip file and extract it
- Install the dependencies
pip3 install -r requirements.txt
- Run the example test
APPLITOOLS_API_KEY="[Your API Key]" pytest tutorial.py
This will first set your APPLITOOLS_API_KEY
into the node process then run pytest tutorial.py
.
Running from an IDE
Start PyCharm, open just cloned project, set project interpreter by File > Settings > Project: > Project Interpreter choose interpreter by dropdown box; tap Run and choose ultrafastgrid_demo
.
Adding Applitools Eyes to an Existing Project
Example Test
import os
import pytest
from selenium import webdriver
from applitools.selenium import Eyes, Target, BatchInfo, ClassicRunner
from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture(scope="module")
def batch_info():
"""
Use one BatchInfo for all tests inside module
"""
return BatchInfo("Some general Test cases name")
@pytest.fixture(name="driver", scope="function")
def driver_setup():
"""
New browser instance per test and quite.
"""
driver = webdriver.Chrome(ChromeDriverManager().install())
yield driver
# Close the browser.
driver.quit()
@pytest.fixture(name="runner", scope="session")
def runner_setup():
"""
One test runner for all tests. Print test results in the end of execution.
"""
runner = ClassicRunner()
yield runner
all_test_results = runner.get_all_test_results()
print(all_test_results)
@pytest.fixture(name="eyes", scope="function")
def eyes_setup(runner, batch_info):
"""
Basic Eyes setup. It'll abort test if wasn't closed properly.
"""
eyes = Eyes(runner)
# Initialize the eyes SDK and set your private API key.
eyes.api_key = os.environ["APPLITOOLS_API_KEY"]
eyes.configure.batch = batch_info
yield eyes
# If the test was aborted before eyes.close was called, ends the test as aborted.
eyes.abort_if_not_closed()
def test_tutorial(eyes, driver):
# Start the test and set the browser's viewport size to 800x600.
eyes.open(driver, "Test app", "First test", {"width": 800, "height": 600})
# Navigate the browser to the "hello world!" web-site.
driver.get("https://demo.applitools.com")
# Visual checkpoint #1.
eyes.check("Login Window test", Target.window())
# End the test.
eyes.close(False)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
More information
The documentation for the Applitools Eyes Selenium 3 Python SDK contains more information about advanced configuration.
Resources
Terms & Conditions Privacy Policy GDPR© 2021 Applitools. All rights reserved.