#100DaysOfWeb in Python Transcripts
Chapter: Day 49: Selenium
Lecture: Test #3: testing the login, using the debugger with pytest

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Alright next we're going to test the log in to the site.
0:05 We're going to click the log in button in the header
0:09 log in with the user we set in our virtual environment
0:13 assert that the log in link changed to log out
0:17 and check the additional private links
0:19 that should appear in the navigation bar at the top.
0:22 So going back to my terminal to the code.
0:26 First of all, we are using another feature
0:28 which is driver_login
0:31 and we actually already wrote the code to log in
0:35 so when we get to the homepage
0:37 we're going to find a link that's called log in.
0:40 And to show you that, if I do inspect
0:46 and here we didn't actually have a class name
0:50 or CSS id attribute, so we're going to look for
0:54 the link text, which should be log in
0:57 so that's why we did find_element_by_link_text of login
1:02 so we're going to click that, the test will be doing this
1:07 and then I'm going to target the username
1:10 and password fields, which are conveniently named
1:13 username and password.
1:19 And going to enter data, which we saw before
1:22 can be done with the send_keys method
1:25 and we're going to give it the username and password.
1:28 And importantly, after filling in the password
1:31 we want to hit return.
1:33 The way to do that is to append Keys.RETURN to the input.
1:38 Alternatively you could also target the log in button
1:41 and hit click, but a form can also be committed
1:44 by just hitting enter.
1:46 And again username and password are loaded in
1:49 from my environment as shown in a previous video
1:52 so once we get into this test function as well as
1:55 the next ones, the log in should already be done.
1:58 And let me actually show you that in action
2:00 and one trick I want to show you is to put a break point
2:03 in there, which is new in 3.7, on 3.6 or before
2:08 you would actually have to write import pdb; pdb.set_trace
2:13 but I'm on 3.7, so I can use
2:15 the new shorter syntax
2:19 and this will pause the execution
2:21 and I want to do that to show the view after logging in
2:25 because what we're going to test in this function
2:29 is the presence of the private links.
2:34 So let me run this test.
2:41 It should login, and then it should hang there
2:44 because I put a breakpoint in there.
2:48 Great, so here I'm at my pdb prompt
2:52 and this is actually pretty convenient
2:54 to poke around at the code as well.
2:57 Not only is this good to see the state
2:59 of my headless browser, it's also nice
3:02 to inspect the driver at this point.
3:07 For example, I can just write my code here
3:09 and later paste it into the test.
3:11 For example, I should have an element now
3:21 called logout, and that's true.
3:24 I should not have a login anymore
3:27 and that indeed raises a NoSuchElementException.
3:31 Another link I should have is, let's see, my books
3:40 and that's true, so I can now just copy this information
3:45 and use it in my test.
3:57 So we can assert that there is a logout link
4:00 but let's do it like I did before to try this.
4:09 And if there's a NoSuchElementException
4:13 I'm going to say, pytest
4:16 fail missing private links in nav bar
4:25 so it should not get to here when logged in
4:28 otherwise the test will fail.
4:32 On the other hand, the login link
4:37 should not be there anymore
4:39 so this code should actually hit
4:46 and if it's not, I will fall through
4:49 to another pytest fail saying should not have a login link
4:59 when logged in, so again, I should not hit this
5:04 because this should already raise an exception.
5:09 Now just to complete this, let's add my books
5:13 and the five hour challenge, that's another nav bar item
5:21 that's only visible when I'm logged in.
5:24 So when I'm logged in, which is the case here
5:26 because I'm using the driver login fixture
5:29 this should all work, and when I get here
5:32 it's definitely not good.
5:33 On the other hand, the login link should be gone
5:37 so I will hit this, which is fine, pass.
5:41 If this actually does not raise an exception
5:44 this test should fail.
5:49 And that's our test for the login transition
5:54 and the linking in the nav bar, let's run it again.
6:09 Awesome, and in the next video
6:12 we're going to add and delete a book
6:14 and see how that updates the counts on my books page.