How to pass KeysPress Event in Selenium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is Lavinia welcome back to Naveen automation labs ok so today I'm going to cover a very very small topic that how to handle keypress event in selenium sometimes we have to enter let the enter or a space or whatever you can see on your keyboard I'm talking about your laptop keyboard or maybe the actual keyboard whatever you are pressing right if you want to send as a part of send keys ok so how to do that in selenium so then this is called key key based events that we have to follow so how to do that so to do this thing is what we have to do that we have to use so our keys class is available it's a very famous interview question also very quickly rich to that first I'll tell you the example that what exactly I am talking about so let's see this is a site key process and then you can see that this site is available and it's saying that press a key and see what you input it so let's see I'm pressing enter you will see you entered enter I press space spacebar spacebar I enter e I entered T I enter s I enter 1 0 Z X like this slash dot dot means period comma like this right pack code all these things I'm pressing so all these as a keys key event impressing I'm passing on these things so how to do that in selenium so to do this thing in selenium sometimes we have to use that sometimes let's see the send Keys the actual takes instead of actually takes you a passing maybe you have to you know through this key arrow key up key down left arrow right arrow we have to press like that so how you got so very simple that I will create one class let's see my class name is key press event select the main method and click on finish or we will do one thing we will write a test in G calculate directly at the rate test method and then public void quickly right let's see a key given the test at the rate test is available in testing G and input and I write web driver driver is equal to new private right and then I'll import these two statements and then quickly I will take this property also system roadside property and initialize and then I launch my cab driver I mean I launch my URL private or ticket what is the URL name the URL name is this this right now what I'm going to do that there are two options there are two ways though the first option is option number one that I will directly pass the send Keys I create the web element and I will directly pass the same keys so how do that so the first time if I come over here if you see the first time it is like this okay so what I'm gonna do that there is if you see the page source one ID is available divided e okay you can see that this particular div ID like this and then let me inspect and I'll show you that this okay so one ID I which want to check this is ID equal to flash Class D by D is equal to content so that is representing this entire thing is represented by this particular T by D it means the moment I you know I'll pass the Sen keys with this particular divided evil so that is that is fine so let's do that thing driver got find element by dot ID ID is in your application maybe it will be a different ID or something like that in that case we have to okay press and down for okay key press and key down and whatever the key you want to pass you can do that lets see some text field is there or something like that here we don't have any text field and I'm just like passing on this particular web element passing all the events so let's see ID is equal to content I'll use this I'd equal to content ID equal to content dot sendkeys and here in China Keys instead of passing any value let's say I want to pass the value for space so how to do that simple keys dot space method is there okay it means the moment you press space over here see a moment I press space right it chain space so I'll inspect that space is coming or not I inspect this particular field that P I'd equal to result and you entered space is available or not so do one thing that I will get the text driver dot find element by dot ID ID is this result and I'll get the text dot get X method and then it will return one string let's say string text is equal to this and what I'm going to do system dot out dot println and print this text and then an assorted dot first one equals what exactly your actual value actual value is text and what exactly you are expecting after entering the space after entering the space expecting this you entered space sorry you entered space this is what I'm expecting to enter space okay simple race why I am using driver or find element by ID counter in your case it will be a text field let's see you want to pass some space or something like that okay you want to pass like Navi in space world hello space well something like that in a particular text field okay so you have to get the ID of that text field okay so and then getting the text the moment I so this particular line it will press space from your keyboard automatically and then the moment you do that automatically this particular you enter the space text will be displayed you capture the text and then you will verify it so how to do that so let's debug this and we will see you know or maybe we will run it directly from here so let's it's launching Chrome and it should press space it's not pressing it's getting failed it's saying unknown error let's see what is the error webdriver cannot focus element okay so let's see what is the error cannot focus element because on this side this is the first time I'm also using let me check okay so we are getting this error cannot focus element okay the problem is any sometimes this problem occurs will send key this is a bug in selenium so we will do one thing let's not use any keys we will use let us not use any keys you can check the send keys on some different browser or maybe in your case it will work so I'm going to use action class ultimately I want to trigger Keys event so let's see I'm going to create the actual class object over here is equal to in that case we don't need any okay any element and simple what exactly I'm going to do that with this action plus reference action dot okay the Methodists end his method I'll be using and then I'll be passing Keys dot space right and then keys dot space will return one space or maybe it will return this particular thing the moment I press just a minute yeah the moment I press space it will return this you entered space text and then it will print on the console and then we are verifying it so let's see that is working or not so let's try it okay so it's launching the browser and okay again it got failed at C key event section we have forgot to enter dot build or perform dot bill dot perform okay now let's so key presses not entering you enter space it immediately entered space and then it's now it's pass and is taking the text you enter space and the assertion is getting filled - your boarding pass so assertion is also working fine and then you enter the space like that now let's test with the enter let's say press ENTER over here let me refresh this and then I press Enter okay enter so it's saying you enter like this so let me inspect and then I'll press Enter so for enter we are expecting this you entered enter so T is dot enter and again it will we will verify this particular you entered enter is coming or not so let's run it again and we will see so now this time it's called enter enter and then we'll see it's printing yes educating pass and you enter enter so ultimately what exactly I'm trying to say guys over here that this is how Keys dot event you can pass if you see inside the keys class there are a number of methods are available okay all all keys arrow down arrow left arrow right arrow up backspace cancel clear command all the options which are all the function keys also available okay and left us left control and everything is available numeric pad is also one two three numeric pad is also available shift command tab space separate or a semicolon right everything is available over here so you can use any of them and then you can use by using action class action classes are very you know it's very important in this case because sometimes send keys doesn't work if you are using like this sometime it doesn't work so better you perform the action and then use action okay like that so I think that's all for today and I mean this was it this particular session it's very simple and just practice with this particular site with the key press event and that's all thank you so much guys thanks for watching this video and this is my channel name Levine automation lapse if you really like this video please subscribe to the channel and share with others if you have any queries you can write it varies in the comment section definitely I'll try to reply thank you so much
Info
Channel: Naveen AutomationLabs
Views: 13,148
Rating: 4.9712229 out of 5
Keywords: KeysPress Event in Selenium, KeysPress Event, Actions class in selenium, selenium, webdriver, Naveen AutomationLabs
Id: 2n8Uay6o240
Channel Id: undefined
Length: 12min 1sec (721 seconds)
Published: Thu Jun 21 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.