Handling JavaScript Alert and Popup - Selenium WebDriver Session 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so today what I'm doing today I'm going to cover one important thing that so so far we have seen that okay water different locators are there and how to handle the drop downs and how to enter the values in a specific field how to click on a link or something okay by using expert or ID or name or okay with the different options are available in selenium so but there are something which cannot be handled by locators like pop-ups okay some some random pop-ups are coming so how to handle these pop-ups or sometimes we called how to handle these alerts so let's you'll create a class alert pop-up handle select the main method and click on finish so let's see I'll give you an example let's see example is one site is the rediffmail calm okay so on rediffmail calm let's see I click on the sign-in link okay I'll come on this page on sign-in page of rediff.com and without entering the username and password just click on Go button ok without entering the username password click on the Go button and you are getting a message that please enter a valid username with OK button this is called pop-up this is called alert okay sometimes we see that ok are you we are getting some confirmation pop-up that are you sure you want to you oh you want to cancel it are you sure you want to move further are you sure you want to delete it ok some pop-ups are coming with OK button or cancel button or a yes or no button or something like that so these things are called JavaScript pop-ups ok these pop-ups are defined by developers once you enter if you once you don't enter anything user name and password and clicking on Go button so this kind of messages or this kind of pop-ups alerts we are getting so generally we say these are alerts or maybe confirmation pop-up or sometimes we say javascript papa okay these are JavaScript pop-up so how to handle this thing okay so if you open your Firebug and fire path and you click on let's see Go button you cannot aspire this see I'm not able to aspire this right so with Firebug and fire of a burg Firebug and fire path you can not despite these things so how to handle this okay so what we have to do okay so let's see we will use the same code okay we will launch Chrome driver system set property and in chrome driver and we will enter the URL so how to enter the URL simple driver dot get and the URL is this let CL enter the URL okay and after entering the URL what we have to do we have to we don't need to click on any I mean we don't need to enter user name and password we have to click on Go button so I'll spy this Go button right so in this go button I have to click on it and this this Go button can be spied by Firebug and fire path so I'm not going to use this expert yesterday I've given you the reason right why because this particular XPath is having this hierarchy form they want dev - dev - and bla bla bla so I cannot use this particular XPath so what are the options are available I can use name okay what happened just a minute I think we need with you okay fine sorry so what I have to do I have to use a bi dot name here okay so I'll get the name from here simple driver dot find element by dot name and I click on Go button by using this dot and this is a button try to use click dot click so here I'm clicking on Go button ok so here I'm clicking on Go button now after clicking on Go button so it will click on Go button and then this pop-up will come now what I have to do on this pop-up I have to perform two actions first to get the text of this particular popper okay and then click on OK button so how to get the text of this pop-up so what we have to do so right now we have two windows here two screens are there okay when the background screen and this pop of the screen so what we had to do we have to switch from here to here okay we have to switch from the screen due on this particular pop-up okay so what do we have to do how to switch from here to here so that we can take this text and we can click on ok so for that we have to tell us driver driver not switch to dot alert method is there okay driver dot switch to dot alert method is there so what exactly it will do driver dot switch to dot alert it will switch from here to here now the control is available on this particular popper simple so we have to tell driver switch to dot alert okay so this particular alert method will give you one alert class object so I will store in sub alert reference so I will create alert class this is a valid class and alert class object references like this okay and this alert class a capitalized is available in selenium so just import it from selenium okay so alert alert I told to drive without switch to dot alert always remember this thing you have to switch okay to that particular alert now the focus is available the control is available on this particular alert only okay and switch to dot alert will give you one alert class object so we are storing in some reference are not lessons you can give any name ABCD for this whatever okay but proper name I have given alert and with this particular alert object reference we have one method alert dot get text method simple get the text of that particular alert and okay and then I'm simple printing is system.out.println or not dot get text I am printing over here okay so system go repent and alert get text whatever the text is available on alert give me the text okay and the second thing is what I want I want to click on this okay button so how to click on OK button without using XPath ID name because there there is no XPath there is no ID so okay with the same alert dot except method is there okay so this is it will click on OK but it ok so a lot dot except method is there and we have one more method alert dot dismiss method is there to click on cancel button but there is no cancel button but sometimes we see that ok cancel buttons are there so in that case we have to use a lot dot dismiss if you want to dismiss if you want to cancel the alert you can use dismiss right now we don't have any dismiss I'm just commenting this line okay so I'll repeat the concept here is guys this ok button or this text I cannot use by using XPath or something like that what we have to do we have to switch from here to here ok you have to switch from here to alert and then the method name is this driver dot switch to dot alert method is there and then it will return you one alert class object reference and then you have to get the text of that particular song to get a lot dot simple get text and then if you want to click on OK button of alert simple user dot accept and a lot dismiss to cancelable ok to cancel on it and it's a very very important concept for interview point of view 100% sure they will ask you how will you handle alerts and pop-ups ok so these three lines four lines you have to tell ok what do you have to tell that we have to use alert a p8 okay to handle alerts and we have to switch what is the method name driver dots which do not alert and they will ask you send X also okay give me the method name write the code driver dot switch to dot alert which will return when alert class object reference by using this reference dot get text method accept and dismiss these three method you have to remember okay so if you run this program so we do one thing after clicking on go button let's wait for five seconds so how to wait how to pause your script for five seconds we have one method thread dot sleep method is there let's see I'm waiting for five seconds five thousand milliseconds I am passing over here so thread dot sleep method is there with for five thousand milliseconds means five seconds okay and this will give you one suggestion had throws declaration justin bullard throws declaration okay so what will happen after clicking one Go button it's gotta wait for five seconds and then it will switch on that particular alert then get the text and then accept it okay so let's run this program and let's see it working or not chrome is getting launched see the alert is like this please and there a valid username with OK button in mag it shows like that ok and then after 5 shake it automatically it got clicked and see it's giving you the text please enter a valid username C in Mac generally once we run through Selenia once again so it shows graphics like I mean I mean the palapa like 6 please enter a valid username it's the same pop-up that we used over here in the actual browser same pop-up is this in chrome it shows like this ok in Firefox it shows like this in chrome it shows like this don't worry it's the same pop-up these are called everything is called pop-ups ok in this form so first is getting the value please enter a valid user name so it's printing the value please enter a valid user name and with OK button so why it is useful first because obviously testing point of view you have to click on this alert and then the testing or rather a validation you can put the whatever it takes you are getting you can put the condition that if this equal to this the test cases pass right so you want to test that whatever the message is coming on the alert box are not pop-up is this is a correct message or not right if it is a correct message your test is passed otherwise it trailed right so let's see I'm putting condition like this I'm storing this a lot dot get take in some variable let's see string X is equal to this and then put one if condition if text dot equals what remember string manipulation is string comparison if test dot equals this system dot out dot println correct alert message else system dot out dot println incorrect alert message simple right so if it is equal to this then correct error message else incorrect alert message so this is the validation point we have written after getting the text from alert so it's important right we have to validate how will you do it manually manually also you will do the same thing right you will open this alert and then you will check the correct message is there or not please enter a valid username let's say you're entering the username and it's giving please enter your password so again you will check that you are getting the proper message for password also right so it as a QA this is the part of testing right - please enter you password or please enter a username okay all these correct alert messages are coming or not through automation we are doing like this we are validating like this ok so this is how we handle alerts and it's a very famous interview question how to handle alerts in selenium okay so make sure you are okay explaining like this and it out okay okay now one more thing I will give you one more example of another pop-up let me give you the URL let's see I have an URL this one ok ok so this is a simple some site is there some very simple site sometimes we see in our project in our application that browse button is there right so if you want to upload a file ok and through browse option how to do that simple manually you will click on browse button and it will ask you that ok let's upload this particular thing let's see this dot HTML file an extend or HTML file will be added over here like this but you are coming on this page fine you are clicking on this browse button also you can do that that's fine but how will you handle this how will you select a file because this is the desktop this is a desktop upper this is like I mean my window Papa Mac pop-up is coming in your case it will be window pop-up so selenium cannot understand this thing that it will go to your C Drive or D Drive or something like that no selenium cannot handle this thing this is not part of browser this is part of your operating system ok this is part of your windows right in that case I cannot handle using selenium I cannot like I will go to the expert or desktop or document or C Drive or D Drive and then I click on it and something like that and then I select the specific file no I cannot do that very straightforward this thing so this is called another power this is called window pop-up ok or a Macbook pop-up something like that this is another pop-up right after clicking on browse so I cannot switch like this from here to here this is not possible this is not part of your browser okay earlier that I sure I've shown you that another pop-up that is JavaScript pop-up that was part of your browser okay please enter a valid username with okay button but this is window pop-up I cannot handle through selenium because selenium does not support any window pop-ups any windows right operation it does not support so how to do that so for that very simple operation what we have to do first is PI this browse button because obviously we have to click on it alright so we can use this ID ID equal to file upload so first take this URL let's say I'm taking this URL okay and then we will write we will launch the chrome and there enter the URL okay driver driver equal to new chrome driver driver door get okay now I have to click on browse button so I despise this browser so I don't need to click on browse button right so let's see I'll take this X ID or I can take name also or I can take X 5 so let's see I'll take X path okay that's until driver dot find element by dot X path and X path is this now I should not use dot click if I am using dot click immediately it will try to click on it and then it's lost then I cannot do because I cannot handle this particular window pop-up so how to do that ok so instead of dot click just use sendkeys method and in send keys you have to path you have to pass the path location of that particular file which file you want to upload so let's see I want to upload this extent or HTML file so I will get the ok whatever the file is available let's see on my desktop I want to upload this particular file and get the path your spots the path slash and filename is extend or etching in your case it will be C dry D Drive or whatever extent dot html' okay now I'm just passing driver or find element by or expert dots and get instead of dot click guys don't click on browse button ok always remember don't click on browse button otherwise it will open this file upload pop up and then you are stuck then you can not do anything so don't click on it simple dot send keys and give the file which file the file path which file you want to upload so simple give the file path and automatically this potato file will be uploaded ok through the Browse button so let's run this program you will see the chrome is getting launched and see it's done you can see extend or HTML code uploaded without clicking one mouse button ok you got it so again this is a very famous interview question and you will see in your life projects also there are browse buttons there are attachments some time it happens I try it we have to attach the files we have to upload the files okay so nothing like this will work with any upload or any upload functionality attachment or any external file we want to attach or upload right this will this function will work for all of them exactly ok if there is some time there is no browse sometimes they say attach the name will be do have not upload upload or attach or browse or add add files ok I catch by something like that but it will work but you need to aspire this you will get some expert ID or whatever but make sure type equal to file is there in 99.99% cases type equal to file will be there mm-hmm okay so it will work for sure 100% name will be different I'm not saying that obviously it will be a browse it cannot change whatever name you want to give the developers they have given some name but like here in their tml tag we can see or like file upload is given as name right so this name will remain same for everything like even if the button is upload or attach name will remain color code it can be different also it doesn't matter but ties will be you type equal to file will be their name I can give some name also let's see my file upload doesn't matter I can give any name that your initiative but this particular browse name okay the name of the button will be different in your case it will be a dead file or add file or upload file whatever but don't click on it on the Browse button otherwise it will give you this box so they have given the solution simple you send keys and pass the value which file you want to upload okay so this is how we ended file upload pop-ups we have another type of pop-up that is window pop-up that you are clicking on a link I'll give you that another example that we are clicking on a particular link and another browser is getting opened browser window editing open that we will cover it later because it's a different concept altogether okay but I think what today I just wanted to be the only these two concepts because I want to okay make sure that okay you are covering all these topics okay so first practice all these things and then we will move further today I wanted to cover these two topics okay how to handle alerts and how to handle file upload box it's a very important interview version both the both the topic 100% they will ask you how will you handle alert second or third question on selenium will be how to handle alerts and problems okay but that's all for today so I'll see you tomorrow okay any doubts if you want to you know discuss with respect to selenium or Java
Info
Channel: Naveen AutomationLabs
Views: 161,673
Rating: 4.8573022 out of 5
Keywords: Selenium, WebDriver, Automation, how to handle popup in selenium webdriver using java, how to handle alert in selenium webdriver using java, how to handle frames in selenium webdriver, how to handle confirmation popup in selenium webdriver, how to get alert text in selenium webdriver, how to click ok on alert in selenium webdriver, how to check if alert is present in webdriver, How to pass parameter from testing.xml, handle alert pop up in selenium, Alert pop up
Id: g5HyaG3RABg
Channel Id: undefined
Length: 24min 39sec (1479 seconds)
Published: Thu Aug 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.