Selenium Framework for Beginners 29 | Selenium Waits | How to use Implicit and Explicit waits

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session I'm rocof and today we will learn all about weights in selenium so we will go very basic step by step and we will learn why do we want to use weights in selenium what is implicit weight and how do we implement it in our selenium scripts what is explicit weight and how do we implement this in our scripts so let's get started and let us first see why exactly do we want to use weights in our selenium scripts now whenever you are working with any web applications the elements are the objects on the web page may not all load at the same time so they can vary in their load time and not all the elements or objects are loaded at the same times and specifically if your web application is using Ajax or JavaScript etcetera then different objects may load at different times and to handle this in selenium we use weights so with the help of weights we can enable our selenium script to wait for some time for a specific period of time as we specify before throwing no such element exception let us take an example if I go to some website say make my trip calm and here let me go to the official website so here we can select some slides so I will say from some particular location so it is already giving me this option new Delhi to Houston some particular date and this is the class and if I say search so as soon as I click search you will see it is waiting for some time before showing the actual result now this wait can depend on a lot of environment factors as well if the network is slow or if the application server is not responding in a very fast way or due to any environment reasons as well it can take the time of loading this particular page can vary now suppose you want to book some particular flight and you want to test it in your selenium script if you do not use weights this will actually fail and show you no such element exception because it will not wait for this page to get loaded so that is the reason we want to use weights now let us very quickly look at what is implicit weight so selenium we have different types of weight that we can implement in our script we have implicit explicit and fluent in this session we will learn implicit and explicit weights and I will tell you about fluent weight in the next session so implicit weight here we can define a time period under which the web driver should wait before throwing no such element exception and this is the syntax so let us go to our Eclipse and I will show you practically how it is done and here I will just create a new class you can create a class anywhere in your project I will just go to my demo package and create a new class and I will say this is selenium weight demo and here I will create a function public void selenium weights and here I will first create an object for webdriver so webdriver driver equals let me say new chrome driver so I will start on a Chrome browser and I have to import webdriver from open cubed or selenium and also I have to set the property for my chrome driver exe so you can see my chrome driver exe is here so inside the drivers folder I have chrome driver and this is my chrome driver exe so I will say system dot set property and I will say webdriver dot chrome dot driver equals to the location of chromedriver dot exe so I will just copy this I will go to properties copy this entire location and paste it here ok so here I am on window so I am getting this double backslashes you can have a single forward slash even that will work both on Windows and Mac and other operating systems also in the earlier session we have seen how to take a relative path so you can say here system dot get property and the property is user dot dir and this property will give me the path of this treasure wherever this project my project is it will give me that path and I will store it inside string variable called project path okay and now I can use this instead of this entire path so unbilled here is okay and before this I will say project path and plus the rest of the path so now I have my driver initialized and now here I will say driver dot get and I will go to let us say google.com so this is HTTP google.com and here it will go to google.com and here I will just enter something in the text box so if I see this text box and I see I will inspect it and you can see here the name is queue okay so I will use this property name queue and then I will hit on this search button and if I inspect this you can see here the name is BTN kay so I will use this I will say driver dot find element by dot name and the name is Q and here I will say dot action is send key so I want to send something and here I will say automation step-by-step so I want it to write this in the text box and then hit the search button so again I will say driver dot find elements by dot name and here the name is BTN K and I can either use dot click and with experience I know that the click button is overlapped with the results of this whatever we write so I can also say dot sendkeys dot sendkeys why is it not letting me do that just wait okay dot yeah send keys and here I will say Keys dot return so this is equivalent to pressing the enter key on the keyboard and then I will say driver dot close driver dot good okay so this is a very simple selenium script which is going to the google.com and then searching for this particular text and hitting the enter key I will try to run this so I have to use the cause this is not I have not created a test ng class here so I have to use a main method and I will create a main method here and I will actually call the selenium vietze method so I also have to make it as static and I will call it from here I will press ctrl spacebar to autocomplete and yes so it is now calling it I will do a right click run as java application so now you can see it is going to google.com and check this and hitting the enter button and then it will close so this is working fine now let us imagine here after it hits the enter key let us say there is some element that I want to find out and do some action so I will say driver dot find element by dot name and I say name is ABCD which is actually a non-existent element and I will say the action is click OK now see this element is not there so eventually it will fail so what happens what will happen let us see I will show you the console and just pay attention on the console what happens here so I will run this again and here it goes to the browser and just see here how soon the exception will come up ok so it here if you see so do you see this no such element so unable to find the element and if I go here so it's a it has thrown no such element exception unable to locate the element with the name and the value as ABCD but did you see it just throw this X exception instantly without even waiting for that element or without even searching for that element for some time so this is what happens in real world in real world you will have several elements which might take some time to load in the Dom and therefore you might want to use this implicit wait so here let us now see what will be the difference when we use the implicit waits so here I will use after initializing the driver I will say driver dot managed dot time-outs dot here if you see we have this implicitly wait and also we have this documentation so here it they have given us all the documentation how to what are the parameters what is the return type so here I will use this and you can see here we can give the time and the time unit so I will say 10 as the time and the unit I will say time unit and dot and here you can see you can select days hour microsecond milliseconds minutes nanoseconds seconds so I will select seconds so this means we are setting our implicit wait for 10 seconds and this will mean that a we'll wait for ten seconds before it will throw this exception so for ten seconds it will keep on polling and the default of frequency of polling is 250 milliseconds so this is our interview question so it they can ask you that what is the default frequency or the poll time of implicit wait so this is the default time it will keep on pulling every 250 milliseconds for a period of 10 seconds so whatever time you specify here before it throws any exception so now let us see what happens I have said this timeout implicit wait and I will run this again and again keep a eye on the console so I will run this again and let us see what happens so this is the browser has opened and goes to google.com and see it is now it has not yet thrown the exception it is still waiting for that element and only after 10 seconds we will get the exception here so it is still waiting for 10 seconds and see now we have got the exception no such element so if we get the element or if the element is loaded before 10 seconds it will do the action and only after the timeout it will throw this exception okay so this is how you can use implicit wait and also note that by default if you do not set any implicit wait then the by default time period is 0 so that is 0 seconds and it is implicit ball for the entire session so you can see here we have said this here in the starting so it will be applicable for this entire session until you close the browser or you go to your test case ok so this is what implicit way it is now let us see what is explicit wait now you may also want that your website is fairly fast and you can you have most of your elements loaded within 2 to 3 seconds but there are some elements or some objects which take more time so you can actually set explicit wait for specific elements and it can also be set for a specific condition so for example you can see the syntax this is how we use explicit wait so there is a webdriver weight class and here we set the timeout here you can see it has set to 10 and then we can also set the expected condition let us see how to use it so here I will say webdriver wait and if you press control spacebar it will auto complete so webdriver wait and any name you can give weight equals new webdriver wait and here you can see we have to pass our webdriver instance and that timeout so here I will say the driver instances drivers you can see we have created this driver here the same name I will use here and the timeout and seconds I can use anything let us say 20 seconds here and that's it ok so this is how we create the explicit weight and then how to use it we will use it on some particular elements so we will say here web element element now you can either declare an element separately and then implement weight or in a single and you can say weight so you have to use this variable that you have defined here dot until now here you have to use this just a minute this is weight and dot until expected condition right yes so until and then we will put our expected conditions so here we will put we will use another class expected conditions so make sure that you use it from this expected conditions which is coming from which is a class and not the interface expected condition so we are going to use expected conditions and then we will say element to be clickable or you can use any others as well so if I press dot here you can see a lot of conditions you can use okay so element select selection state to be element to be clickable element to be visible and so on so you can see alert is present and so on so i am using element to be clickable by dot name you can use any locator and Here I am saying ABCD or something which is not actually existing okay and a semicolon at the end okay so here let us see again we have to import the element I think yeah from orj open q selenium okay so here now I will just comment out the implicit wait and now we are having this explicit weight which is these two statements first we have defined webdriver weight weight equals new webdriver we have passed our Java instance and given the timeout and then we have said that wait until and expected condition element to be clickable and then this is the element that you want to find out okay let us try to run this now and again you can keep a look on the console I will run this again so here let us see it opens the browser and enters automation step by step hits on the enter button and now see it is still waiting so it is waiting for it will wait for 20 seconds until it finds that element and if it does not find then only it will throw out the exception so it is still waiting 20 seconds are not yet over so you can see now after only 20 seconds it has sent out this exception and also if you see the exception it will say expected condition filled waiting for as he meant to be clickable so it is not just checking the element to be visible we can set the actual condition okay so this is how you can use explicit weights and actually you can set implicit weight for the entire session and explicit weight for certain elements where you think it needs more time and also if you go to the selenium website or the official webpage let me just show you if I go to selenium HQ which is the official website for selenium this is selenium sq dot o-r-g and here I will go to the documentation and here you will find weight so you can see here you will find explicit and implicit weights and if I go here they have given all these details okay and also if you see there is a warning given here do not mix implicit and explicit weights doing so can cause unpredictable wait times for example setting an implicit wait for 10 seconds and an explicit wait for 15 seconds could cause a term timeout to occur after 20 seconds so this is you can see if you want to mix or not this is this will depend on your needs your requirements so as I said you can either put our implicit wait for the entire application which is a little less like 5 seconds or max 10 seconds and for some particular elements where you think that it will take a lot of time and you are not sure it can take more than 10 or 20 seconds there you can put an explicit weight so you can use it as per your requirement so today we learnt why to use weights and implicit and explicit weights now I hope this was very useful for you I will suggest that you some do you do some hands-on on some websites for example make my trip as I showed you earlier you can do a hands-on there and you can do some more examples with weights and also share this knowledge with everyone I will meet you in the next episode thank you for watching
Info
Channel: Automation Step by Step
Views: 48,829
Rating: undefined out of 5
Keywords: selenium waits beginner tutorial, how to use waits in selenium, how to use selenium waits, selenium what is implicit wait, selenium what is explicit wait, how to use implicit and explicit waits, difference betweeen implicit and explicit wait, selenium beginner tutorials
Id: UN8cauyoZsk
Channel Id: undefined
Length: 19min 0sec (1140 seconds)
Published: Tue Aug 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.