Selenium WebDriver with Python Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to this mini course selenium wave driver with python crash course my name is armaskinfu i'm a senior software engineer in test and here are my credentials i have a bs and ms degree in mechanical engineering from san diego state university right here in the heart of silicon valley and i transitioned to qa automation 2013 and i started teaching in 2015. so there you have it that is my credentials if you're new to my channel please make sure to hit that subscribe button and like this video also you want to hit the notification icon because i will start going live and to do live coding session as well as live tutorials so you want to know when i go live and get some reminders all right i'm pretty sure you're going to learn a whole lot from this channel so in this course i'm going to get you started writing selenium webdriver scripts okay telling webdriver is a huge topic and qa automation is a huge field so you're not going to be able to learn everything in a two hours course but you definitely are gonna get started and have ideas on how it works and what to do to continue to practice on your own okay so we're going to write few scripts here in this course and i'm going to give you ideas on what kind of scripts you can write to continue to practice and what kind of sites to practice on so the prerequisite for this course is basic python okay so this is python and selenium right selenium with python so you do need to know python if you take my full course i have an amazing full course that starts from scratch and teaches you python like basically starting from what's variable all the way to classes and objects everything you need to know about python then in the same bundle in the same course you're going to learn selenium from scratch okay so that's an option at the end of this course you can decide if you want to take the full course or not but you do need to know python in order to work with selenium right selenium supports different languages and python is one of them and this mini course we're going to focus on python but what i'm going to teach you is selenium not python because python is kind of big right all right and knowing what qa is obviously if you're here you know what a qa is you know what testing is you know what automation is so that's kind of a prerequisite i do have a course on qa if you don't know anything about qa if you're you're just exploring you you can take that free course uh just to learn about what qa is and what the job is like what the field is about okay feel free to check out supersql.com that's super software quality assurance superscript.com and check out the free course all right so at the end of this mini course at the end of this crash course if you like the teaching style if you like the video auto quality then by far the full course is the best selenium course money you can buy i can say that about my course i know it sounds a little bit egoistic but i really think it's a great force for somebody who's just starting out and it covers pretty much everything and really good exercises framework design and such so let's quickly go over what we're going to cover in this mini course okay in this mini course obviously there is an introduction section and we're going to say selenium preview first i'm going to show you just a script i'm going to run up the linux script see it work then we're going to talk about a practice site how you can create your own practice site if you want or how you can use there's a site that i created that anybody can access and we're going to talk about installing python in tools we're not going to get into details but i'm going to show you how to get started if you don't have it most of you guys have it and at the bottom of the description basically in the description you're going to see a breakdown of each of the chapters so feel free to jump around between sections okay or there is a time stamp of everything all these topics you see here there's a time time stamp for all of those okay then there is an html refresher for somebody who's completely new then there is running scripts and pad which is kind of important because to be able to to run a script you have to understand pads or how to all the drivers basically you need to download drivers we're going to talk about that then we're going to get into finding elements in action basically selenium stuff okay we're going to get into that that's basically the core of selenium we're going to quickly just review the selenium weights webdriver weights then we're going to look at we're going to write four different scripts four different tests one of three of them are tests and one of them is wave scraping because i want to show you different examples of how you can use selenium and how you can practice in a name then i'm going to talk about project ideas that you can do on your own okay so this is all in this course that's why it's going it's pretty compact and the whole point is to just get you started and you know have an idea on how to continue to learn on your own if you don't want to purchase any course right and the full course like i said is teaching python from scratch uh it teaches selenium web driver in detail from scratch and there is framework design so when we write scripts and for example in this particular course here in the crash course we're going to write scripts but those scripts are standalone scripts okay they're just individual files and they're not really reusable but then when you're writing automation for for a project or for a site you need a framework but framework makes code reusable it makes it easy you write the code one once and you can use it in several different tests all right and you're going to generate a report uh the framework can work with back-end automation and front-end automation because same way driver is front-end right but when you when you're testing some kind of application you also need to test the back end usually there is a front-end engineering back-end engineer same thing qa is also focused on front-end and back-end and some key ways do both like me at my job i actually i am responsible to test the back-end as well as the end and the frameworks need to support both right because they complement each other especially backhand will complement front end and i'll explain what i mean so the framework will do that and the the full course and bonus there is a mysql or sql crash course and i'm planning to add jenkins and docker crash course and in the course so look out for that it's a really good course and i would love for you to check it out and i'm going to add coupons in the description all the links you need they're going to be in the description um all the scripts we're going to write in this particular crash course you can download them i'm going to put a link to the downloads in the description as well and also you can just go to supersql.com and check out what deals are going on now basically this is basically the same slide i just added a few screenshots of the reports that we will generate really cool reports a lower report is one of them and pi test html report is the other one we're going to use pi test to build our framework okay we're going to build our frame on top of pi test and there is a plugin that regenerates a report and the report will have a screenshot when a test fails it would add a screenshot and i'll show you how to do all of that in the full course and for somebody who's interested in lore allure is a very popular report generation tool and we also generate a report that looks like this is actually this is an actual report from the framework we designed we run eight tests and one of them i made i made one of them fade on purpose so the report will look a little cooler all right so what is selenium right i'm sure most of you guys know what selenium is but it is a web test automation tool so it is primarily used to automate uh web apps okay on a browser so it supports different brow different languages you can write it in python in java ruby as well as c-sharp okay in my personal opinion python and java are pretty popular for selenium but plenty of people use ruby as well as c sharp and in this course we're going to focus on python so what selenium does is basically anything a human can do in a browser okay selenium can do it when anything well almost anything but pretty much everything a user can do selenium can do it so we're going to see an end-to-end automation for checkout as guest user okay end-to-end automation so we're going to manually execute it and then i'm going to show you running a script all right so we are going to run an into in checkout automation okay so i'm going to do it manually first and i'm going to show you uh selenium doing it so there's a site we're going to talk about it in a little bit but for now it's just demo store.supportsqa.com okay demo store that's pretty square.com so it's just a domain a subdomain for my main site and it's just an ecommerce site you're going you can create this i'm going to talk about it coming up how you can create this on your local machine it's just a wordpress site with few plugins well technically just one plugin woocommerce very easy to do and you can create it on your own machine and you have my sql and php and wordpress running and it's just super simple okay so anyways an entry and checkout as a guest user would be a user would come and let's say out to cart just randomly add one card we can see it's added i'm going to go there and since it's a test site it's not a real size almost a real site not quite and the only reason i say that is we cannot process credit cards right that's an issue so i created a coupon you guys can use coupon so this website is accessible to anybody you guys can check it out right now so if you do ssqa super software quality assurance 100 ssq 100 which is for 100 off it's a free coupon so it's going to make everything free so select on free shipping and then click on checkout all right and here you're going to fill out the name i'm going to say test last name user just fill out the required fields i'm going to do one two three main street i know that's not one two three and let's say san francisco california uh nine five zero zero eight even though that's a campbell zip code and i can do five one oh one one one one one one all right test at supersqa dot com is the email address i can click on create account if you want the automation is not actually doing that all right then i can click on place order and order receipt so what the selenium script does is everything i just did right now then it will come here and make sure this page is loaded make sure there is an order id and it actually takes the order id and checks the database to make sure this order is actually placed it's actually recorded in the database okay you can't see that because that's a backing that's what i mean when i say back in actually supplements the front end because the front end we just finished with all the front end we're good to go but it's a really good idea to make sure the order went through maybe something broke the front end did not send the message correctly to the back end and the order never got created in the database okay so it's a good idea to go check in the database cool so now let me show you selenium doing exactly what i did and it's going to go really fast so let's go to pycharm and this is the the framework we designed so i have the course material or the python section selenium section and a cisco test is our framework it has a bunch of things i don't want to get into detail but if we go to tests and go to end to end and check out this has a test id of 33 so i can just run that i can say pi test dash m for mark tc id 33 i can i can do html report 2 ht html is going to be demo.html and hit enter it's probably going to open the browser in my other window my other monitor there you go all right you can see it going there you go i'm not doing anything it added to cart they applied the coupon and click checkout i filled in the form it's going to click on place order and voila it actually went you can't see it but it actually went to the database and checked it out and this one actually run on my local instance of the site remember i told you you can create the site i could create one locally and that's that's what i ran that test against okay so demo.html if i was to open the report we just created open in browser let me open it in chrome i opened it on my other monitor there you go there is a report just one test demo.html okay so that's what selenium looks like so we just saw selenium execute an end-to-end test starting from going to the home page adding to cart applying coupon clicking buttons filling on the forum and confirming everything went through so that's the power of selenium something it would take you minutes so anymore can just do it in seconds all right let's keep going so about the practice site you just saw you just saw me do a manual test on demostore.supersql.com demostore.supersql.com and then you saw the automation running on localhost some port okay but it looked like exactly the same right it's because it was created in exactly the same way using exactly the same data okay so it's just a wordpress site running locally and using a woocommerce plugin woocommerce is one of the most popular e-commerce plugins for wordpress and wordpress is one of the most popular tools to build a website okay so if you already know wordpress how to work with wordpress go ahead and just like i said install wordpress you need a tool right to run the web server in the database on your machine there are several options uh the option i show on the course is one of them is uh currently i'm using map m-a-m-p and there is amps and you can also just do separate you can just install the server by itself and the database by itself but then you need to know how to configure those it's tough so map mamp is what i recommend but you can take this course again it's free uh this is the image i'm using for the course right now this might change so you can just just search just go to the site and search right now i will put a full link to basically this link i'll put it in the description okay so it's about an hour long but once you know what you're doing for me if i was to create the site it will take me about 15 minutes right now but the course is about an hour because i'm explaining every step it's adding sample data you saw there's a bunch of items for sale and there is a coupon so i go through everything how to create the coupon how to create the sample data how to make it look like the way it looks api keys because sometimes you want to access the apis that's one of the advantages having your own site is where you can practice back-end automation you can always practice selenium with public sites right you can just write a test on amazon.com or python.org or any website that you cannot really practice back-end testing you cannot access the back-end you cannot access the database and you cannot access the api right so creating a simple e-commerce website on your own local machine is one of the best options to actually practice and you can do anything you want to do with it okay so i wanted to give you a pointer on how you can practice on your own machine in this video we're just going to talk about our environments and the tools that we need okay so the first tool we need is python obviously we need python we need selenium which is super easy to install once you have python we can store selenium and i will just show you that in two minutes and we need some kind of ide so ide is integrated development environment we don't necessarily need ide you can always use some kind of text editor some simple text editor but since you are becoming a programmer why not use an ide and there is a free option for python a great one a really good one and it's completely free and easy to use okay so we are going to talk about all about those in this video i'm not going to show you how to work with everything here because it wouldn't make sense to take up so much of our previous our precious time doing the setup but i'm going to guide you on how you can set it up for most of you guys you probably already have all of this okay number one thing you can go to my youtube channel and look at how to install python for your individual environment whether you're on a mac or on a windows okay so it's basically youtube slice c super sqa okay so oops i i mistyped my own site name super sqa there you go so that is my channel okay here um there is about virtual environment so i really advise you to look into what virtual environments are so as a beginner they're not required but they're really awesome eventually when you start working on different projects virtual environments makes your life a lot easier you can have multiple versions of libraries installed for example selenium 4 is released as an alpha version right now not a stable version as an alpha version the selenium three is a as a stable version with virtual environments you can have both you can run selenium three incident four in fact in my full course i use selenium four because i have virtual environments and i i switch between selenium three and seven and four whenever i need to to show the difference even though the difference is not really that much at least as a beginner level okay so there is uh let me go to all videos actually uh there is a video about about python virtual environments it's a really cool one to just understand you don't have to do it just bet watch it and know what it is and then you have one how to install python two and three both of them running on a mac and how to do both of them running on windows okay so watch those if you don't already have it and look in the description for this this video i'll put the links to those those uh those youtube videos that i want you to check out all right so let's go open terminal to see if you have it or or not if you're on a windows just open cmd and if you're on a mac open terminal okay so the first thing you want to do is you can do which if you're on a mac which python okay python and if you get any kind of result like this which means you have it but guess what if you want a mac python 2 by default it comes with it because mac itself the os uses a lot of python stuff so you always have python so if we do python dash uppercase v python dash uppercase v will tell you the version you can try python3 uppercase v will also tell you the version python 3 you will not have by default you would have to install it so so just if you don't get any results like this just go to python.org install the latest version of python3 it might be python 10 or python i mean python 310 or 39 or something just install it nothing special about it just install it but watch the youtube video on how to do it if you're on the windows the which command okay you don't do what you do where where python here is not going to know what where is but and on a mac you do which to find a program you do which like it just shows you where that program is installed on a windows machine you can do where i've noticed when you do wear if you only get one result i don't know what that is but it's not really python if you if you get two results then you have it okay and for this course you can use python 2 or python 3 but preferably python 3. why not use the latest one okay again watch the youtube video install it on your machine it's very simple nothing nothing fancy about it you just go to python.org and download and install all right with regards to the ide so the ide we use is going to it's called pi char okay if it's the first time you're hearing it it's p y c h a r m okay this is it this this logo here is this icon that is pie chart so go to the website it is by jetbrains jetbrain they make a couple of different ides okay so just click on download and there is two options let me see the first one that gives you yeah so when you click on download you get professional and community make sure you download the community one okay that's the free one professional is not free there's a free trial that would let you install it and run it but later on you're going to keep getting a bunch of notifications okay the community is free and that's all i've been using for years this is perfectly enough and we don't we don't use anything we don't do anything crazy with it okay it's just editing our code but it knows what code is it knows what python is so when we're writing python code it makes our lives easier that's the whole point of an ide all right so for it supports windows and it supports macos okay so just download the community edition and this is what it's going to look like this is what it's going to look like except you won't have anything here this these are all different projects i have for like just different things okay and a project in pycharm is just a folder nothing nothing into it so you won't have any of this but this would look similar i mean obviously the newer version is actually already 20 21 there's a newer version out i need to install but it would be very similar at least you would give something that says open and create a new project okay so for this course let's let's already create a project so i'm going to click on create a project so like i said a project is just a folder so i'm going to create a new folder i'm going to click i'm going to click on this folder icon okay and in the home in my home directory i'm just going to click i'm going to say create folder and i'm going to call it selenium py crash course okay that that's just a folder and you could create the folder first before you get to here whether you're in a macro windows the same thing then you can just choose this the folder here click open and existing interpreter this is with a virtual environment stuff i was talking about if you if you understood what virtual environments are and if you want to use it you can actually select this which will create the virtual environment or you can just say use the existing interpreter which will use your system python your default python okay so it's using python38 for me i also have 39 installed but this is fine so i'm just going to click on create and this is what the project looks like in your case this might not be open when you start and that's it you have a folder an empty folder and we're going to start to write our code in this folder here so we just created a project in pie chart all right so to summarize install python 3 whether you're on a windows or a mac just install python 3 install pycharm those two are the only things we need oh selenium to install selenium so with python uh pip comes by default okay in fact for his python 3 we want to use pip3 and in python when you want to install libraries dependencies you just use pip it will go find whatever you need and you'll install it so for example to install selenium pip3 install selenium okay hit enter voila that's it now i already have selenium installed okay actually this one says already satisfied because i already installed it before and it did install the version 3. so you can do pip freeze to see the list okay pip three freeze actually my python uh my python two have issues so i i get a bunch of gibberish like that but if i do pip3 freeze i get a list of everything that's installed or i can do pip 3 list you get a list of everything that's installed as you can see selenium is installed selling name 31 something okay all right so this is all we need for the course now we can actually start writing some code in this video we're just going to have a quick html refresher so if you know basic html this is going to be extremely basic for you so you can just skip this video okay we are just going to go over what html looks like and what it is just very quickly just to go over for those who are completely new because when you write selenium tests you have to know a little bit of html right so what are we doing with selenium with selenium on the page we are finding elements and doing something with those elements right find element and do something with it or to it so those elements are html elements so it helps to understand what html looks like and to know how it actually functions so we're going to do a quick summary of what html looks like and what it does so if you are completely new to html i think it's a good idea for you to do a very basic uh tutorial okay just go over some html tutorial because this is not going to be enough but it's going to be enough to do what we do in this course all right you don't need to you don't need to be a guru of html you don't need to know a whole lot about html because html's own can be huge you can have a whole lot of topics but just the basics you should know all right so we're going to create a simple html file so i'm going to create a folder actually i'm going to say html refresher okay and i'm going to create a file so you do right click new here there is an option that says html file try see what happens when you do that but i'm just going to start from just an empty file so i'm going to say html refresher.html all right so html files they always end with html or htm but lately so i only see html so that might be old school so do your file with that html so the file always starts with html tag okay and as soon as i close it the pycharm gnosis and html so it closed it for me i didn't type this in as soon as i typed this it did this so every time you have a tag this is called a tag okay this is an html tag this is an opening tag and whenever you have the same exact tag with a slash it's a closing tag so i just opened html closed html and usually it has two major sections there is a head tag okay see how it finished it for me and then there is a body tag okay so in the head usually this is information that's not displayed on the page you don't see what you see in the head you don't see it in the page you have things like title for example so i can say my html html title so this is the title of the tab if i was to open it i can just copy this uh the path so you can right click copy absolute path go to browser and just type it in in the address and it will open nothing it's empty but you can see the title okay the title showed up here then other than that in the header is just a lot of external files like references to a css style anything external you need it will go in the header it's just a lot of stuff that goes in the header but that's not displayed on the page so in the body is most of the things we actually see on the page okay so my keyboard is acting up all right so here for example we have an h1 tag which is a header okay h1 and it's the biggest header headers start from h1 to h6 so i can say largest header okay and h6 is the smallest header okay so now if i go refresh this page now i have this two largest editor and smallest data h1 is the largest h6 is smallest so those are elements on this page that's one element this is another element if you do right click and do inspect okay now you can see what the html looks like so this is exactly what we wrote right we have the header and the page highlights so when you hover over body the entire body you expand it h1 you see that that's h1 this is h6 play around like this okay so that is an element h1 is an element h6 is an element so if we're writing selenium test and we want to make sure that we want to validate the header we have to find that element first then there are different ways of finding it selenium supports different types of locators to locate elements and we're going to talk about those in this case for example i can use css and i can say find h1 that's right underneath body i can just tell selenium that and you will find it and you will just if i can just say get the text of that element or like if it's an input field i can say go type a text into that element i can have commands like that okay let's just do a few examples for example another one uh usually you see a lot of div is div is like just just organizing it's like a little box on the page okay you can't see it unless you specify saying oh i want to see it like you add a border to it or something let's say i do a span and i say input name okay that's a span element now i'm going to do an input element and the type is going to be type it's going to be text and i can say placeholder is going to be your name or something your name okay let's just refresh the page see what happens there you go now we have another element we have an input name and we have an input field so we can tell selenium find this field okay here we can see it in here there is an input we can tell selenium find this field and get the text or input text to it or find this label the the span and verify the spelling is correct we can do that uh we can give it an id like this input field i can give it an id which is an attribute i can say user name okay now if i refresh this this this this field now is going to have an attribute of id one of the best locators okay selenium supports id so i can tell selenium get the element with that id okay so this is what html looks like the whole page is built like this a bunch of tags okay collection of elements we have one element two element this entire div is an element inside of that element there is another element there is another element it can be just like thousands of elements deep you really never know okay so that's the basics so let's go look at a real site that's running for example if we go to supersql.com the same thing you do right click inspect okay now we can see all the elements all the html this is a whole ton of stuff here okay and in chrome you see this little icon with a box with a pointer if you're using a safari there is a like a cross yeah basically a cross and every browser has this type of functionality okay so if you click on that now you can select the element you can see it highlighting every time i move it it highlights the element if i move a little bit to the corner it highlights a bigger row so if i click that now i get a focus here so this is telling me it's a div it has a class and i can expand it and in there there's a ul ul is unordered list okay so they used a list for the header if i expand the ul there's a bunch of lis okay all of those are elements if you look at the header so i'm going to zoom out so you can see everything um so every time i move my pointer on each element i can see it highlighting there or i can select this thing again and let's say i want to click on my account so i'm going to click on my account now my account this is the element it has an id it's under li and it's an a tag and an anchor tag and i can tell selenium oh find find li that has this id and inside of it find the a and then click it so i can tell it tell selenium find li with id of this and click it and selenium will click it okay so generally that's the idea that's what we're doing with selenium we're finding elements on the page and we're doing something to it either we get the text and verify the text matches or we send the text to it or we click it or a few other things basically anything a human can do selenium can do almost anything okay take that with a grain of salt all right so that is a quick refresher of html it's not a whole lot of information that i just gave you but it's if you follow that along it's good enough to be able to write the script style we write in this video because the locators we are going to just right click copy x and it's just going to give you selector or expat we're going to use those two selectors we don't have to create your own okay let's keep going in this video we are going to write our first selenium script okay we're going to see what we need to do to get a browser to open so we're going to set up it's almost like setting up the environment all right so let's go to pycharm and let's create a new file right right underneath our project root i'm just going to keep creating files here so i'm going to say first selenium script all right so the first thing we always do is import webdriver from selenium import webdriver that's the first thing we do sometimes we we we need to import a few other things for now that's all we need now we need to create an instance of the browser so we just create a variable and we're going to call it driver this is very common like all the code you see people wrote that they use the word driver for the variable to hold basically the browser or the web driver okay so we're going to do webdriver.chrome and that's that really is it so this will create the driver object then we'll do driver.get and we're going to give it the url http uh let me see demo store.supersqa.com okay so typically this should work but now oh i have my hand now it's not going to work so let me show you the error so i'm going to try to run it all right and it's going to get to a really clean error message okay so just look at the last part of the error message it says selenium common exception web driver exception message chrome driver executable needs to be in path see this documentation so you can actually go into that documentation and it would open this up for you okay so we'll come right back to this and to talk about it let's try firefox so i'm going to duplicate it command d to duplicate comment that out and i'm going to do firefox okay and let's run it again okay so it's trying to do something um surprisingly i'm not getting the exception okay it did open uh firefox so let me see if i open my terminal i was hoping to show you the error because it's not going to open for you uh which gecko driver okay so i do have it in the past so the whole point is to run the selenium web drivers we need to download drivers okay for chrome we need to drive to download chrome driver for firefox we need to download a gecko driver and one of the options is to add those drivers into the system pad so when the script runs it will be able to find them okay so right now i did which gecko driver on windows that would be where gecko driver and it just showed me it's in this in this location so i'm going to remove that actually i'm just going to rename it okay and so now now it's not in the path so selenium should be should fail just like we saw it failed for uh for uh chrome driver okay message gecko driver executable needs to be in pass all right so you need to go download gecko driver all right so that's what i wanted to show you that you need to have the gecko driver or chrome driver in your system path now back to the browser so even if you don't click on the link you can just google download chrome driver and usually it's the first hit okay and then you go you're going to have to download the version so let's see i'm going i'm going to click on chrome i'm going to say about chrome to see what version i'm at i'm at version 89. so i'm going to download the one that supports version 89 so just click that and select your machine i'm on a mac so i'm going to do mac for window users two windows so i'm going to select on mac it's going to download the zip i'm just going to let it go to downloads save all right i'll click this and it will extract it okay and now i have this file that says chrome driver so you do a very similar thing for um gecko driver okay so i'm going to get the pad so if you know what pads is system pads you can add it to your system pad so one thing to look up is if you want to research this research system pads how to add uh how to add two system paths okay it's the same thing for the same concept for windows or and for mac okay so that's important i'm not going to cover it here but it's a good idea to know how to add to system pass but for now let's just get a full pass to the chrome driver so i'm going to right click hold down option copy the pad name so in windows you just go to the address bar and copy the whole the whole location so let's now we're going to work with chrome right so i'm going to uncomment chrome so since we don't have it in the past we don't have chrome driver in the past here you're going to specify executable path is going to be the path i just downloaded okay so that is the key driver like webdriver.com and you pass an executable pass and give it a pass to where you download it if you add this into your system pass like i just told you look into that then you don't need to specify because it would automatically be able to find it okay so let's let's run this okay so the first time you do that you're going to get this error chrome driver cannot be opened because your comb driver cannot be verified this is on the latest versions of mac okay so copy that error cancel it don't move it to trash cancel it go back to the browser and literally search for that error okay just put the whole error and the first result at least right now the first result there is this command here okay it's gonna it's gonna quarantine something and you're gonna give it the full path okay so i'm gonna add it here so so you have it as as a note so in fact the solution is this and we're going to give it this full path here okay so you have it in your notes i'm also actually wanna put the error also there so you know uh there you go so let's execute this now i'm gonna take this i'm gonna open my console or my not console terminal and just execute that and voila now let's run it it's that that was some security thing okay all right it worked so it opened the browser and it went to demo store.supersql okay so now we have a driver object we can do a lot of webdriver stuff to it find elements here we can just actually just show you a quick example of getting the title i can say print driver dot title okay so driver is our web webdriver object and there's a bunch of methods we can do which we're going to look at in the next video so let me run it one more time and i want to see it printing the title of the the website so if you look at it it just did print it all right so we just ran our first selenium script hopefully you were able to follow along important steps is download chrome driver if you want to run chrome or gecko driver if you want to run firefox and the exception will tell you exactly what the issue is once you download it on mac user you might get this error so execute this command okay and when you download make sure you download a version that matches your chrome and remember chrome i usually by default auto updates so it might work today and it might not work tomorrow because you'll get your chrome driver is not going to update but chrome is going to update itself okay keep that in mind that's a common common issue that comes up time to time all right let's keep going finding elements okay in this video we are going to talk about how to find elements and how to interact with those elements you saw the slide before okay so what do we do with webdriver we always find something on the page and do something to it or with it on the page just like what we do manually we don't notice it but what we do is visually with our eyes we find something and on the page and we do something to it right we make sure it exists we make sure it's visible we make sure the spelling is right we make sure when you click it something happens we make sure when you type into it something happened when you make sure when you drag it something happened right that is what testing is so that's what selenium webdriver actually does all right and it gives us several options on how to find elements on the page okay so there are two methods find element and find elements so the difference is one is singular the second one is plural other than that they work in a similar way so when you say find element and you give it the strategy on how to find it and you give it the locator it's going to give you one element the first element that it finds on the page that matches that locator in that strategy if you say find elements with plural it's going to find all the page all the elements that match what you specify and it will give you a list the first one is going to give you a web element it's just going to be one item just a web element okay that's that's the object type the second one is actually going to give you a list all right so we're going to see examples of those then the different strategies to locate are the ones you see here in purple okay there are about eight different types of locators so there is an you can find by id you can find by css selector you can find by expats by class name by link text partial link text by name and by tag name okay those are what selenium provided to us that's what selenium supports okay and we are going to see examples of those in action at least some of them we're not going to go over everything but some of them so finding by id is the the easiest one and the best one because if the page is designed properly there should only be one element that has that one id it's the whole point of an id right it's unique there cannot be multiple elements having the same id so if you find an id it's a good way to use it because you just find that one you know for sure what element you're looking for all right css selector is the most flexible one i in my opinion it says and expats they're both flexible and that i use this is the most frequent okay if there's no id my next choice is css selector then the third choice would be xpath okay expat is also great and it's really fast for selenium but it is more prone to breaking more prone than css to breaking and i'll show you why that is then there's a class name uh class name the issue is there is it can be multiple items with the same class usually that's the case so it's useful sometimes but i don't use it that often and the link text and the rest of them they just self-explanatory and you can explore those and whenever they come out when we do our practice we will use a couple of those right the id css selector and expat we are going to use very often all right so one thing i want to show you is if you're looking at a little bit of an older code you you might find a method that says find element by id find element by css selector find element by css uh i have a typo there by expat point element by expat and all the other ones and it just says find element by something find element by now in selenium four those methods are gone okay they're going to be deprecated so if you use cell and if you use those and selenium four you're going to get a warning i just want to bring that up so what you want to use is this one just find element and in the parenthesis you give it what you want to use all right so let's jump to code and actually start working on that all right so here is our project uh let's see what i do i can find my pointer so let's create a new file right under the root i am going to say finding elements example all right so the first thing we do we import driver from selenium import webdriver we need one more thing we need to import we need to import that by class okay from selenium.webdriver.com and that by import bin this is a class all right then we're going to create our driver object driver is equal to webdriver.chrome and we need to give it the executable pass or you can add it to your pads we talked about that right so if we go to our first selenium script we have our pads here so i'm going to copy that so i don't have to go find it so i'm going to type it right in here all right then we're just going to say driver driver.gate http demostore.supersqa.com all right let's test this out i'm going to right click and go all right i should there you go browser opened and voila we have it here okay so i'm going to close this also at the end of your script you should put driver.quit okay this will quit the process and driver.close will close the browser so what the difference is quit will quit like if you have multiple browsers open sometimes you run a test and you can you can have a new tab a new window open so a quit will close everything but close will close just the one you're actively working on okay so i'm going to comment that out but i'll leave it for your notes in fact i'll comment this out too so we can look at we will close so quick we won't get a chance to look at it so i'm just going to close it okay so now let's go to the site and actually look at some some locators so the first example i want to show you is how to locate by id so if i right click inspect and i'll look at this cart here okay so if i scroll up here i'm going to click on this pointer select the cart so the ul here the entire box has an id of site header cart okay i'm going to copy that so i'm going to say i'm going to say let's say cart is driver find element we talked about right by that in this case what do i want to use see the auto suggestion shows me everything now i'm going to use id and i'm just going to paste the id down i copied as easy as that okay now let's print what cart looks like like what what did what it is i can say print cart i can also say print type of cart right we want to see what type of object cart is so i'm going to run it and we should get a print out of those two all right the browser open it loaded should be done by now scroll up there you go so when i say print cart it printed this that's basically details about the web element and when i say type of cart it printed this which is a web element that's what we're looking for we get a web element and now web element has its own methods i'll show you uh right in a few minutes it has a bunch of its own methods right for example if we want to get the text of the cart i can say i say cart text is going to be cart dot text okay so we use that very often you have a web element you do that text and you'll get the text so if i say print card text and then let's run it again there's a faster way of showing you this but for now let's do let's just run it one at a time all right so it finished and it printed zero zero zero zero items so if we look at the cart it printed all the text in the cart okay so this has been an example of by id very simple when you use id it's the simplest thing you find the element now this is a web element there's a bunch of things you can do to it we'll talk soon and one of them is getting the text of it so a lot of a lot of automation actually a lot of tests is you look at you check the text of the element that's displayed so that text is common and you can assert what the text is if the text is the right text okay so another example by id is let's close this so let's go back my let's close this too i just have since i have this open in fact let me i'm commenting now so i don't have too many browsers open like right now i already have so many browsers open so another example by id is let's look at the search field here so let's say i'm i'm trying to test search okay so i want to come in here and find the search field element now i come here and try to find what the best locator is as soon as idea id i don't have to look no more because i know i can use id id is one of the best options okay so another example of pi id here by id so i can say search field is going to be driver.find element by id and i'm just going to give it the id all right now once i find the element what do i want to do i want to send keys i want to type into it right so i can say search field dot send keys and let's say i want to type in hoodie okay so now in fact this time i should i don't want to close the browser so you can see it now let's run it and it should it should do exact same thing open a browser go to the site find the element and type into it you see that right you see my pointer didn't move but i didn't do it it typed into the field okay another thing you can now after you type that field you can send the enter key so in fact let's import keys so you need to import from selenium.webdriver.com dot keys import keys now i want to send the enter key because in this case there is no button to click there is no submit or search button right our only option is to send the enter key all right so i can say search field dot send keys keys dot enter okay hopefully you're i you're able to follow i'm moving a little fast because of time constraint and we're going to see all this when we actually run actual tests and examples there you go it actually searched right it typed it into it and it hit enter now we know it worked because we got a search result for hoodie all right so that was an example of use searching by id now let's look at an example of searching by css okay so let's say i want to go i want to go find just link my account and i want to click click on my account okay so again pointer find my account search it all right now just got the focus we got the a tag and we we got the l tag whichever one we want we can see it does not have it does not have an id right it does have a class but there is multiple items with the same class so using class name is not our best option but and say it says you can use the class okay so i'm not going to talk about how to build css um you can look it up just spend a little bit of time trying to learn how to build your own css and how to build your own expats in the full course there is about an hour worth of content just discussing how to come up with a stable css and a stable expats but for our purpose for this short course all you have to do is right click it copy and say selector copy selector click on the selector okay so now so i'm going to do this is going to be by dot see it says selector okay so what are we looking for is my account link right my account it's going to be driver.find element by css name no not class name c it says selector and i'm going to give it the css selector this is what i got when i did right click copy and copy selector that's what i did so what do i want to do now i want to click on that element so i can say my account dot click all right so now i can just run this yeah we should see the browser open okay i clicked it it went there all right perfect so it takes time to actually wait for a browser to open and see it right let's say you're automating and you wrote a lot of code that goes page after page after page it runs for like you know a whole whatever three minutes and now we want to test something on that page every single time you don't have to wait for that to happen so what i want to do is i want to put a breakpoint here let me import pdb import pdb okay pdb is a python debugger if you don't know pdb look it up it's a very useful tool so i can say pdb dot set trace if you do say trace it always just breaks at that point okay so i'm going to run this in fact let me not click this i want to stay in the home page okay so i'm going to do run and then it's going to end up in a breakpoint and i want to show you a few things once we get to the breakpoint okay all right so we actually searched for hoodie i was hoping will be in the homepage so let's not hit the enter key either and we can see it's a breakpoint here i'm sure let's just run it one more time all right and another thing i would like for you to do is just check out xpath so while this is going it's actually done but let me show you here we we use css but you can do right click copy and you can do x pass or full expat so let's say copy xpath okay so we can come here and we can do uh for this one we can do exact same thing but we can say xpath and we can put in this expat which is a relative expat you can see it it starts with double slash so it's relative so i found something with an id and from the id it went all the way but if we do right click copy and full xpath okay this is just for an example i'm going to duplicate this and i'm going to paste this now this one is a fully expanded it's not relative so they all find these three things will find the same exact element one is css one is relative express one is absolute expanse or full expats okay remember that all right so now what i want to show you here is in the breakpoint if you you need to be familiar with the the method dir okay it's a python thing dir will give and you give it an object so let's say i do dir and i do driver okay i give you the driver object is going to give me all the properties and methods of driver so you get a hint of what you can do with the driver so we have driver okay we have find elements we we we've seen that this one five five elements buy something we told i told you that's going to be deprecated in selenium 4. let's say this there's a there's just a lot of things title we saw an example if you have multiple windows you can switch to windows switch to frame uh let's see current window handle just so it's a good idea for you to go through this and get familiar basically these are all the methods that selenium offers okay so all the things we we looked at send keys it should be somewhere in here click should be somewhere in here handling alerts okay so dir and driver even even when you have an object for example let me get the search field okay let me run this in in pdb right here okay now if i do type of search field it's going to be a web element okay if i do dir or search field and now i get all the options i can do okay i can take a screenshot of the element um i can send the keys like we saw before i can get tag name i can get the text i can check if it's selected or not let's say it's a radio or if it's a check box you can you can do is select it and it will get a true or false you can find element okay once you find an element you can find a child element from that element all right so remember that that's very useful like let's say you find a table and you save it in a in a in a variable then you can do that variable dot find element and find all the rows okay so you can do the same thing to the element whatever you do to the driver you can do the same to the element keep that in mind it's very useful and you don't see it very often but i've as it have become very useful for me okay so those are what i wanted to show you there is a lot to talk about that i've gotten you started on how to look into it especially this dirt command with pdb is very very awesome it's very useful uh go ahead and try find elements with with plural okay for example if we go to the homepage um yeah the homepage right if we look at these products they all have they're in a lie and they all have a class product okay so i can say i can just say my products it's going to be driver.find elements with plural okay by class name and product okay if i do my product it's a list you can see right it's a list of elements if i do land of my products it's 16 of them because there's 16 that showed up on the page okay so i did plural since i did plural it got me all of them i could write exact same command i can write exact same command with singular right if i do oops let's say driver.find element by class name and i give it product that gave me only one item right it just give me the first one i found all right keep that in mind it's useful if you want to get multiple elements you use plural rs singular for one element all right so that is that's basically how to find elements and how to when you find the element how to um you know do things to it you can send keys to it click it get the title and there's a bunch of other things that we saw what you can do you can take the element you do dir on the element and you can see what you can do to that all right so that's the basics and now we can go actually apply this and write some practice scripts in this video i just want to quickly talk about weights because weights are one of the common issues when working with selenium right a lot of times we you're going to get an error that says element not found but when you go look at the page the element is there so what happened is it's basically a race condition selenium is checking for the element before the element exists sometimes the element is loaded with ajax sometimes there is a delay and such so there are selenium weight there's two kinds of weights there is an implicit weight and explicit way so for implicit weight this is what you do like whenever you create the driver object just add this line and that will give it an implicit weight so what this line is saying is telling selenium wait 10 seconds before you you say i can't find the element and so it's not just sitting there waiting is it's actually checking the element every half a second up to 10 seconds okay so if it finds it on the third second then it's going to continue all right so this is implicit weight and this is common it's easy to use so we're going to use it for the for the examples and the other one is explicit weight and explicit weight is a little bit more involved and this is how you do it so this expected conditions is a class that comes with selenium and there's a lot of options okay so you want to go and check out what those options are now we're not going to use them here because we just take time to explain that in our framework we actually use explicit ways it's more more recommended to use explicit weight okay but implicit weight is the easiest one and for the type of scripts we write we are going to use those so i want to bring this up here and so you know how to deal with it when you get that element not found issue and clearly the element is there but you get an exception saying element not found so two kinds of weights implicit weight and explicit weight those are what you wanna check out all right in this video we are going to write a test case that automates python.org and verify a link actually works okay so i want to use python.org for this example so i don't want the whole course to use the same exact page for everything i want to expose you to like different pages okay all right so let's go to so let me manually show you what text we're going to run all right so we are going to go to python.org our script is going to go to python.org click on pi pi and verify the right page loads okay all right and i'm going to move a little fast just you know you already know i'm trying to cram as much information as i can in this two hours course all right it's for your own benefit so let's create a python file and let's call it the verify underscore pi pi link uh python.org dot py all right so from selenium import webdriver from selenium.webdriver.com.buy import pi so driver is going to be webdriver.chrome and executable pass i have it in here all right that's what i just downloaded it in an earlier video and i kept it there i don't i didn't add it to my pad so you can do whatever works for you all right so let's get let's let's make sure uh no let's go to the site so i can say driver dot get okay so let's go to python.org copy this and put it here now let's make sure we get to the right page so i can say uh current title is the driver.title and expected title expected title is welcome to python.org okay that's the title that's the space you can see it here right that's the page that's we know we're in the right page if we get that title so i can say assert or i can say an if if current title not is equal to expected title then raise an exception i went to went to [Music] python.org but got wrong title i can say current title just just give it um whatever like it's a meaningful message okay format format current error right just a meaningful message okay so let's keep going we need to verify this but this is very very simple okay now we want to get the locator for this link so let's right click uh inspect all right so i'm just going to do css i'm going to pipe i i'm going to take the link and i just going to copy the css selector i'm going to do it the easy way okay so i can say pi pi header link locator is going to be that all right then i can say pi pi header link element it's going to be driver dot find element by csa selector pipe locator all right and then what do we need to do we need to click pipe header link element click okay at least this far let's test it out so i'm gonna right click and go and it should open a browser there you go browser opened and clicked in voila so once you clicked it what do we do to verify the right page loaded i think i can get the url and check the url there's a bunch of things right you can do this element this element look for the title you can check the same thing we did first get the title a lot of things you can do okay just for the sake of showing you different types of examples i'm going to get the current url and verify that okay so earlier we verified the title now let's do i can say current url the driver dial current url so if you want to know what are our options i showed you right you can do driver and you will get a list of all the methods and this is the current your the one of the options okay then the expected url our expected url is this all right so this is the expected url then i can do an if statement like earlier i can do a cert current url is equal to expected url and if not the error is clicked on pi pi but the url opened words oops i can do current url that's it and at the end if you want you can just print pass and you can close the driver driver quit okay this time i don't want to close it i'm going to comment it all so let's run this test it should be opening a browser there you go there you go all right let's see if there's any nope no exceptions it passed okay so we just finished our first test and we're going to write scripts like this a few more times i'm moving fast for obvious reasons all right great let's keep going and do another one in this video we are going to automate another test kit so we're going to write another script for python.org we are going to verify the search actually works all right so back to pie chart and let's create a new file i'm going to be a python file verify search works and i'm just going to say python pork okay so what are we going to do let's do it manually first we have this still open so we're going to come here search into here i'm just going to search with some some word like testing or something all right and then make sure this table shows up basically at least one element i expect at least one result so i'll just make sure uh what the first one shows up all right so let's open the other one and copy a bunch of code from there so the first thing we did we did here is we went to we opened a browser went to the site and verified we landed on the same site so let's get this we need all this and paste it here so this is when a framework comes in play right when you when you have a framework you don't have to copy paste every single thing over and over again now you just make a function call or something like that that's the whole point of a framework makes things a lot cleaner right so now what we need to do is we need to find the search field so let's actually go back to the home just to make sure the elements didn't change so we're going to do inspect and let's see what our options are all right so it looks like we have an id right so it makes our lives easier we don't have to use xpath or anything so i can say search field id is that and the reason i'm saving it in a variable is to show you that's that's a good practice normally you would have all the locators in a different file and you will you would import them okay or you have you have it in configuration file somewhere so if you need to change it you only change it in one place now if you have a lot of tests for search and if i have hard coded the id in this script then what happens when the id changes now i have to find all the tests and change them right so that would make sense so you would create some common place for all the locators and then you will just import those and that's what we do in the framework that we actually build in the full course okay so i'm going to say search field element it's going to be driver. find element by id search field id all right so what do we want to do we want to type into it right we want to send keys so i can say search field element dot send keys and let's just say testing okay let's just like i did when i did manually then what we want to do we want to click on this go so let's let's find that let's see if we have an id we do have an id of submit so we can use that okay so i can say go button i can say go button id is that then i can say go button element is going to be driver.find element by id and i'm going to give it go button id and i want to click it and go button elements dot click all right so let's run it let's see so far we haven't made any mistakes all right so it's going to start to run browser open typed click go perfect so i went to this page now i can do inspect here and i just i like to put this at the bottom right there so we have each row let's see this whole thing is a ul and under the ul each each search result is an align so let's get the first li so i'm going to right click and i'm going to copy x pass okay so i'm going to say first result expat is going to be that so i can say first result element is going to be driver.find element by x path and first result x pass okay so what do i want to do now now we i find that element the element might always exist so it's right the li might always exist so what i need to do is verify it actually is displayed all right so there's a method that says is displayed in fact let's put a breakpoint here import pdb i'm going to do an inline import pdb say trace and let's run it so we can actually see that we can interact with the element okay all right no now we have a breakpoint so if i do dir a first element okay so one of the options should be is displayed so let's look at it is this looks like it's alphabetical and is displayed all right that's what we wanted we want is displayed is enabled is selected so for example if we do first result l is displayed we get a true so that's what we want to verify right so we go back to our code and we can say assert first element that is displayed okay i can say after searching the element the result the result is not displayed right we can do that or if i say if that then i'm going to say print pass else i want to raise this exception all right just a little bit else raise exception and that's going to be my exception you can do either way you can do the assert or you can do display all right so but that's basically it for this test to verify that the search actually worked so let me do continue on this one see and continue and let's let's get rid of this and let's do just run the script and hopefully the script will pass unless we have a typo somewhere it should pass there you go okay perfect so our second test is done one thing we should have added sometimes you could run into this problem is always add a timeout implicit way so driver that implicitly wait i can just say 10 seconds so what implicitly weight does is i know i've talked about it it's going it's going to wait for 10 seconds before a failed saying element is not found and it's going to check every half a second up to 10 seconds okay so as soon as it finds the element it will move forward hopefully we'll come up with an example when that actually saves us in this case our test was passing without without the implicit weight but i do want to show you an example the last thing don't forget is driver that quit because you want to save on some resources all right welcome to another video this is your instructor adamaskinfu from supersqa.com in this video we're going to look at another example okay we're going to write a test this one we are going to write a test where it verifies an invalid coupon actually fails and gives the right error message okay so as always let's execute it manually first let's go to so basically that's our code we'll do right go to demostore.supersql.com and we need to have an item in the cart to be able to apply coupon okay so click on are to cart and then we go to cart either we can click this or this but in our test we just go straight straight to the url slash cart all right then once we get here we are we do a few things we have to make sure there is an item in the cart because selenium moves fast uh too fast and sometimes it gets to this page before the item is actually in the cart then we're just going to put some random some random string into the coupon code click apply and make sure we get this message saying coupon whatever we added does not exist okay so that is a test case now let's go to pycharm and start writing the code for this okay so i'm going to create a new python file verify invalid coupon fails all right okay so let me minimize this all right so the first thing we need to do as always import in fact yeah let's do it uh from selenium import webdriver from selenium.webdriver.com.buy import but okay we always do this now this one i want to do it a little bit different a little bit more structure a little bit more proper okay so i'm going to use functions and i'm going to do a bunch of function calls so at the bottom i'm going to do if name is main so basically this is a main this is a block that's going to get executed when you run this pipe this file specifically because in the in python i i don't know if you know this is i'm just rambling but in python when you import this file if you were to import this file you will execute everything in there right but if you have something under this block when you're importing it it doesn't it doesn't execute it but when you execute the file itself it executes this part okay so you can have functions in here that you can import into a different location different file all right so we need a function first to go to to open a browser so i can just say open browser okay so let's define a function dev open browser all right it doesn't take any parameters so i can say driver is equal to webdriver.com and we're just going to give it the pads like we always do and we're going to return driver okay so we got to store this i'm going to use the same variable name driver open browser okay so we need another function that says go to home page go to home page so this is kind of like a page object so normally what you do is you create a class for each page a page object pattern basically in the full course we are using page object pattern okay we're going to we're going to design our framework using page object pattern and in a page object pattern every page will have a class and that class will have methods of things you can do on that specific page so when you write multiple tests all you do is import that class and we have everything you need to do on that page okay hopefully that makes sense i went a little fast all right so let me go to define this we're going to do dev go to home page this needs to take the driver object okay we can give we can give it any name but i'm going to keep calling it driver because it's easy to follow so i can say driver.get http store.supersqa.com right that's all this needs to do so it needs to pass this into it all right once we get to the home page we go we're going to have to add item to the cart right so at first item to cart okay and this needs to take driver as well and we're going to define this dev add first item to cart driver all right now so we need to find the add to item button right so let me exit this out let me go to the home page all right so i'm going to do inspect and i'm going to go to r to cart so r2 card let's see if there is an id there's a class right r2 card button i can just take that since i want the first one i can do that in fact in on the console if i do dollar dollar and code for a class you do that okay for id you do the hash because i can say class that and hit hit enter basically this is the way to test out css okay if you want to test out css in your browser this is one of the ways if you do dollar x that's a that's a expat if you want to test out expats okay this this is a very nice trick to know but you have to know how to build your own css to be able to even have the need to test it okay that's why i'm not talking about it it's a longer topic that's why i avoided it in this course i just right click uh in fact let me be consistent but i just wanted to show you this but normally what i've been doing in this course to save time is i just find this this is it i just right click copy expat or selector i'm just going to do copy selector i'm just going to do that i'm going to keep doing that but hopefully you understand in real life that's that's i would never do that i would never use whatever chrome gave me unless i need something quick and dirty that's not going to be uh for long term because this is the one chrome gave me like this one look how long it is it's huge right and it's not maintainable it can it can break very easily okay but i could have used what i just did in fact that's too long so i'm going to do this i'm going to use this uh this css because i know it works right if i click if i hover over here i can see that's the right one all right anyways so let me delete this and what we need to do we need to do driver.find element by css or class name in fact let's do class name and i'm so when you're doing the class name you don't have to give it the dot because that's not that's already telling it it's a class all right so what do you want to do we want to click it i can say let me just put it in a variable i'm going to do it differently on everyone so first uh add button is that then i can do first add button that click okay all right so that we can test this out so far let's see if it's doing what we expected to do all right it's running all right all right all right it added to cart we can see one item to cart and we can see it clicked here so the next thing we do we need is we need go to cart page okay and we need driver again oops every function need driver if it's going to interact with the browser so i'm going to take the same exact thing like this but i'm going to say go to cart like i said we could have clicked here we could click there i chose to just go here directly all right so we need to make a function call here all right then what then we can say apply coupon right uh apply let's say apply coupon and we need to give the driver and we need to give it a coupon code so the coupon code is going to be let's say let's just say fake one okay so let's define this function def apply coupon and it's going to be driver and coupon coupon code okay now this is not going to work we're going to see the issue instead of just giving you the solution without showing you the issue i figured i'll relate it fail and then i will give you the solution okay so now let's go to the cart in fact let me open the other brother all right so add item to cart okay go to cart all right now we need to find this coupon code field and apply coupon to it so i'll do this it has a name it has an id okay so we're going to use the id so i'm going to say coupon field is going to be driver.find element by id and that's the id all right then we're going to do coupon field that send keys because we want to send the coupon code right and we're going to say coupon code whatever is passed in here then we need to click on apply coupon so let me see if there is an id there is no id it has a class it's a button so it's not unique at all so in this one let's just copy css normally how would build the css myself make it short and easy at the same time we're just going to copy all right then i can say apply button it's going to be driver.find element by css selector and this is a selector so long one is going to be cut off the screen then i'm going to say apply button and click okay all right so let's run it and let's see what happens it is running and should go into cart and most likely fail oh we never called go to cart we did call go to cart and we did apply coupon so it did not go to cart let's see what happened here okay go to cart page missing one required position or argument driver okay what have my mouse just cuts that oh here we're missing driver i'm sure you guys have caught that mistake i oversaw it okay so let's see it went there uh it added to cart it went to cart okay this is the issue i was talking about all right so it added to cart and went to cart right away but when we got to cart it says there's nothing here this is just the way the plugin works or the site works okay nothing we can do about it it's not our fault because we can see here there is item in the cart that says nothing down here so if i was to refresh the page all i do is reload it and voila it's there okay so that's just the way it works whenever you're testing a site there are different behaviors you don't you don't expect like something that's just not common sense it just happens and you have to deal with it okay so in this case we're going to implement few things so one thing i've done before actually in the full course when we had an entry in this case what we did is we're using a we use we check we wait for this element to have this text then we go to the page and we use explicit weight for that that one is a little bit more complicated so i'm going to do a little bit different i'm just going to use pure python logic and i'm going to show you a few other selenium functions okay so we're gonna do dry def i'm gonna say weight or i can say verify cart has item okay and it's gonna take driver all right so let's look at what this function looks like so the way i want to verify it is i'll go to cart and i want to refresh the page and basically you saw me manually refreshing the page right so in selenium i want to refresh the page and look for this element make sure this element exists okay so if i was to look at what that row is the product the product row is a row with with uh with a class of cart item okay so i'm going to keep refreshing up to x a number of times until this is visible okay so i'm going to say how many times do i want to try let's try one i want to i want to refresh five times okay so i can say for i in range five right basically i wanna try to get that element right i wanna say driver find element by class name and i want to give it the class name and if if it found it then it's it's available right so i can just do try try that and return from the function just finish and just get out of the function x except if there is an exception right i'm going to catch a specific exception in a minute i can just say print uh item not in cart retrying after two seconds okay seconds so now we need to sleep for two seconds and refresh the page so i can do time sleep two seconds and i can say driver da refresh refresh okay so driver.rephrase that's a selenium function we have not talked about you can reload the page so if you look at it five times i'm gonna try to get the element if if there's an exception then i'm gonna sleep for two seconds and refresh and i'm gonna do that five times okay so let's import time that's one thing we need import time also i don't want to catch just all exceptions i want to catch element not found exception specifically that exception so let's import that exception from selenium.webdriver.exceptions no from selenium.com that exceptions import no such element exception okay we take that and we're going to catch that specifically here all right you can try this and it would it would it would raise that exception in fact i have it open right here it says selenium common exception no such exception so when you see this you know where to import it from you don't even have to remember how to import it you look at this it's it's a class an exception so you can just import that way all right so now verify cart has item in in cart has item so before we apply coupon let's actually call that method right and let's run it okay it's running this should work add to cart okay it's going to reload probably you see that it's reloading voila and applied to cart we got that and if we look at our console item not car retrying after two seconds so we just tried once and literally we didn't need to do five times but it doesn't hurt to have it okay so we saw that working and we applied the coupon the last thing to do is we need to just get the message okay so i'm gonna do right click inspect and let's see so i'm gonna take that and go straight to the message and there is an airline let me just copy the expats for this as a selector in fact let's do expat since we've been doing selector okay now i'm going to define a function that says get displayed error message so dev get displayed error message or it can be just message okay driver all right so we can say we can literally just say uh driver dot find element by uh by xpath that's what we said right and this is the xpath and dot text and we can just return that we can do a pythonic way just very compact code we can do this literally right but we are going to run into the same type of issue because this is going to move faster than the message actually shows up let me run it and i'll talk about it because here what we're saying is we're going to click on apply coupon and right away we try to get the message but this is we're going to do it way too fast the message is not going to exist right so we can visually see everything worked but if we look at the test it failed because message no such element unable to locate element because it clicks it and looks for the element and we don't have any kind of weight okay so let's add implicit weight and see if that that helps so here when we create the driver object we can do driver that implicitly wait 10 seconds let's see if that's helpful and my notes actually used explicit way but implicit way should should work also but let's see what happens all right success success okay no issue so now we just need we say get displayed error message all right so we can say message is get displayed error message driver and we need to know what expected messages expected message is going to be we can go to here and just copy this and this is expected message now we just say assert error message is is equal to expected message and unexpected error message and we can just give it the error and expect it all right so you can give it a better message here uh yeah a better when when the exception is raised we want to give it more a better message next unexpected all right so this is the last thing and here we can do print pass if we want and we can do driver dot quit okay but let's just comment that out let's not quit the brother so we can see it all right so it's running i think the test is pretty much completed at this point let's see it's going to reload one time because you can't find the element it doesn't look like it's doing that looks like it is doing that but it's waiting oh we did we added an implicit weight here so that's going to cause a problem it makes our test slower all right because here we said find element and then uh reload the page but we also say wait five minutes before uh 10 10 seconds before you quit so it makes it slower think about why that happened so let me just make this five seconds anyways all right but it did work item not in card refresh one time then pass it actually got the message and it asserted it all right so that is almost like an end-to-end test case um and it's a long one and we talked about different types of concepts on this one in this video we are going to look at another example but in this one we're not going to write a test we're going to do more of a wave scraping okay so if you don't know what web scripting is it's basically when you go to a website and download data from the site when you're scraping the site okay in this particular example we are going to go to the homepage like here and we are going to download all the prices for the items on the homepage okay we're just going to do it on the first page and as an exercise you can improve the script so it will actually go to the next page and to the next page into the next page and do it even though there's only two pages here but your code should be able to handle it that is an exercise okay so for example a competition they say i have a store that sells hoodies and builds and and things like that and you also have a store and you want to see my prices you want to maybe you want to adjust your prices based on my prices right so every every morning or midnight every night or something you want to you want to run a script that will go and check all my prices so you will come to my site go to uh every item and download the price so that's called scraping okay it's very common i've personally used it for different things to scrape websites and get some data so selenium is one of the tools to be able to do that there are other tools like there is a library called beautiful soup there is a library called scrapey those are they actually faster because they don't open a browser those two they don't actually open a browser so they run faster but selenium opens a browser renders everything so it's lower that's an option because you know sometimes uh sometimes it's even a better option because you get blocked and stuff like that so this example that's what we're going to do we're just going to come to the page and download all the prices all right maybe not all the prices but we'll show you why okay so let us go to our we have our last example here open so let's create a new file and i'm going to say python i'm going to say script prices demo store okay all right from selenium import webdriver from selenium.com from webdriver.com and that buy import buy all right so we're not going to do functions or anything like that so driver oops driver is going to be webdriver.com executable let me just copy from here to be faster i'm going to copy this normally you don't even have to provide that okay and i'm going to say url is http demostore.supersqa.com or whatever if you have a if you already created a site locally the same exact site locally like i showed you then use your your site your domain driver.gate url okay and let's add implicit weight driver that implicitly wait let's say five seconds okay so now what do we want to do we want to come and get every element and if we look at every product okay if we ex collapse this every product is an li and it has a class of product okay so we can do all products okay those are the elements but i can just say all products is equal to driver that find element with an s right plural because we want to get all of them by class name and product is a class name all right we can do css we can do a lie dot product or just a class name in this case we need all of them so we use pluraler so this is going to give us a list so if we do print number of products is going to be i can actually do the newest way of formatting lan all for all products okay let's run this and see if so far we got it going should be 16 products on the page all right let's see voila number of products 16. okay good so now the this element let me close this what we just got will give us this entire box right in the box we need to we need to dig down into the price so we need to get to this this span price and another span and exactly to the price from the product right we could have done originally we could have just gave it the xpath uh not expired css to to specifically to the price but i just wanted to show you how you can apply the same method to to an object to a web object web element okay so now this is a list of web elements so we're going to loop through it okay so for product in all products all right then we're going to actually get the the the price it says amount let's see basically we want to get this span it's a span with a class of amount okay so i'm going to say product dot find element by css selector span that amount okay take your time and analyze this because product is not a driver we so far we've been doing find element on the driver object but product is actually a web element and we're doing find element on the web element okay and you can do a dir and see what you can do with this okay so now i can say price element is that and price is going to be price element text okay and another thing we want to do is we want to get the name of the product because when i'm downloading i'm downloading i'm going to go put it in a file and load it into my site or compare it to my own prices so the price by itself doesn't mean anything to me i need to know the name plus the price okay so the name has an h2 with a class of that okay so to to build your own is css you can just do other dollars to test it out it's h2 dot that means class and the class name okay and there is 16 of them right that has the product name so i'm going to copy i'm going to copy that css so now i can say product or i can just say name element is going to be product dot find element by says it not class name css selector and oops let's see i messed up when i copied it and that is it we need to get rid of this then i can say name is going to be a name element dab text okay so let's print price and let's print name just for now but i'm going to show you an issue so i'm going to run the script hopefully you were able to follow me okay i'm going to summarize at the end all right so here when we come here uh you can count this or you can just put a display here and saying how many they are so this is not everything because some of them let's see yeah this is not everything it went went few and it failed because some of them don't have prices if we look at some of the products right all of those have prices all of those have prices even the sale ones have prices but some of them i have varieties right it's a select option there is a range of price so we won't be able to find that okay so if you're if you're doing this as a business it's important to use so you have to add a logic that says oh if this syringe then get the prices or maybe even actually select click on select options and actually select the options and get the price you can do that in our case we don't really need to do that because it's not that important so i you know this is just a demo so what we want to do is we want to identify the ones the the products that that has prices so what i did is wait this one you can you can barely see let me open the other browser i think this is better so what i did is see how we used for the product for the li we used the product class but some of them are uh product simple what is it what is it what is it product simple product style simple okay if we were to look at the ones this type of product here with the li it does not have the product simple it says product type variable okay so we want the product type simple is the one we want so uh product type simple let me copy that uh back to our code so when we say get all products by class name let's actually get the simple ones that's all we need to do now we're gonna actually get everything so now we're just gonna say uh all products and price is a list and here let's append to the list all product and price that append and the way i chose every product to be a dictionary okay so i can say uh name is the name price is price right so we're going to get a list of dictionaries so i can say print length of all product or price and also print all product and price okay that should be it now let's run this and we should get oops okay that would uh let's kill that okay it's fine i'll run it in a debug mode which is the same thing it's just going to run for us and it finished okay we got a list with uh ev every a list containing a dictionary you get the name and the price the name in the price and the name in the price and the price is also a string so you can convert that if you like and we got 14 of them so out of the 16 products 14 of them are simple products okay so this is an example of scraping and we used a different logic and we use mainly what i want to show you is you can actually do find element on an element this is an element and we defined element okay the only problem when we do that is you cannot use um well you could use an ssa selector but it's better to you can't just copy the css selector from chrome it's better to do it this way okay all right that was uh that was a scraping example all right so let's talk about ideas so far you saw me automate few test cases and some web scripting right we wrote about four different scripts and you can continue to do the same thing now you have an idea of what selenium can do and how to use it and you can come up with your own ideas to practice even further you can take the same site and just keep creating more test cases we can create a whole different site you might already have a site so you can just continue to practice but here are just ideas to to get you going okay all right so i have two different groups we have practice test case ideas and i also have practice web scraping ideas and let's just briefly go over them so for example for the test cases you can say verify new user can register just verify the registration right go to the go to my account page i don't want to read every single thing here but you you get you get the point right you go you go to the account page you register make sure the user is registered verified the user can log in an existing user can log in so you write a script to create a user then you can use that user on a different script you can probably hard code it or you can use the api to create a user okay and in production in i wouldn't say production in my real experience what we will do for testing an existing user is we create a user via api because api just takes a couple of seconds it will create the user then i'll take that user i go to the front end and try to log in that's a good thing especially since you're using your own site uh you should be able to do that if you're using demo store as per sql.com you cannot do that because you don't have access to api but if you're using your own if you create your own local site that's a good test case okay the home page has 16 items make sure the six all 16 items are displayed you can just get products and do a count verify checkout works is another test case basically this is going to be an end-to-end test case you just go to the home page add item to the cart use a coupon i actually gave you if you're using demo store i have a coupon that's 100 free so you can use that uh i gave it to you acs qa 100 is what it is if you're creating if you create your own site then create a coupon that makes the whole cart free so you can test out a checkout flow all the way to the end all right uh verify total prices some of all items in the cart page so basically add a couple of items to the cart and then you know cut in your code calculate what the total should be then with selenium get the total that's displayed and verify that's actually very important test case right if you have three items in the cart you want to make sure the total adds up there's no funky stuff that's messing up the total if you have taxes and stuff make sure you have you calculate for that as well okay uh verify removing item from cartworks you basically add item to cart go to cart and click on the x icon and make sure the cart is empty uh there's another one when you have multiple items you add multiple items to the cart then you remove one of the items and make sure the rest of the items remain this kind of bug actually seen in real life okay i've seen this happen when you add multiple items let's say you add three items to the car and you you remove one of the items and next thing you know all the items are removed right that's a major bug that's not something you want so that's a pretty cool test case and that that's a bug that can actually happen all right uh the other one i have number eight is verify sorting works so on the home page you can sort products you know select different types of sorting and verify the sorting changes you can you need to write a bunch of logic in your python but that's a really good practice remember if you're new to python this is a great way to practice python okay this is not about selenium selenium is in my opinion an enemy is the easy part right i said the name just does a few things it has a limited set of functions you can do you can go get element and do something to the element that's that's all you're doing over and over again get element do something to the element over and over again the difference is going to be your python code every time you're going to add a lot of logic once you get the element like there's a lot of things you can do so it's a really great way to practice python not just selenium webdriver okay keep that in mind all right another test case uh changing quantity product works yeah you go to a cart uh i i go to a product page you can click on one of the products the product page opens and you can just change the quantity and make sure it works uh there is a lost password link in my account page and click on that make sure it opens the right page and you can go further and actually test out emails right if you want to go deep you can actually set up a smtp server which is an email server and verified emails actually get sent out okay that's really really cool and this type of test cases if you actually take it seriously and you work on it and you can actually put it on your resume right you can showcase you're capable of doing things like this one here if you set up a smtp server and actually test the email then or you're doing backends backing stuff right so you're good to go it's really good for uh your resume if you if it's a kind of if you are a person that's actually looking for a job okay so those are example tests again i'm pretty sure you guys can come up with really great test cases so you can just do your own uh but this is just an idea all right then web scraping ideas this type of idea some of this i actually used in real life in my personal life okay so a lot of times when you need inform when you need to gather information from the website selenium is a good option so if you don't know what wave scraping is web scripting is basically when you go to a to a site that's up that's available open to the public and you actually get the data literally they're not they're not giving you the data to download but you're actually parsing the html and downloading the data for your use okay what you can do with that data there's a lot of gray area there right obviously i don't think you can you know you can sell that data i i doubt that's legal but for personal use is probably okay in our case it's just for practice right so example uh you want to compare car prices there was a time i was debating with between two different car models and i wanted to know the average price per zip code per per miles per year and such so i scraped two different websites and calculated an average and do i you know i should dig that scribe that script because i did a few years ago but it's a good idea if you go to like carparks.com or autodrive autotraders.com or cars.com and download some of the information right but that way you practice to write python with selenium um you can research most demanding programming languages this actually could be interesting for people that are in the job search you can go to job job sites like dice.com or indeed.com and download the job descriptions right you can do a search for let's say qa automation download the job description and count words and see which word comes up more frequently let's say you download 500 job postings how many of the job postings have python in them and how many of them have java in them right that's a really cool project and you're not only you're practicing you're actually coming coming up with good analysis something useful okay um list of stocks from different sites you might not be a trader you might not know anything about stock trading but let's say you get a job and your boss wants a list of stocks if you go like fin biz this is a very popular uh stock scanning tool okay we'll scan it and we'll give you a list check it out it's free it's a lot of pop-ups um it just gives you a list of a list of stocks and you want to scrape it into a file so your boss can take that and put it into a different platform it's a real use case people would like there are a lot of people for example tinker swim is a platform that's really popular but people like to scan on this site and take the result and put it into think or swim which is a different app but a lot of people do it manually right one by one take a ticker take a ticker and copy paste copy paste copy paste but if you have skills like this if you have selenium skills you can just save it into a file and upload it to the other other site this is a cool project um this will be challenging it's free but there's a lot of pop-ups right so when you're dealing with celine and pop-ups are nightmare all right so how to deal with that it's going to make you think it's going to it's gonna make you come up with a good solution so this is a good practice i i personally like the scraping practice more than the testing practice they're all really good but the scraping practice a lot of times will throw a lot of challenges you don't expect so it's good it's going to make you think outside the box and it's going to make you write a whole lot of python logic which you really want right all right so there's more ideas more ideas for example find books you can buy from one site and sell to another site there are a bunch of website that buy books and you can and there are a bunch of websites that sell books right so you can scrape one site see how much they sell it like ebay for example stuff on ebay is typically cheaper you can scrape ebay and go to another site and get a code and see if it's cheaper right you don't necessarily have to actually do the buying and selling but it's a really cool practice house prices you can go to house listing sites real estate sites like realtor.com or redfin.com or zillow.com and download a list of houses and do an average of prices per zip code per cd things like that all right and research salaries there's a couple of website that gives you salary information right there is salary.com payscale.com i believe linkedin have the same feature and script those sites go there do a search for some career that you like for qa automation for example and download what they say and so you can play around once you write the script you can just set parameters right what how much does a person with 10-year experience get paid versus how much does a person with two years experience get paid in three years experience so you can do that analysis so those are examples some of those are things that came up in my life i actually used in my life so think about something similar that you would like on your life right something that's relevant to you right now and practice with that there's advantage with that is you're going to be interested you're going to be more interested because you're you're not just practicing the code you're actually getting something out of it okay so this are what i'm thinking you can practice with those are i believe good practice ideas and you will find this in the course material so you can there is a link to download the course material in the bottom of the video or at the beginning of the first video of the course depending on how you're watching this okay so but this sheet is available for you and enjoy and make sure you send me questions and any kind of comments you have i'd love to hear from you guys thank you very much
Info
Channel: Super SQA
Views: 673
Rating: undefined out of 5
Keywords:
Id: MWxifZJDCG8
Channel Id: undefined
Length: 117min 21sec (7041 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.