#100DaysOfWeb in Python Transcripts
Chapter: Day 49: Selenium
Lecture: Virtual environment, dependencies, pytest starter code (fixtures)
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
First we need to get set up. We're going to create a virtual environment activate it, pip install the dependencies and then we're going to use pytest
0:12
in combination with Selenium. We need to have the chrome driver in our path and we can download it from the link shown on the screen.
0:22
Heading over my terminal, I made a new folder and we're going to run penv, which is an alias for Python3 -m venv venv.
0:33
So venv is the module and venv is the name of my virtual environment and I activate it. Fresh new virtual environment, nothing installed.
0:51
So pip install pytest and selenium. Alright and thirdly let's download the driver. First of all, my path is currently set to this.
1:11
Which includes my bin folder so I'm going to put the driver there. So I'm going to the chrome driver downloads page and then click on the latest.
1:28
And I'm going to get the mac version.
1:47
Alright, that's done and the last thing I forgot to mention on the slide is if you actually want to follow along on the code, you can head over to
2:00
my reading list and create an account, here's the direct link or you just go to login register. Then you get a username and password which you can
2:12
set in your virtual environment because part of the test will log in the user and log out etcetera. So I'm going to do that now for my existing user.
2:25
Go back to my project folder, into my activation script, go to the end and I set those two variables in my environment.
2:36
Of course, I will set this to the real user and password and click save. I have to deactivate and reactivate, so I use ae
2:48
because I'm doing this a lot but you can also type source venv/bin/activate and now we should have those variables available in the shell.
3:05
Lastly I made some starter code because there's some pytest in there that should not be the focus of this lesson.
3:11
So let me create a test_pbreadinglist.py and copy in the starter code. Before explaining let me quickly run this to see if that works.
3:39
Alright, I'm going to break that because the one passing test is good enough for now. Quickly, so I do my imports of the standard
3:46
library modules, I import pytest and I import some functionality that we'll need from selenium. Then I load in the username and the password
3:55
from my virtual environment we just set. I set some constants for the homepage, two book pages and a link title my books.
4:04
And then I write a bunch of fixtures. Again it's not a pytest lesson so I linked an article here where I explain how fixtures work
4:13
what they are and how to set them up. So just quickly a fixture, you usually need some set up and tear down code.
4:23
So a code you run before and after a test. So in this case, the repeated code before each test is getting an instance of the web driver
4:32
and going to the home page. I also need some tear down code to quit the driver. Which means that we've all closed the browser which selenium opens.
4:43
The neat way of pytest, or doing this is to build a driver, so this will run before the test this will help us into the test code and then
4:52
we come back to this final tear down code after the test. So again, closing the browser. So I made a fixture to go to the homepage.
5:01
I made a fixture to go to the first book page which we defined in this constant above and I also
5:09
made a fixture to login a user, because that's code that I needed for various tests so to keep it DRY or don't repeat yourself, I abstracted that code
5:20
in this fixture, so we only need to write it once so we can use it for various tests. Then, the way to use the fixtures is just to
5:28
pass them into the test functions as an argument. So here, I'm using driver_home, and further down I use driver_first_book and driver_login.
5:39
Then, as we will see when we write it test, we can access that fixture. So all the set up and tear down code has been
5:46
provided to really focus this lesson on the selenium and over the coming five videos, we're going to implement those ten test functions, one-by-one.
5:56
Teaching you how to write selenium code.