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

Talk Python's Mastodon Michael Kennedy's Mastodon