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


Talk Python's Mastodon Michael Kennedy's Mastodon