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