ExplicitWait, Take Screenshot and Navigate Back & Forward - Selenium WebDriver Session 7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so today I'm going to cover one important drink although it's very small thing that how to navigate to some other URL or let's see some external URL you are on google page and you are moving to facebook.com right so google.com is a different domain and you are moving to the facebook.com that is a different domain okay so how do you know switch from one domain to another domain or one site to another site and how to come back and how to click on forward button so this concept is called tickle how to simulate back and forward button of the browser right so let's see if I open let's see if I open google.com right so google.com is there and immediately if I open msn.com right so it's opening amazon.com but if I want to go back so I'm clicking on this back button and again clicking on this forward button alright again go back go back by clicking on Go button okay by clicking your back button not go button so this is called back and forward buttons of browser so how to click on these two buttons because this is not part of your webpage this is part of your browser so obviously I cannot aspire this if you try to aspire obviously I cannot do that because this is what these are not web elements these are browser elements so how to do that right so for that case this is called back and forward simulation so I will create a class let's see my class name is navigations okay so we will take this the same thing okay these are the prerequisite ease we are launching with Google driver and let's take instead of this thing let's take this one yeah okay and I'm launching the URL once everything is done after that okay maximize the window to read all the cookies and I've given one page load time out also for 30 seconds and we are launching Google Chrome okay now we are launching Google Chrome after that I'm launching www.google.com okay I'm launching google.com now what I want after google.com I want to move it to facebook.com or amazon.com so what I have to do we have to write driver dot navigate method dot to method is there and you have to pass where exactly you want to go so www dot let's see amazon.com okay so and after moving to amazon.com you have to come back right so let's see this is working or not in here well let's run it and see it's Google Chrome is getting launched and it will enter amazon.com okay from Google do it shifting to amazon.com okay so tell a err it's working fine now what we have to do we have to click on the back button so how to click on the back button we have a method driver dot navigate dot back okay come back to Google page and let's wait for Red Dot sleep let's wait for two seconds okay and I'll just add this throws declaration wait for two seconds and again click on forward button so after coming back again we are on Google page right you have to come forward so driver sorry the driver dot again navigate dot forward method is there so it will move forward to Amazon again right yeah and let's again wait for two seconds okay and then again come back so we will write this line once again so what will be the latex page the last page will be Google Wave the first it will open Google page then it will navigate to amazon.com click on back button come back to Google page click on forward button come forward to amazon.com again come back to Google page so the last page will be Google page right so let's run this program Google is launched Amazon ok so once the page is fully loaded then it will again clicking on back button again forward button ok and again come back to Google page okay so it's doing backward and forward button simulation this is called how to simulate backward back button and forward button ok it's very important there they will ask you at a time of interview ok how will you some millet how will you click on back and forward button off the browser then you have to use navigation you try button as we get back and now it or navigate dot forward simple sometime it happens that okay in our application we are clicking on a link and some other external site is getting opened and again you have to come back to your site you have applied your application right in that case we have to use back and forward button okay wait let's hear you you are ta you are on a Google page and somewhere you clicked on amazon.com okay so you are moving to amazon.com but again you with your actual site is Google page so again you have to come back so in that case no need to write google.com again just click on back button okay this concept is called back and forward simulation okay now some time what happens that okay let's our pages it is stuck somewhere you want to refresh the page so we are clicking on this refresh button right to refresh the page right this refresh icon is there on all the browsers are available so how to refresh the page simple driver dot navigate dot refresh method is there sentence to refresh the page okay so driver don't navigate dot any fresh it simple refresh it will refresh the page okay so let's run it so amazon.com again come back to Google page and come forward to Amazon again come back to Google page and then it will refresh automatically different page is refreshed okay so this is called how we'll use some millet refresh button how will you submit it back and forward button correct okay very simple this is called they will ask you at a time of interview what is a difference between driver dot navigate dot to and driver not get so dry but get is also used to launch the URL okay but driver dot navigate dot to is used to whatever the application URL is there if you want to move to some other URL some external URL in that case we have to use driver dot navigate dot to okay both are used to launch the URLs but if you want to move if you want to switch to some external URL in that case you have to use a tripod or navigate dot to method okay okay this is called tilde navigations okay so they will ask you a time of interview what is the difference between okay get method versus navigate dot to method okay both are used to launch the URL but navigate dot to is used to launch the external device okay let's move to the next topic so next topic is how to take a screenshot ok take screenshot concept so sometime what happens in manual testing also we have to take a screenshot once the error is coming or okay any bug is there we have to take screenshot let's see I want to take the screenshot of this Google page then we have one printed screen how will you take it manually right in Windows we have put in the screen method is there right the Print Screen key is there on your keyboard oh you can take print the screen and you can take the screen shorter but how to do the same thing in selenium so to do the same thing in selenium let's say I want to take the screenshot of let's see I'll open this page Google page and immediately after the Google page I want to take the screenshot so how to take the screenshot so to take the screenshot we have a standard method is available no need to remember the school just remember the concept whenever it is required just copy paste so only these two lines are there just copy and paste okay okay I'll tell you what exactly it is this line will give you one suggestion just add throws declaration okay now what do you have to do just change the path where exactly you want to generate your screenshot okay so let's say I want to generate my screenshot in this folder in this package of itself so I'll give the path of this particular package okay and what is my file name a file name is Google dot PNG okay but now what is the code the code is after google.com I want to take a screenshot so one method is there get a screenshot as method is there okay so get a screenshot as method will give you one file object but we are top casting in to take a screenshot class converting driver in to take a screenshot class so take a screenshot is the interface not the class sorry is interface if you mouse over its written over there interface so see there is no need to remember this code this is the standard code in selenium in this site also they have written ok the exact two lines of code so no need to remember that okay what exactly this method and what exactly the interface and all those things just remember only two concepts they will ask at a time of interview can we take a screenshot in selenium yes we can take a screenshot in selenium by using get a screenshot as method okay so we are converting this driver in to take a screenshot which will give you one final object and this file object we are copying file from SRC to where exactly on which location you want to generate your screenshot so I want to generate my screenshot on this particular location in my hard disk on my hard disk I want to generate this particular screenshot my screenshot name will be Google dot PNG well you can use Google jpg file okay so if you take the screenshot please generate in this particular location so we are giving this location also that's it only two lines of code we have to write okay if you run this program you will see it will open Google Drive chromedriver okay don't libraries open and then it will take the screenshot so how to check the screenshot is there I have written in your workspace this is your workspace in this particular selenium sessions folder selenium sessions folder and then refresh this page if you refresh it see Google dot PNG is there can you see that so if you open this screenshot I swear this is the screen shorter yes okay but it's not compulsory that every time you have to write you know your workspace location you can generate in your C Drive D Drive anywhere so just write C : Google dot PNG or C documents new folder is there and Google dot B Indian whatever the name Facebook dot PNG and dot jpg you want to generate it will generate that particular file okay so these are the two lines of code that you have to always use the okay just copy paste no need to remember at the time of interview nobody will ask you okay how to take a screenshot simply okay so this is how we take screenshots okay one important thing today I am going to cover that what is explicitly weight - so far you remember in the last session we have seen what is implicitly wait and what is remember what is implicitly wait and what is page load time out remember yes that will check implicitly wait that what is page load time out and what is implicitly with page load time out is for page loading implicitly well is not implicitly we are putting a wait for all the elements but what is explicitly wait right so for explicitly wait also let's see one page is there let's see I'll open half.com okay this site is there it's same half.com has closed why it is closed okay let you this page is there right what is happening this Habib a.com or whatever any side you get we can pick any site let's see this particular email ID and password these two fields are coming after some time the page is fully loaded completely but is still specific element let's see this button register button of signing button is coming after some time ah let's see this help icon is coming after some time then and we have written that okay after the page we have to click on this icon this help icon and you are using dot click method but this icon is not visible immediately selenium will throw an exception that element not found exception or a stale element exception something like that then what we have to do so we have to put one explicit wait explicitly we are waiting for that particular element okay once the element is visible once the element is available on the page then we have to click on it getting a point okay so explicitly it is for the specific element you can put explicitly rate for any specific element let's see this image is not coming this image is coming after five seconds or 10 seconds after the page load page is fully loaded which will not give you guarantee see there is no guarantee of page is fully loaded there is no guarantee that all the elements are loaded properly no page is fully loaded means the page is completely loaded from the server but some images are still pending or some limb or links or some you know elements are still pending on the page right sometime it happens especially for edge X component right let's see one country drop-down is there you select USA and in-state also you are selecting California and then you are selecting one city that's the same thing let's your selecting India and then you are selecting Maharashtra and then tune it but let's see from mara from state drop-down you're selecting Karnataka so the options will be changed immediately in the third drop though what ifs taking some time okay so if you select Kannada you want to select Bangalore but if you select Mara sure you want to select Mooney so but there is a there is a time gap the moment you select Maharashtra all the states of all the cities of Maharashtra will be loaded okay in the third drop-down same thing if you are selecting Karnataka all the values all the cities of Karnataka will be loaded but it's taking some time all the values are coming from the database but immediately if you want to perform something it's your selecting dot select and your selecting Pune okay so immediately it will give you one element exception that element not found exception or some other exception why if it's taking some time in selenium is not going to wait for it so we have to put some weight in the form of explicitly wait you can put thread dot sleep I can write thread browse three four five seconds or two seconds but this is not the right way that you have written thread dot sleep for two seconds but let's see element is still not available in two seconds then it will give you a turn after two seconds so we have to put the one in dynamic wait you have to put one explicitly wait okay so let's see so what we have to do okay so I will write this code so there are two types of it first we will provide page load time out and then implicitly wait time out driver dot manage dot time out dot implicitly wait let see for 40 seconds and time unit dot seconds right so these two implicitly wait we have added payload time out and immediately wait we have added this is the global wait now why I have to provide my explicitly wait so for explicitly wait also what exactly I have written I have written one a standard method which I will give you okay so let me give you that method you can use that mattered anywhere that is very a generic method and I'll tell you how I give Utley I created that method just a minute sorry after the main method and adding this method okay now see I have created this click on method can you see that this click on method so this is a method so obviously I cannot create a method inside the main method so there is a separate method now this mattered what exactly is doing this method is void which mean this method is not returning anything okay so what I am doing here in this particular method we are passing three parameters what is your driver what exactly your locator and what exactly timeout you want to give so what exactly we are doing the method body is starting from here and ending here we are creating webdriver weight object so webdriver waste is a class already available in selenium so we are creating object of webdriver weight by using new webdriver weight okay in webdriver weight that we give you it will take two parameters what exactly your driver and how much time you want to wait okay so my weight is my driver this is the driver and this is a timeout what I will pass whatever a timeout thirty seconds of what is that means I'll pass that parameter over here and then one condition I am putting that piece ignore is tail element reference exception please ignore this particular exception until see don't be confused with the second line we have written this method like this until okay one condition expected condition that once the element is clickable then please click on it okay so this is called explicitly wait I'll repeat I'm creating new webdriver weight object webdriver weight object I am passing what is my driver and what is my timeout let you timeout is 30 seconds and please ignore this tail element reference exception until until the condition what exactly condition my expected condition is expected condition again it's a class available in selenium that element should be clickable element to be clickable which element this particular located element okay so let's see I'm open I am opening let's see facebook.com so let's open Facebook not come here let me log out this is a dummy's Facebook I have created for the practice point of view let's see this is the page ok log first page I want to check that this login button is OK I want to click on this login button so I aspire this login button so this is the input field and we can take this ID ok so far we can take this expert ok now what I'll do I'll call this method click on method ok because we have to click on it so what exactly I'll do click on method I'll call ok this method will be called so this is a static in nature so no need to create the object remember the static so no need to create the object because this is a static in nature so simple click on know what is your driver this is my driver I am passing what exactly your locator so my locator is driver dot find element by dot XPath an XPath of this right expert we have already taken off login button and what is your time out let's see I'm gonna wait for 20 seconds so what exactly do I do I'll open facebook.com ok it will refresh the page like this and then let your login button is coming after 5 seconds so it will wait for maximum time is 20 seconds it will wait for 20 seconds - ok - login button is visible or not it will check that login button is visible ok and then it's gonna wait for 20 seconds explicitly for login so and then what exactly it will call this method - it will come over here on click on method the driver will be given to this driver what is your web element locator that this particular locator is this on the basis of XPath and timeout is 20 timeout is 20 right 20 will be given to this we are passing three parameter now new rev driver driver is this timeout will be 20 ignore this particular exception an expected condition dot element to be clickable which element this particular locator and this locator is this which element to be clickable this particular element to be clickable the login button okay and then this condition is satisfied okay and then locate a dot click then click on it see I can write driver dot find element by dot XY dot click directly without putting explicitly wait but the problem is if you write like this let your login button is coming after five seconds selenium will give you exception immediately let element not found exception right so there are two ways either you wait for 5 seconds you put thread dot sleep for 5 seconds and then click on login button but I don't want to use red dot sleep I want to use explicitly wait not explicitly wait for login button to be appeared on the page and then once it is clickable then click on it and the maximum time I am giving 20 seconds right the maximum time I have given 20 seconds now another concept let's see element is visible within two seconds so the rest of 18 seconds will be ignored getting a point if the element is found let's see within 5 seconds so the rest of the 15 seconds will be ignored totally so this is so dynamic in nature that it's not gonna wait for 20 seconds in the maximum time is 20 seconds but if element is visible within 2 seconds now the 18 seconds are still left will be ignored and your program will continue okay so this is very very strong okay wait this is called synchronization point in selenium in the form of explicitly wait so explicitly we'd always remember for the specific element okay not for all the elements explicitly where it is for the specific element what is your element this element on the basis of one condition your condition is element to be clickable okay sometimes one condition is there if you see after dot controller space you will see element to be visible so there are number of options element to be available element to be selected element to be clickable right so there are number of options presence of element if element is present then click on it text for text also sometimes okay text to be present so the whatever time we use element to be clickable once the element is clickable then click on it some time what happens I have a login button displayed but it's not clickable sometime it is latif or initial fall couple of second it's disabled some sites are there right Facebook is a very obviously very good site so obviously in that is login button immediately visible but some sites are there some buttons are coming which getting displayed but not visible or maybe not shakable oh it's disabled all right so we can put one condition that one's element to be clickable then locator dot click then click on it right so this is very important method you can use this method anywhere in your program this is a standard generic method okay to perform any click and it will work for all the buttons it will work for all the links it will work for all the checkboxes or radio buttons whenever you have to perform any click method or any click action it will perform it will work if you just need to pass three parameters what is your driver on which element you want to do okay and what exactly the timeout what exactly the time you want to provide let's see some link is available now let's talk about some other option let's see I want to click on this male or female let's see how many if we want to select a value from drop down in that is in that case in that case it will not work but drop down you have to get another explicitly wait for that because this is only an only for click okay so let's talk about drop down also wait let's say I'm giving you a rather example let's see I want to click on this create create a page okay so what I'll do simple see now it will be very simple click on what is your driver this driver lot is located driver dot find element by dot expert and XPath of this particular link this is x1 okay and what is your timeout that you timeout I am giving only for 10 seconds okay so this is for login button this is for create a page link right so I'm using click on method this is for login button time passing login button located on the basis of expert you can use any of anything by Road ID by door link text or by ID or name whatever you want to use and click on this just for create a page link but here I'm giving only 10 seconds but I know that because create creator pages is coming within 10 seconds so no need to write 10 seconds or 20 seconds or different different other weights okay so I talked to you what exactly the timeout you want to give and this is the standard method that I have created every time I'll call this method for all the click options okay so if you run this program let's run it see chrome is getting launched Facebook first it will click on thing and then let's see it's just and working it's see it's showing some error what is this waiting for element okay see maybe XPath is wrong so that so what exactly try it for 20 seconds but XPath is not visible right see the error I wanted to show you this error waiting for element to be clickable right this is the error and what exactly the time for this particular element you underscore zero in this code to you underscore zero in this code to the try it for 20 seconds but element is not visible then throw this exception okay exception is timeout exception expected condition field okay why because we are using a wrong X 1 so this particular X 1 is not correct maybe some whatever some different X but is there that's right see this is the testing of our code that login for login button we have given the wrong XPath it means we are expecting some different element but the element is not available and it waited for 20 seconds and after 20 second okay it gave you error right we waited for 20 seconds and then it gave you in it see if you run this program again let's run it again see let's see X father's not correct for login but then X bar this not correct form I clicked on sorry manually I clicked on it so let the X bar is not correct so it will check away that element is not available but it's gonna wait for 20 seconds and after 20 second it will give you error so it's not giving you any error immediately but after 20 seconds once the locator is still not available it's not able to clickable then it will give you exception or error okay so let's wait for 20 seconds I think and well seconds already gone and then it will give you error if you don't use this thing immediately it really but at least we are giving or gave an option to selenium boss please wait for 20 seconds and then throw some exception to is still waiting it's waiting waiting waiting okay and then
Info
Channel: Naveen AutomationLabs
Views: 96,905
Rating: 4.8372593 out of 5
Keywords: Selenium, WebDriver, ExplicitWait, Implicit Wait in Selenium, webdriverwait selenium example, selenium implicit wait, thread.sleep in selenium, webdriverwait selenium java, Take Screenshot in Selenium, how to take screenshot in selenium webdriver in java, selenium take screenshot on failure, selenium screenshot full page, how to take multiple screenshot in selenium webdriver using java, selenium takesscreenshot, Mukesh Otwani
Id: rIFnN15nSJU
Channel Id: undefined
Length: 39min 41sec (2381 seconds)
Published: Tue Sep 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.