#100DaysOfWeb in Python Transcripts
Chapter: Day 49: Selenium
Lecture: Test #4: a test function for adding/deleting a book
0:01 In this video, we're going to test the addition and deletion of a book when logged in. So we're going to navigate to "My Books"
0:11 and to see what the number of books are that I've read which is a headline on my profile. I'm going to navigate to the book of my choice
0:21 and click "Add", then I go back to "My Books" and see if the counter of the books read increased by one. Then we delete the book again to make sure
0:32 that the next time I run the test we start from the same state. Let me show the GUI. So the test would navigate to "My Books,"
0:46 and this is just to tester account. So I have zero books added but when I add a book the counter should be one.
1:00 For simplicity I'm only going to look at the books to this first counter not to the pages to keep it simple. And at the end of the test
1:10 we're going to delete the book to get back to zero. So how would we code this up? First just let me go into my profile twice
1:19 to get the number of books. So to keep it DRY or don't repeat yourself we're going to build a helper to address that first.
1:32 I call it _get_number_books_read. I pass in a driver. First I'm going to go to My Books which we just select by link text.
1:59 Actuall we already have a constant called MY_BOOKS and I'm going to click on it. So that will go to My Books.
2:13 I already was there. And we're going to target this div which does not have an id but we can target a class.
2:23 So I can take either this one but let's go with subhead. And I think that's the only one on the page. Alright, so that's good.
2:37 So I'm going to define the variable stats and I'm going to call find_element_by_class_name, Movie Text Subhead.
2:57 And then I'm going to use a regular expression to extract the number of books from that. So I want to have this integer so I can.
3:08 I already have my import re. I'm going to do a re.sub with raw string bear with me. First of all the stats variable is an object and the text
3:30 in this piece is in the text attributed. And then I can do. A regular expression match of all actually starts with Status and match
3:43 Everything up till the end. And this number of books will be the variable part. Which is one or more digits.
3:53 And here I use parentheses to capture this data. So we can reference and in the replacement section so what this is going to do
4:03 Is match the full string Capture the integer or the number of books. And that's the piece we want to extract from it.
4:11 So it's going to throw everything away. Before the first number and everything after that number. And let's cast that to an integer.
4:21 And that's what a return. So I need to do this lookup twice. So now we have a nice helper function. We can use in our test function.
4:32 Now let's write the test function. First I get the number of books read from the start I can use our new helper. And that gives me zero.
4:48 Let's actually see if that works. And let me not only use -k to filter the test but also -s to capture standard output to bounce
5:04 that number to the screen. And it grabs the zero successfully. Great. So we need to code to subtasks
5:26 which is adding a book should increase the book counter by one in the ratings. Deleting the book should bring the counter back
5:45 to the initial count. In the initial count use this one. First I'm going to navigate to the second book
5:59 and the second book was defined as this one doesn't really matter can be any book. Once I'm on a book
6:16 I'm going to target this button to add it to my reading list. And again I use inspect to see what the name of that button is. And it is bookSubmit.
6:29 So I'm going to find_element_by_name bookSubmit. So that finds the button. And then in the same statement I click on it.
6:46 Then I'm going to calculate again what the number of books read on my profile is now. And this helper actually does the navigation as well.
6:59 So this goes to my books page. I don't have to do that here. And what we're going to assert then is that the number of books
7:08 after addition equals, the number of books from the start plus one. Let's test it out and put a breakpoint in here to see how we get rid of the book.
7:22 Again the break point is to pass the execution. So we have the headless browser available to inspect the HTML.
7:39 I'm logging in Navigating through the book adding it and I have one on my profile which is trying to assert.
7:48 Here I met the current state of the execution the test got into the debugger see how I can get rid of this book.
7:56 So the next step is to hit the delete button. So that the book is deleted from our list. So I need to target a button
8:09 with name deleteBook. So I copied this name actually deleted here as well so I have a clean slate back for the next test. And the test worked
8:24 So our code is working. And the final part then is to navigate to the book again. Target the delete button click it.
8:44 Calculate the new count. Number of books after delete. And again the helper takes care of navigating to my books
8:56 and the assertion then is that the number of books after delete should be the same as the number of books when we started.
9:07 So adding book number of books goes to one. Deleting the same book. The counter goes back to zero. So let's try it out again.
9:34 Awesome, that worked. On the final video we write one more test to log out the user and see if the navigation links are going back to the public ones.