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