MouseMovement, Drag & Drop and ImplicitWait in Selenium - Session 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
start the recording so today I am going to cover that what do you mean by actions class one of the most important topic okay in selenium that how to do drag and drop how to do the mouse movement okay so let's create first we will see that how to do the mouse movement so I am going to create a class mouse movement concept okay select the main method and click on finish let me clear the output first now I will take this thing because this is the common thing this common code and what I'll do I'll show you one example we have one side let's see SpiceJet calm right everybody knows about a spider calm and in SpiceJet dot-com this is the flight reservation site flight booking site so what we have to do sometimes we have to mouse over on these menus can you see that these menus are there these right these primary menus are there once you mouse over on about us then the submenu will be displayed right so let's see if I want to click on fleet so first time you come on this page but fleet is not visible selenium immediately will give you that element not visible on element not available exception right so in that case what we have to do how to handle this when that is what we have to do we have to mouse over on the specific menu let's see in the in this case we have to mouse over on about us and then fleet option will be displayed then we have to click on it right same thing for charter request career it's and corporate overview what we have to do we have to mouse over and then element will be visible and then we have to click on it this is a very special case sometime in most of applications in web applications we see that menu items are available and we have to click on let's you have to click on airports or I want to select that contact us and international offices I have to go to this page so how will you go international offices is not available it's not visible so what we have to do we have to mouse over ok and then the submenu will be displayed and then click on it and then perform some action once it is visible then perform the action ok so let's see this is a site so how to do that so driver dot and get ok now what we have to do we have to mouse over on let's hear about us and we see that about us is we will spy about us is a link is represented by a a means link so I am NOT going to use this X but because we can see about us there is no ID also so we can use by dot link text ok because this is a link but before that if you move on to mouse over so how to do that we have to create one actions class object so actions not action actions class object we have to create let's see my object name is action you can give any name is equal to new actions just like select class we have to create one actions class object ok and this actions class we have to import from selenium so imported from selenium new actions class object it's saying that give me the driver where is your driver so we have to pass a driver reference this is my driver okay in select class we were passing the complete element driver or find element but in actions class we just need to pass the driver driver reference so my web driver references a driver so you have to pass a driver over here okay now with this object named action dot okay we have a method move to element okay move to element what is your target element my target element is this about us so I'll pass by dot sorry driver dot find element by dot okay by dot link text I know that this is a link so link text is about us so this is the link text about us okay I dot link text about us and what you have to perform okay so move to element so it will be moved so I'm not going to click on it it will be just moved over here like this okay so whenever you are using actions class always remember you have to perform like this dot build dot perform so dot bill dot perform will actually perform the action it will just move okay your mouse it will just move to that a specific element so one method is their action dot move to element where exactly you want to move this is the location one about us we have to move and dot build and dot perform is a method okay to perform the action so whenever you are using actions classes guys always remember you are using dot bill dot perform method okay now it will be moved so now let's see till here it's working or not okay my mouse or whatever the control should be moved to about us so let's run this program and see chrome is launching see about us okay we can see all the sub menus from about this right right now fine so about us Mouse moment is working now I want to click on sweet okay now I want to click on fleet this particular link so how to click so I will see what is the XPath of Fleet so fleet is again a tag a tag means link so I'll use PI dot link text again so the mouse moment is done and then I use a driver dot now the fleet is visible simple by a dot link text link text is this and dot do we have to click on it so dot click right so what exactly it will mouse over on the about Us link then free it will be visible and then we have to click on it right so let's run this program again c 'mouseover is working but it's not taking one fleet if you see an eclipse we are getting error what is the error that unable to locate element fleet why because see we are just mouse over from here to here but it's taking some time to display the fleet option now sub menu options the right so we our mouse over in from here so it's it's taking fraction of seconds to display the feed option so what we have to do we have to wait for some time so let's see I'm putting some static weight thread not sleep and let's sleep for 3 seconds ok it will give you one suggestion add throws titillation ok now if you run this program it will mouse over wait for three seconds and then by the time fleet option will be displayed and then click on it ok now let's run it let me close the previous one okay so fleet is getting displayed now and after three seconds it's clicking on it so now it's moving through the fleet page right this is the three page simple right so this is how we do mark swimming same thing for any mouse movement ok it will work if you want to click on FAQ or a spatial assistance okay so first mouse over on travel info and then perform some action like this ok so this is the mouse memory will be done by actions class ok always remember this is the actions class mouse movement will be done by action flow right so I think we haven't covered that one more thing the mouth movement is turn locators ok no Peters are also done ok one more thing so image after your chromedriver or whatever your Firefox driver or launching your specific browser I want to maximize my screen see currently what is happening if you run this program see I'm running this program again if you run this program the Google Chrome is getting launched but it is not maximized more right it is not getting maximized it should be like this right I have to do it manually right max to maximize like this so how exactly we can do it through selenium okay how to maximize your screen so to maximize this what we have to do immediately after your chrome driver is launched driver dot we have a method manage dot window and dot maximize okay so we are saying driver please manage the window to maximize this is a method to maximize the window automatically and at only one time we have to write immediately after your once the web driver is initialized then you have to use the driver dot manage not window dot maximizes okay sometimes what happens that okay every time whenever we are opening the problems we want to delete all the cookies on the cache a and we want to start a fresh browser so what we have another method driver dot manage dot delete all cookies this is the method to delete all the cookies and caches and everything ok immediately opening after opening your browser it will maximize it and it will delete all the cookies and then enter the URL okay so all the cache a cookies ok those values will be deleted from your chrome and then it will perform action right so this is how we use these okay and this is one-time activity make it a habit that okay you are using these two things after your chromedriver launch okay and this is all we use actions classes to move the okay to move your mouse or over a specific element okay now next thing is drag and drop okay so I'll take this thing this is the common code right launch Chrome maximize and delete all the cookies and for drag and drop I have one site and give you an example okay sometime it happens but it's very rare that in some applications we have to drag and drop some files we have to drag and drop off some a screen short we have to drag and drop right so how to perform ok drag and drop by using selenium so I'll give you one example let me check drag and drop what site is there this is a site okay see so if you see here this is a very simple site so there are two objects this is a source object and this is a target object I would say so what I have to do I have to pick this object and drag it here and drop it here okay and repeat this is these two objects are there I have to drag it and drop it here right so what exactly I will call this is my source and this is my target so on target I have to drop so what is a manual step the manual step will be open the browser enter the URL and then click on it click on your source object click and move and release I'll repeat click move and release right these will be my manual steps okay I just need to click on it move it and release it but how to do it in using selenium so for that one console in this particular application we have to understand so there is a browser so browser is Google Chrome as a browser right after that there is a page pages this after opening this particular URL we have got this particular page the browser page okay the web page but in this particular web page we have one frame this is called frame you can see this box but how will you get to know that this is a frame sometime what happens in some web application its 0.1 percent chances are there that okay some frames are available okay but how will you get to know that okay there is a frame in this particular application how will you get to know that okay this is a frame nothing is written over there right this is a frame or something so what we have to do we have to right click on this box go to view page source and search for frame just press ctrl F and search for frame so you can see that if I frame tag is available okay in the page source iframe it means there is a frame okay on this page in this particular application we have one frame is available okay but it's very rare that frame is available because according to the latest w3c standard okay well web why we standard frames are not allowed I mean frames are not recommended so most of the UI developers okay they are not using frame concept nowadays okay if any very old application they are using or very old UI they are using then only frames are available okay and most of the latest applications frames are gone frames are deprecated according to the latest w3c standard okay but in some applications frames are available it's a very old application okay some tutorial application is there to learn jQuery okay so they have created this particular frame but how will I get to know this right click on page source and search for frame so you will if you see this tag iframe then there is a frame what we have to do to handle the frame because once you open web driver driver equal to new chrome driver chrome will be opened then you enter driver or get this particular URL okay the page will be open okay so let's do it driver dot get and this is when I have a doubt yeah we can use this only when frames are available like this drag and drop this whole process has to be inside a frame compulsory but I can draw maybe it will be you know it can be over here also it's not compulsory okay but is spatially in this particular application and drag and drops are available in this particular frame okay okay that means if it's outside then we will not consider about frames in one okay okay but in this particular application frames these two objects are available may be these all these links are available in a particular frame in that case we have to handle somes so next frame would be there like this bordered thing not at all compulsory that some border will be there or some outline will be there nothing like that okay first you have to right-click and then you have to check if any frame is available iframe tag is there then there is a frame in this particular application okay so I have already seen that frame is available that you have already aware about it know about how to handle frames so if you want to handle these two objects if you want to interact with these two objects you have to come in this particular frame so you are on this page now so after opening this URL driver or get this particular URL the page will be open the browser page is displayed but what we have to do we have to switch from here to here in this particular frame okay we have to get the control from here to here so that we can perform these two actions may perform some action on these two objects so we have same method just like to handle alerts we have driver dot switch to dot frame and we know that there is only one frame just pass zero okay most of the cases if frame application is there only one frame will be there if there are multiple frames you have to test by passing zero or you want to four move to the second frame or third frame or fourth frame okay so we know that okay there are only one frame available then there is only one frame is there then just pass zero okay now what exactly this 9 will do driver or switch to dot frame it will switch from here to here now we are ready to perform actions on these two objects now how to drag-and-drop how to perform drag and drop now so to perform drag and drop again we have to create actions class object simple again I am going to create actions is equal to new actions and pass a driver and this class is available in selenium to just import it okay it got imported action section is equal to new actions now by using this action we have a method action dot okay we have a method see manually how will you do that click and hold right how do you do it manually drag and drop first click and hold right to same method we have action dot click and hold which element you want to click and hold so driver dot find element by dot X path and X path of this particular element this is X path okay click and hold now after the do you want to do click and hold and then move to element on the source on the target element then we have dot move to element ok so which I live on which element you want to move so I want to move on driver dot find element by dot expert an XPath of that particular element ok XPath of this segment copy paste right so what exactly is doing ex action dot click and hold this element moved to on this particular element and then dot what we have to do manually click and hold move to element and simple release simple drop it so we have a method dot release okay and then as I told you with actions class we have to use at the last dot build dot or form simple so the sequence will be like this see click and hold which element this particular element ok dot move to element dot release dot build and dot perform ok you have to use like this action click and hold this particular source element move to element on this target element and release it build it and perform it simple ok so let's see it is working or not see it will be very fast so let's see ok so chrome is low right it's dragging and drop okay so it dragged the source and dropped it to the target alley so this is all we do drag and drop and it's a very famous interview question that how will you perform drag and drop using selenium so for drag and drop you have to use actions classes this clear are not present on any like on the webpage if none of the frame is present you don't need to switch to frames we just need to find X path exactly my XPath weekend exactly no need to write this line in that case this line is only switch to frame if it is present okay okay then only be switch otherwise no need to write this line okay so some time what happens they're talking you are getting you are directly let's you are not aware about it okay there is a frame or not you are not writing this line you're performing drag-and-drop or any kind of action not click or dot send Keys or whatever sometime you will see some error okay not able to find the element X not found element exception or something like that so there are chances that maybe frame is available we have to switch so make sure that okay one of the step you can perform that right click and check frame is available or not right click and go to view page source then the page source makes okay just press ctrl s and search for frame if frame is available it means yes there is a frame on you have to jump over it you have to switch over it ok then only you can do that ok so this is a use of actions class how to move the element okay the mouse movement and then drag and drop although these drag and drop functionality generally not there in you know in our applications but it's a very famous interview question at how to do drag and drop okay okay one more thing we will cover it today implicitly transept okay so what do you mean may implicitly wit implicit wait is a dynamic way to first of all see we are using thread dot sleep three-second or five-second that is your static wait okay so how to use implicitly wait so first let's see take this thing let's see I'm launching google.com or I am launching let's see half dot e be calm okay this is a site I'm launching but I know that okay it's a very heavy site okay so if you open half dot ebay.com and sometimes at sea this is a very heavy site a page it is see page it is still loading so page is getting loaded fully let's see in five to ten seconds sometimes lets you on your laptop and it was a laptop network is very slow so it's taking around five to ten seconds in my I'm having very good internet connection the network is amazingly good so it's taking around three to four seconds only or maybe some site is very easy it's just loading okay so or maybe it's a very heavy site a lot of images and lot of content is available on the web page so obviously it's taking some time especially in e-commerce application if you open amazon.com or snapdeal or flip card it's taking some time to you know load the full page if a lot of images are coming in some other companies are getting loaded or some advertisements are coming okay like that so these carouses are coming okay like like these so these things are getting loaded fully and it's taking time right so what happens some time application is so slow on your lower environment let you run QA environment or the state environment are not okay about production and you want to execute your script on cue environment or a staging environment right in that case application is so slow that selenium is not going to wait for it so let's see immediately after coming on this page I want to enter something here but this L this page is not fully loaded and then selenium is still waiting waiting but there is a limit once the limit is crossed then selenium will immediately will give you exception that element not visible exception or something like that so how to handle the situation so we have one wait is available the driver dot managed dot timeout dot page load time out okay this is the method especially for page the driver dot manage dot time out of time out of what page page load time out let's see map I want to provide 40 seconds so we have to write time unit dot seconds see driver dot please manage the time out time out of what page load time oh okay the pay at the the time out of page load so we are giving 40 seconds so we are telling to selenium at least wait for 40 seconds for okay page to be fully loaded once the page is fully loaded we are giving for maximum 40 seconds okay to selenium to wait for it okay once the page is fully loaded within 40 seconds then perform some action let's see what happens sometimes the page is so heavy it's taking more than 40 seconds okay then it will give you error so we are giving some maximum timeout for page let's see I've given 40 seconds so maximum timeout is 40 seconds of selenium is going to wait for 40 seconds the maximum limit is 40 seconds and once the page is fully loaded within 40 seconds then it will perform action but this way it is so dynamic in nature let's see page is loaded within two seconds so the rest of the 38 seconds will be ignored okay so we ask for the safer side we are telling to selenium boss don't perform any action wait for 40 seconds and then do it at least give some time to the page to be fully loaded and the time is the maximum time is we have given 40 seconds if within what is again if it is not fully loaded then give me you the give me the error cut it so this is called page no timeout another timeout we have one more time out that is driver dot manage not timeout dot implicitly wait let's see implicitly wait also have given 30 seconds now time unit is seconds okay so we have given that implicitly wait implicitly wait is 30 seconds now what is implicitly wait implicitly where it is for the elements for all the elements and this is a global wait immediately after may webdriver initialization I am putting on these two weights these two weights are global weight implicitly weight is applicable for all the elements which are available on the page okay so some time what happens let's see this particular element of this button is coming with C after let's see 5 seconds or 10 seconds page is fully loaded ok page is fully loaded but this go button it is still not visible right page is loaded properly let's see some edge X component right asynchronous JavaScript components are there so in that case wall what will happen go pages go button is not available let's see after 5 seconds after page is fully loaded some elements are still not available oh let's see these sometimes we see that okay images are broken images or and images are not visible after the page is fully loaded also so what will happen and if you want to click on this Go button and Go button is not available then selenium will give you an exception so for a specific element also for all the elements also okay we have given this weight implicitly wait for the specific element that not for the specific limit for all the elements it will be applicable we are giving 30 seconds maximum time after the page is fully loaded so once the page is fully loaded we are saying to selenium that at least wait for next 30 seconds the maximum time is 30 seconds okay to perform the action on a specific element okay so let's see immediately after that if I write okay click on Go button but I would not find element by or expert of Go button and not click the Go button is not available immediately it will give you error right but lets you go button is coming after five seconds so ideally it should not give you error because a Go button is coming after five seconds it's taking time to be appeared and it's taking some time to be appeared on the page right so in that case we have to tell selenium to provide implicitly wait that at least wait for 30 seconds for other elements to be fully loaded and then perform action right so this is coined implicitly wait and this is called page load time out see it's not compulsory once the page is fully loaded still some elements are there on the page which are not loaded fully which are coming after some time let's see some drop-down values you two drop downs are their state drop-down and City drop-down right you select California and in city you are selecting San Francisco but let you after send California let's see you're selecting some different state it's you're selecting Nevada but according to the neighbor now whatever the cities are available in City drop-down which will be updated according to Nevada so it's taking some time right let's you're selecting Karnataka and Karnataka still cities will be displayed like Bangalore Mangalore and all those things now immediately are selecting Maharashtra so according to Maharashtra Pune Mumbai all these values will be selected is taking time right it's the data is coming from a database all the cities are coming from the database according to an attacker according to Maharashtra in the second drop-down right so that's why it's taking some time so it doesn't mean that page is fully loaded but there are some elements on the page which is taking some time to be loaded okay so for that whatever time we have given that at least wait for 30 seconds and then performs an action and it's not compulsory that it's going to wait till 30 seconds if the element is found within two seconds also let's see so the rest of the 28 seconds will be ignored and it will move further so that's why this is called dynamic wait okay what is called dynamic wait okay this is not a static in nature see if I'm putting thread dot sleep I can put Hardy be calm and put thread dot sleep of 20 seconds but thread dot sleep is the hard wait okay thread dot sleep for 20 seconds means every time it's going to wait for 20 seconds so there are two days over at disadvantages with red dot sleep first of all script will be paused for 20 seconds totally paused okay and then let the element is still not available after 20 seconds then it's gone okay but dynamic weight advantage it's so dynamic in nature that let's see page is fully loaded within 5 seconds so rest of the 35 seconds will be ignored it will ignore 35 seconds and it will move further it's not going to wait for next 40 seconds or next 30 website same thing let's see element is visible within two seconds so rest of the 28 seconds will be ignored right but if you are putting thread dot sleep of 30 seconds and let's say a hundred times we have put thread dot sleep for five seconds it means five in two hundred five thousand seconds we are putting in your script your script will be very slow in that case okay so we should avoid red dot sleep to use better to use implicitly weight or page no timeout like we have one more wait explicitly wait that I will tell you later how to use explicitly wait okay so this is how we use page load time out and implicitly wait and this is only one time activity immediately after so make it a habit okay immediately after your web drive and initialization you need to write couple of things first maximize your window and then I would not manage not Mexico dot window dot maximize and then delete all the cookies to try but not manage dot you read all the cookies and put dynamic wait so a couple of things after launching the you chrome enter the URL and this particular code is for this is for maximize the window maximize the browser window this is for delete all the cookies and this is what dynamic wait okay and this is your system property so immediately after you're launching the Google Chrome browser or any browser these three four steps you have to write everything and then enter your URL I mean then enter whatever the action you want to perform not that you are in the URL we have already launched and then perform other actions okay so always may habet immediately after that you have to write these three four lines okay maximize the window delete all the cookies and dynamic weight - dynamic weights are available page load time out and implicitly wait you have to write immediately after your webdriver installation and then perform action dot Lake dot send key directions classes or select classes and whatever the drop-down you want to select okay got it any doubt ok so please practice spatially mouse movement in mouth woman you can take this example SpiceJet dot-com ok and the dispatcher calm though there are number of links you can click on all these links ok so please perform all these you can write a script ok to click on the multiple links or you can take some other application also any other flight application of application or some other example you can find on the on Google and then try to perform where the menu and sub menus are available like this this is called menu and this is called sub menu you can perform like this ANSI you can do one more thing sub menu ok this is the menu this is sub menu and this is one more menu is there and one more link is there so this is different levels I would say L 1 level L 2 level and 3 level and L 4 you have to click on code or fair disclosure how will you do that so this is your assignment ok you have to mouse over on investors and then you have to mouse over on government corporate government then again you have to mouse over on policies under insider-trading regulations and again you have to mouse over on code of fair disclosure I mean not mouse over here then you have to click on it so to click on code of fair disclosure first time it is not available here so what you have to do you have to mouse over or investors then again mouse over on your corporate government governance and again mouse over on policies this insider option and then code of fair disclosure will be available then you have to click on it okay so there are multiple hierarchies okay so this is your assignment for mouse-over okay see mouse over is the very common scenario in any application lot of and in lot of applications they are using mouse over and mouse movement and they will ask you a time of interview also how will you handle this multi with hierarchy based browsers same thing for this see this is there is only one option financial information animal reports and will reports of 2015/16 like that so you have to click on this link so to to come oh okay to reach at that particular location you have to do all these mouse over first mouse over second mouse over third mouse over and then click okay so this is your assignment for mouse over for drag and drop I haven't found any good or you know a good example so you can use this example with the frame so you will know that okay how to handle frames also and they will ask you at a time of interview that how will you handle frames so one very simple answer driver dot switch to dot frame okay very simple answer driver dot switch to dot frame and whatever the frame number is there zero one just pass it that's it okay any doubt
Info
Channel: Naveen AutomationLabs
Views: 151,007
Rating: 4.9137254 out of 5
Keywords: Selenium, Automation, TestNG, WebDriver, selenium drag and drop not working firefox, unknown command mousemoveto safari, selenium ide drag and drop not working, selenium drag and drop not working chrome, chromedriver drag and drop, selenium html5 drag and drop, drag and drop selenium, drag and drop using javascript in selenium webdriver, Actions class in Selenium, Drag and Drop, Mouse Movement, Robot class in Selenium
Id: hJteeuFGeOI
Channel Id: undefined
Length: 42min 14sec (2534 seconds)
Published: Tue Aug 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.