#100DaysOfWeb in Python Transcripts
Chapter: Day 49: Selenium
Lecture: Test #4: a test function for adding/deleting a book

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