How to implement FluentWait in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome back to another video on selenium-webdriver so today we'll talk about fluent weight in selenium-webdriver and how to use it so we'll start with our high-level agenda so before moving into fluent weight so the precondition is you should visit the explicit weight in selenium-webdriver because fluent weight is the advanced version of the weight so white is coming into picture what is the need of it to understand all this you need to visit the previous video on explicit in selenium-webdriver then we will talk about fluent wait what is a fluent wait what is the syntax we'll write a quick program for fluent fit and you will find the same code in this below URL a fluent weight in selenium webdriver so I will show you one by one so the complete article you will find this I will give in the description so now if you talk about let me go to the next slide fluent weight is the separate class in selenium webdriver and using this class we can wait for a specific conditions until it is not satisfied so we already know explicit weight also having lots of preconditions that are more than sufficient but using fluent weight you can customize a specific condition so I will just show you in a few minutes what I mean by customize existing conditions so in fluent weight we can change the default polling period based on our requirement so by default explicit weight implicit weight the default polling time is 250 milliseconds so if you want to change this polling period like say 1 second 5 second 10 second 30 second you can change but by default to 50 millisecond is the polling period so if you might get this questions and interview what is the default timeout for weight so it's 250 milliseconds so if you talk about the technical details fluent weight is a class that comes under this package which is orz dot open Q dot silly not support dot UI package it is the implementation of weight interface so you can say weight interfaces of base interface for this fluent way so each flint weight of instance will define the maximum amount of time which you have to wait for a specific condition and as I said we can change this frequency so when I talk about frequency you can say it's a poling period you can change how frequently you want to poll for that specific condition and the last point we can ignore some exceptions so while polling some of the element if you are getting some exceptions and if you want to ignore them you can ignore them using ignore method I will show you how to use this ignoring feature as well so this is the technical details in the normal definition of fluent weight and if you talk about the syntax in text is quite complicated but thanks to selenium guys they already gave the complete syntax in the source file itself so what exactly we are trying to do same example we'll take here so if I click here on start timer after certain time timer will start and after some time you will get the webdriver so we will write one fluent wait until this webdriver is not coming keep waiting our key polling so once the webdriver comes it will stop the execution and it will close the browser same thing we have done in explicit wait but next press it wait we have used a predefined condition but in fluent wait we will write our own condition and you will see how easy it is to customize the x conditions so let's start with our regular test I will create a separate class and I will say fluent wait example so from the previous program I will take two three lines of code because it's unnecessary to write same piece of code again and again so this is the code I just copied four lines of a statement from a previous code I am just starting a browser and maximizing it I am opening the same URL which I Josh just showed you right now then I am clicking on this timer button so I am clicking on this timer button then I into wait until that a specific text is not coming so let me open this in Firefox because I need a new XPath for this so if I click on this qtp webdriver so I have to wait until this webdriver is not coming so I will write XPath like search for a paragraph double /p where text equal to webdriver right so I will check for this keyword so I will say driver dot find element by XPath and I will simply check is displayed then I will say boolean status if it comes I will print it if it comes I will sit test case pass so right now I shall do this right now it will fail because we have not given any implicit fate or any other wait for this particular web element so definitely our test will fail so you will see and our test is failing because that element is not present because by default time is zero so if that immediately not present it will throw you no such element exception so now we need to use wait so let's use the fluent wait I will comment this code as of now let us remove it so if I write fluent wait you can see it's coming from oh-o-oh RG dot open created selenium and if you just see here everything they have given here so they had given the simplest sample usage how you can do this and they have given the complete description which I just showed in the PPT so you don't have to do so much just copy-paste this syntax so now you need to import some of the things because they have not given the complete codes so you need to import the weight first of all so you can see weight is coming from support UI in thought works so don't use the sort works because it's the older ones of click on the first one do the same for fluent weight imported and seconds they have given in normal forms it's time unit dot second it's not direct second time unit dot second and you need to import from Java dot util everything I will show I will explain this code let me remove all this exceptions first so no such element you should take from Aceh dot selenium same thing will do verbally mint will take function no it's important you can see the function is an interface that is coming from different libraries it's coming from Sun library utils you have to take from corn or google.com and base that's all so you can see what it says if we are creating object of fluent weight we are giving that timeout so it should wait maximum 30 second so you can see the description as well it will set the how long to wait for a evaluate conditions to be true we are checking poll after five-second so that's very big polling period so we'll change it to one second as of now and you can see we are ignoring so while polling for a specific element it will throw no such element exception so we can use this ignoring no such element exception dot class so in case is this exception is coming it will ignore this exception not let's come to the next this is really very important so you can see apply is a method which is coming from this function interface so syntax is little bit complicated but you don't have to worry about it because this is how they have written so I will show you what things you need to keep in mind this is your input this is your output so webdriver is an input which you will pass here and web element will be the output which you will return from here so you can see webdriver driver this is the input so we will search for a specific web element if it is returning then we will return the web element so let us make some changes same thing I will do I want to wait for this XPath sin spite of ID I will say wait for this XPath and I will provide this XPath here and if this condition gets true our web element will be stored in this then I can write condition element dot is displayed I will say element is displayed so once you get this condition it will say true so now you can see it's very straightforward but let's make some changes because I want to show you very special thing which you have not seen till yet so let's make some changes let us try to find out this Web element first of all and let's take this default XPath I am not waiting for a space with webdriver condition I am just giving this XPath same example I have used in my blog post so you don't have to where you can copy paste the code for the trial purpose but I will suggest you you can give a try from your side so what we will do we will not use this XPath will write this so what exactly we are doing here we are trying to pull this web element directly okay and we will try to capture the text whatever text it is coming we will print on the screen and we will keep printing until the webdriver is not coming so let me show you what I mean it's giving some error because we missed one so this is our web element right so I will say ele so from this ele i will try to call inner HTML so what is inner estimate what is coming on the screen is your inner HTML so selenium is inner HTML then q2p and webdriver so I will say ele dot get attribute inner HTML so whatever inner HTML will come I will store in a string variable and I will say value so I will say if value dot equal ignore case equal to webdriver if it is equal to webdriver then returned this element or else return null when I say return L it means if our specific condition is not true it will keep running this wait and if it is not matching up what I will be able to try to print this value I know syntax is little bit tricky but it is really very interesting default value or text which is coming on-screen is value so let me explain it this code once again so this is the weight which we specified like 30 is the maximum time owed by default polling we have taken one second and it will ignore this so until is a condition inside this condition we have given one function which will take webdriver as input and web element is output so apply is a method which comes by default with function which will accept driver as an argument and return type as a web element so first we'll try to get our web element we'll get the inner HTML and we'll check if equal to webdriver then return the element or else print the text which is coming and return L so when I say return L it will run the loop again and again until the specific element is not found within this 30 second and once it comes it will definitely return you the element in this and we are printing here element is displayed true so let me run this program now let me close the previous window and you can see it's printing on the screen text coming selenium selenium QT p QT p and it's not printing webdriver because once webdriver is coming it is giving the condition is true and it is coming out of the loop and you can see the polling period is one second so as my code says selenium will come for two second QT p will come for two second and web travel webdriver also will come for two second so this is the code which I have written internally so the timer which we are using in our code is two seconds every or text Selene key to be webdriver will come for the interval of two seconds so when I told about the polling it is polling after one second so you can see the polling frequency right now let us see in the polling time if you want to see I will make it as ten millisecond and you will notice it will do this polling very frequently let me close this so it is started now if you see the polling it is polling very frequently and once the selenium-webdriver comes it is it's stopping the polling end is written in the web element is true you can see how frequently it is polling right so you you too pitted polled and for selenium and by default text also we have so like this you can change this polling period by default is 250 milliseconds so let me keep it to 50 millisecond and let's see you'll see it's polling solariums rhenium qtp when webdriver comes it will stop so now I hope you understand this concept so let me show you last time so you can see it will click on the timer and now it is checking selenium qtp and once webdriver come it will stop and it will say true right so this is just an example you can change this condition based on your requirement I just used this like capture the text from the web element capture the inner HTML and wait until this webdriver is coming or not if coming then return otherwise keep printing the statement which is coming on the screen and return null so when you say return null then only it will repeat this loop otherwise it will stop here itself right so don't worry about the code part code I have already updated here so I will paste this blog link in the description of this video so you can check so there are so many examples of fluent wait which where you can use this so you can take any live application make my trip Amazon where they have the dynamic weight you can take this application as well let's make my trip and I will tell you how you can apply this so let's say Delhi to Bangalore take a nice flight so you can see it will take some time to load the flight right so you can keep a fluent wait here wait until the flight rates are coming a lot rate so there's so many example so hope you will implement the same so if you still have doubt feel free to drop an email or you can come in below thank you so much have a nice day / bye
Info
Channel: Mukesh otwani
Views: 60,005
Rating: 4.8095236 out of 5
Keywords: FluentWait in Selenium Webdriver, Dynamic Wait, Selenium, Selenium Webdriver, Sync issues in Selenium, Automation
Id: xmgWAaFtoFA
Channel Id: undefined
Length: 20min 25sec (1225 seconds)
Published: Mon Sep 19 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.