4. Writing Tests with Pylenium
Easy as py π₯
Create a Test File
A Test File is just that - a file with tests in it. Depending on the type of project you're working on, you may want these to live right next to your code files or in a separate /tests
directory.
In these docs, we will assume you are writing your tests in a /tests
directory of your project.
Create a Test File called test_qap_dev.py
Test Files do not need to start with
test_
, but it's recommendedThis is a naming convention used to make it easier to distinguish between code and tests
You should now have a Project Structure that looks like this:
Project
tests
test_qap_dev.py
conftest.py
pylenium.json
pytest.ini
venv
Write the Test
We are going to make a test that does the following:
Visits the QA at the Point website: https://qap.dev
Hovers the About link to reveal a menu
Click the Leadership link in that menu
Assert Carlos Kidman is on the Leadership page
When you type py.
, you should see an auto-complete or IntelliSense menu appear with the list of Pylenium Commands like .visit()
and .get()
Your IDE may not do this or you may be missing an Extension or Plugin.
PyCharm has pytest support out-of-the-box.
Let's move on with the steps.
Visit https://qap.dev
Hover the About link to reveal a menu
When you get an Element from locator methods:
.get() | .getx()
.find() | .findx()
.contains()
you can perform actions against the element like:
.click()
.type()
.hover()
and more!
Make sure to check out the many commands available in Pylenium
Click the Leadership link in the menu
Assert Carlos Kidman is on the Leadership page
Run the Test
If you're using PyCharm or VS Code, there should be a green Play button next to the test definition. Click it and select either Run to execute normally or Debug to use breakpoints in Debug Mode.
Otherwise, use the method your IDE provides. You can always use the CLI as well:
Look at the Difference
Here is the same test but written with Selenium out of the box:
Another Test Example
Let's write another test that searches for Pylenium
and makes sure the results page contains that term in the title.
Navigate to Google.com
Type
Pylenium
into the search fieldSubmit the search
Assert the results page contains the title
You've already seen different Element commands like .visit()
, .type()
and .submit()
, but there is also a Should object for:
In the example above, py.should()
uses an Explicit Wait to wait until the "driver" detects that the current page's title contains "Pylenium"
.
If the title contains
"Pylenium"
within the specified timeout, then it returnsTrue
and passes the assertionIf the title does not meet the expectation within the specified timeout, then it returns
False
and fails the assertion
You can leverage these Should expectations to easily wait for conditions or write assertions!
Last updated