#100DaysOfWeb in Python Transcripts
Chapter: Day 49: Selenium
Lecture: Test #2: testing individual book pages and JS autocomplete

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The second task is to check a book page.
0:04 First we're going to navigate to a book page.
0:08 We're going to check the page title and its metadata.
0:13 You're going to see if there's an add book link.
0:17 Then we're going to test the search box
0:19 which has a nice auto-complete.
0:21 We test it out by typing fluent Python
0:23 and clicking on the first match.
0:25 It should redirect us to that book page.
0:28 The last video I noted that we had this helper
0:31 and we will use that later, so that we move
0:33 that down for now and go straight into
0:36 test_book_page_title.
0:38 Notice that we use another fixture.
0:40 And there's only a slight difference so both
0:42 initialize event driver, but home is going
0:45 to home and try your first book is going
0:48 to first book, which is defined as this one.
0:52 And that's actually the Hitchhiker's Guide to Python.
0:55 So now we're going to focus on testing this page.
0:59 So again, this fixture makes that we are already
1:02 on that page. So here I can just test the title.
1:07 I'm going to copy that over from the page.
1:10 So I do a view page source and I'm going to copy
1:14 over the title.
1:22 And as its a very long string, we can wrap it
1:25 over two lines by using parentheses.
1:34 And although this single quote is encoded
1:37 it's not what the title would give me in the script.
1:42 And now I can just assert driver_first_book.title == expected
1:52 Let's save then.
1:58 And just run that single test. Now it passes, cool.
2:06 So let's look at the metadata of that book.
2:09 So a nice way to get the source of the page
2:12 is to use the page_source attribute.
2:18 And that's dumping this whole source code into a variable.
2:21 So then I can just look for sub-strings.
2:26 For example, we should have this.
2:34 And for example, the page count.
2:45 Let's try it out.
2:54 Awesome.
2:56 Next test, test_book_page_has_add_book_link.
3:00 So now we're going to assert that logged-out
3:03 view of this book, we have this link.
3:06 And that's actually pretty similar as last time.
3:09 So I can just copy over this code
3:12 Make sure I use the right driver
3:16 and the right name of the link.
3:21 It should be add book title cased.
3:24 If the link is there, this will succeed.
3:27 If it's not there, it should raise a no such
3:30 element exception, and then we run pytest to fail.
3:42 Great.
3:44 Now, let's test the auto-complete.
3:46 And that should be a little bit more involved
3:48 because we're going to type in a title
3:50 in the search box. Let me show it here.
3:57 Then we should target the response HTML
4:01 retrieve the first element, click on it
4:05 which then should cause the page to redirect to that title.
4:08 So there are a few elements involved
4:09 and what you probably will be doing a lot
4:11 is right-click on the element and use inspect.
4:17 That goes straight to the matching HTML
4:19 of the source, and here we want to target this input field.
4:23 And we're going to target it by name, so search titles.
4:28 So first step is to find that element.
4:31 And before that actually, I'm going to also store
4:33 the text we're going to enter in a variable.
4:39 Then we get that input field, so we call it
4:41 search box, driver first book and use
4:46 the find_element_by_name.
4:52 And we just saw that it's named search titles.
4:57 Then another cool new thing you're about to learn
5:01 is the send_keys method, and that's a way
5:06 to get text into a form field.
5:11 So this code, search box send keys text to enter
5:15 which is Fluent Python will be the equivalent
5:18 of me clicking into this input field
5:21 and typing Fluent Python.
5:25 On here I had a little bit of trouble
5:27 because the drop-down would not show up in time
5:31 so I need to do a little bit of a sleep
5:33 to make sure that the titles pop out.
5:37 And I could do one second, but just be safe
5:39 and do two seconds.
5:41 Sleep already imported here at the top.
5:43 Now we can assume that the drop-down is visible.
5:48 And again, let's see what that looks like in HTML
5:53 do a right-click inspect, and we see
5:56 that it's an unordered list of list items
6:01 which are named ac_even, ac_odd, ac_even, ac_odd.
6:07 And look at how nice our web toolkit is these days.
6:11 That definitely has come a long way since the old days.
6:15 So we're going to get the list items, and get
6:19 the first one and click on it.
6:24 So the way to do that is use the
6:31 find_elements_by_class_name, but we actually want to have
6:35 the plural in case we want the whole list.
6:39 The list item class is ac_even.
6:43 I'm going to grab the first one and click on it.
6:46 And you shall see Selenium, it's pretty readable.
6:51 That click on that item will redirect
6:53 to the Fluent Python book page.
6:55 Once there, I'm going to see if the URL matches
6:58 what I expect.
7:01 And the way to get the current URL, so this one
7:10 is to use the current URL attribute and then
7:14 to assert that it pends with id, which you see here.
7:28 I'm also going to assert the title.
7:39 And that's just as before.
7:45 Let's run this.
7:51 We can see it in action.
7:57 Beautiful. And that concludes the testing of the logged-out page.
8:01 In the next video, we're going to log
8:02 into the site and test more features
8:05 that are only available when logged in.