#100DaysOfWeb in Python Transcripts
Chapter: Day 49: Selenium
Lecture: Selenium's find_element_by methods, test #1: homepage

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's dive straight in with the first test. And that will be to check the logged-out homepage. So the test should navigate to the homepage
0:14 and should check the title of the page assert that there is a login button and count the number of thumbnails.
0:23 So on the homepage we have 100 thumbnails of the last books that people have searched for. And we should assert that those are indeed 100.
0:36 Before writing some code, I want to point out that the documentation of selenium-Python.readthedocs.io is very decent
0:45 and especially you want to read through chapter four, locating elements because as you will see a lot of the code
0:52 that we will write deals with finding elements on the page, by name, by class, by link, even by XPath. So if you want to become serious at writing
1:05 test in Selenium, the find element by whatever or the plural, find elements, so to return lists of items by whatever, those are very
1:16 important methods you want to master. So back to the code, back to the first test. Test the homepage title.
1:26 And first I'm going to paste in what's actually expected. And that will be PyBites my reading list because we love books because when we go
1:37 to the site and we check the title here on the tab that's what we should assert. And one of the things I love about pytest
1:53 is that code is just shorter and more concise. And one example of that is the use of the assert statement. So here I can just type assert driver home
2:03 and driver home is the fixture we passed in which is an instance of the web driver which just visited home.
2:10 So when I'm here, the Chrome headless browser that Selenium opened, should be sitting on the homepage already. At that point, the driver home contains
2:20 a title attribute, which I can match with expected. Cool, so that's one test. And to run just one test, you can use the -k
2:32 of pytest and give it a sub-string. And here I gave it the name of the test. So here we see the headless browser opening.
2:46 It went to the homepage and boom, it passes. So it asserts that the homepage has this title. Awesome. How do we then look for the number of thumbnails
2:59 on the homepage? Well, we can look for the tag name img which is HTML for image tag. So images will be driver home, find_elements
3:15 and notice I used the plural because we want to retrieve all the elements, not just the first one. I tag name, image, and then we're going to assert
3:28 that the length of the return list equals 100. Again, you can just filter on one test with -k. See if it works.
3:50 Great. And the last thing we wanted on this slide was to assert if there was a login button. So let's make a test for that as well.
4:00 Test has login link. And I'm going to use a little bit of try and accept logic here. We imported an exception from Selenium called
4:13 NoSuchElementException. And what I'm going to do is I'm going to try and assert that the login link is there at this point.
4:24 So at the point where the driver went to the homepage and is sitting there in a logged-out state. So I'm going to do a driver home
4:38 find_element_by_link_text. And here it is, singular because it is only one link. And the link is called login. And that should not fail.
4:50 If that throws an exception called NoSuchElementException then something is wrong because when I'm logged out I always should have the login link.
5:00 So then I do pytest fail, should have a link called login, and then the test will fail.
5:18 Let's try that. And that passed as well. Perfect. Let's see if the tests work all together.
5:38 And notice how it launched the browser again. And that's due to the scope of my fixture. And when I finish this, I will explain why that is.
5:49 Okay, I can break now because there were only three tests and it's passing those as well. I don't need to see that now because they
5:56 don't have actual code yet. So the fixtures, here you can actually find a scope. For example, module, and then this code will only
6:08 run once for this whole module or file. It would probably work on especially the later tests we'll log in and log out, I might pollute
6:22 the state of the driver. So I'm going to go with the default, which is test by test. So I want the driver to start fresh every test
6:31 so that we start with a clean state. So that's why you saw the browser relaunching before every test. But again, it's not about pytest fixtures.
6:43 We have a whole article about it that explains the scope. So if you're interested go to that link and read all about it.
6:50 All right, so we have three tests already done. That's awesome. And in the next video, we're going to test an individual book page.


Talk Python's Mastodon Michael Kennedy's Mastodon