Write Selenium Script in Python II

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to talk about writing selenium scripts in Python and this is a part two so if you haven't seen part one make sure you go to our Channel and check out part one first so first what we're going to talk about in today's video is we're going to talk about how to handle drop- down list after that we'll show you how to submit web forms and in particular the reason we want to show you how to submit web forms is because this involves executing commands sequentially and so while we're executing the command sequentially we want to show you how we can um we can handle certain cases and exceptions during the sequential process so that it doesn't disrupt the whole process next we'll talk about how to handle new windows and tabs that pop up during your test and finally we'll talk about weight statements and how to use weight statements in your own so talking about sequential uh test again so referring back to it uh what we refer to sequential test is that for example um we have a bunch of different commands command one two 3 four and first we do command one then we do command two then we do command three and then we do command four after they pass but this can be problematic sometimes if for example command three fails and when the command 3 fails then the other commands afterwards will not execute and so we'll show you how we can handle this in our cases today and typically what we do is we use a tri accept statement in Python so that whenever we get an uh an exception during this command three it catches it but then it continues onto command four as well after that we'll talk about some weight statements so in web test sometimes web elements will need time to be loaded or be enabled and we should let selenium commands to wait for a period of time and then do our operations after and so that's what weight statements are for for example you have time dos sleep which is in the time package and this allows you to pause the execution of a TR thread for a certain amount of time you also have explicit weight statements which allow your code to to stop and freeze until a certain condition is met and we'll show you that later again we have implicit weights which kind of similar to time uh we just basically tell the web driver to wait for a certain amount of time and then try to do any actions afterwards all right so again if you haven't watched the part one of this video I would strongly suggest you go and check out part one first and then come back to this video so this is the code that we had left off from part one we basically created a uh class or a file right here a python script with a bunch of different uh functions to test different functionalities on our web page and for you that don't remember I'm just going to navigate to our web page page it's just adm.com and we wanted to test this web page so we tested all these buttons last time these text boxes check boxes and radio boxes today we're going to be working with this web page for a bit but then we'll be working with other web pages afterwards but to begin with we will work with this web page so for today's video Let's Make a new file or new script so go ahead and right click test right here uh and then we'll click new and then we'll click Python and again because we're using test we're going to start our file name with testore and then this time we're just going to name it selenium commands and then this would be part two so two py okay press enter now we have our empty file now before we work with anything we have right here I'm going to have to do a few things so let's first import some packages that we need to use uh I actually have it on the side right here so I'm going to ahead and copy and paste it because these are just importing packages so first thing obviously we import the web driver uh we also import py test we import U web driver select action chains uh the weight statements which we'll need later and time as well which we'll need later as well okay so once we have that let us begin so again remember we talked about fixtures which are basically functions that you can pass into your classes or into your other functions and so for p test if you want to specify a fixture you just use this app tag and then you say py. fixture and so this time what we're doing is we're using Edge instead of Firefox which is what we used previously um we're going to start a edge uh web driver and then we're going to maximize a window and then proceed from there again we commented out driver. quit right here so that we have to manually close the driver uh so that you can see what's going on on the browser okay so now that we're done with the fixture let's start making some stuff so the first thing I'm going to do is we're going to make a class or a I mean a function called we're going to test the drop down drop down list so we're going go ahead and call it drop down list we're going to pass Edge into it um and what we're going to do is we're going to do Edge doget and we're going to navigate to our URL which is the um the web elements URL that I just showed you so I'm going to go ahead and paste that right here um and so ADM lucid.com - home- web elements after that let's wait for 6 seconds and the reason I'm implementing this is so that I can drag the window open because I'm using two monitors open over to the main window and you can see what's going on before anything happens and then also maybe if there's any JavaScript elements on the web page that we need to load we give us some time to load after that let's call Select element so something on this web page we're going to name this uh we're going to use Edge and we're going to say find element and we're going to use by and it's going to be by. name and then the value for this is going to be age so we're going to search for the element that has the name AG on our web page go ahead press enter select now we're going to use select is equal to select so this is basically we're selecting an element uh we're selecting this uh web element right here so select element and then right here we're going to do select dot select uh by visible text and this basically allows us to select from the drop- down list we're going to select 12 so basically entering our age as 12 and so now that that's done let me show you what this looks like so go ahead and press run test uh drag this over make this big and we see that there's an age right here and we should see it change anytime now see changed to 12 right before it closed and the test passed so that's good let's do some more stuff so we're going to do definition we're going to make another one uh we're going to do a drop down list again uh but this time it's going to be we're going to call it two pass Edge fixture in again and then what we're going to do is we're going to so I'm going to actually copy these two statements right here because these two are the same we're going to go to this URL again and what we're going to do is we're going to uh select the different elements I'm just going to copy and paste everything over just because um we want to select different elements so we're going to uh find the element that has the value service then we're going to select preschool from there and so if I run this again the same thing should happen it's just a repetitive I just want to show you what's going on so I maximize this again uh service right here so we should see it change sometime soon so it changed to preschool before it closed and there we go okay next one let's work with submitting a form on our web page so this one we're going to call def test and this is just going to be submit form and this is going to be Edge right here and what we're going to do is we're going to again no not that I just want to paste over since we're always going to the same web page just going to use that at the same time and now we're going to have to define a bunch of things so uh I'll type the first one out so again let's say we want to find the name field so we go name is equal to edge. findor element and then we're going to use bu is equal to by. name right and then we'll have to find the value uh and then the value was equal to name so for those that don't of you that don't remember how we did this and how we find this value we just go to our web page for example um this is what we have to fill in we just right click this inspect and then then we um so we right click this oops uh my bad uh we right click this inspect and then we open this it's not working but let me try that again right click inspect and we have name is equal to name so by name is equal to name and that's why we put name right here but yeah so basically we do this for a lot of stuff so I'm just going to go ahead and uh paste some stuff over so we're also finding the email field the phone field the submit field using the name as well uh for the submit field we're using X path which uh is this locator after we do that what we're going to do is we're going to do name. clear and we'll just clear anything in the name field and then what we're going to do is we're going to do send keys so we're going to use name send keys and this is what we basically put into the name field so I'm just going to name it John on Smith there you go and so that's basically filling out the name and this is really repetitive so I'm going to complet I'm going to show you how to do this again so again now we do it for email we clear out the email we send the keys for the email clear out the phone send the keys from the phone now for the other stuff uh for the drop down list inside the form we have uh some other stuff so uh since we talked and showed you how to use drop- down list earlier I'm just going to copy and paste to so this is basically what we did in the functions above we found this drop- down list uh area age we selected 12 found service selected pres school so I'm going to do that again in Here and Now what I'm going to do is we're actually going to do edge. execute uhor script and then what we're going to type is uh we're going to do this this is what we're g to this is the script we're going to execute in here and it's basically telling basically what we are telling them to do is we are telling the edge browser to scroll down to the window so that we can see what we want to work with because if we don't scroll down sometimes you won't be able to see the submit button which is the same button right here in your browser and then it won't be able to click it so we're telling to scroll all the way down and then you can click it after that what we're going to do is we're going to do submit. click and then we're going to time sleep for two seconds and then what we're going to do is edge. switch to alert accept so accept any alerts that happen and then we run this again so you see that you can't see it down there so you have to scroll down for a bit and then it can see and yeah so that's basically um filling out the form and so filling out this form becomes a sequential process and so what I'm going to do is I'm actually going to copy and paste some uh the same thing we just had above but this time what I'm going to do so we have the same stuff we have above but this time instead of using telephone as this value for this finding this element right here we switch it to so so earlier it was telephone we're going to switch it to telephone 2 and basically we're switching it to something that doesn't exist so it shouldn't be able to find it and because of that this should fail right here uh by finding this element and nothing else will execute and that's what we basically want to show you we we want to show you that um when in uh using Edge and selenium if one thing fails earlier then nothing else afterwards will execute so let me run this again show you what's going on drag it over maximize it and you see it fills up the name and the email but doesn't fill out the phone number and so because uh and then nothing else afterwards gets filled and it's basically saying that this fails because it can't locate this element right here uh is it no such element unable to locate it yeah but basically it's not able to locate the telephone element is what I'm trying to show you um yeah um so what you can do is you can use a try and catch uh try and accept statement and so I'm going to uncomment out this code I have right here and I'm going to comment out this one and so basically we're saying let's try to find the element telephone 2 and if it doesn't work then we're just going to print out this but if it does work then we can just say oh it works and we can proceed so this try and catch try and accept statement is really good uh if you're not sure if this will work for sure so let me run this again same code but just new we're using telephone 2 again Ste uh but let me drag this over maximize it so it's going to fill out the name and email and it fills out the drop- down list and everything else as well and then it doesn't fail the telephone it just skips it because it fails and so uh you see it printed could not find the telephone element uh the element of telephone and that's because this statement didn't work so if it fails then it goes to accept okay now let's move on and show you the next uh test that we're going to do uh this time we're going to show you um some different uh weight statements so let's show you a uh test weight I'm actually uh since we're just showing you the weight statements it's not too much to to know but I'm just going to paste this over and I'll explain over so again we're going to the browser and then we're finding the head header text element so here we sleep for 6 seconds and then basically uh right here this is another wait statement we tell it to wait another six seconds and then um we basically wait until a certain a certain thing uh condition is satisfied and right here it's basically header text is displayed we wait for that to be satisfied and then we scroll down and then we wait another uh we wait right here so we're looking for an element called value weight 45 and then basically before you click on it uh we see if weight 15 is enabled and then afterwards uh we click on weight 15 right here and then we see what happens and then after we click is it enabled and it's not enabled and then we wait until this is enabled and then we print and ask if it's enabled so this is a lot maybe for you right now but let me just show you what I mean basically on our web page we have this button where um when you click it it it's disabled for 15 seconds and so what we're testing right here is we're testing if to see if this explicit weight which we're specifying is uh working and so we're basically telling our program to click the button and then we're waiting uh until the button is enabled right here and then once it's enabled we check the condition and see if it's enabled and see if it works properly uh so yeah so let me run this for you and you'll you'll see it in live action so go ahead and drag this window over so we're just uh have our web page page right here and then um it Scrolls down to the bottom it'll click the button you see how the button just went gray out uh now we wait 15 seconds but in our case we're telling it to wait until this button is enabled until for it to stop the test so now we're going to wait for the button to reenable itself after 15 seconds it re-enables itself and then there we go it finished running and so if we see uh before click on it is enabled after we click on it it becomes disabled and then we wait for 15 seconds it becomes enabled again so that's a true false true statement and yeah so that's basically weights we use two types of Weights here we Ed the weight so we just wait for 6 seconds and we Ed the explicit weight which we tell it to wait for a certain condition which is the weight 15 is enabled to be satisfied and then it continues okay finally we're going to talk about handling windows and so right here um handling Windows uh we have this right here and basically we go to a different URL this time and we're telling it to find an element right here and then uh basically uh we telling you to click on this button right here and then this will basically open up a different Tab and then we're trying to tell it to search on the new tab for a certain location and then see if what happens so let me show you what happens right here I TR over so it opens a new tab and then it's trying to search uh in here in the location field for Edmonton right here so it found the location search buttons and it's trying to search for Edmonton but it's not working and the reason it's not working is because it's not able to find uh right here it's not able to find location search and the reason it's not able to find location search is because after you opened your new tab lenium web driver doesn't know to search on the new tab it's still trying to search for this web element on the old Tab and so one way you can go around this is let me show you the new function we have right here so I'm going go down here one way we can go around that so everything's the same except for here so this one we're basically specifying oh this window that you see right now is our original window so remember that that's our original window and then we basically tell it to click that button to open new tab and it's basically saying for every window in all the windows open if your if that current window is not equal to the original window right here then let's switch that current window and see what's going on there and so basically after we open a new window it sees that new window is not equal to original window and it switches to the original new window and by doing that we're able to find location search we're able to search for it so let me show you what's going on here uh but yeah just take a notice of all this uh this uh syntax so basically switch to window we're also using current window so it's just using the web driver so let me run this for you so drink this over maximize it again I'll open a new tab and now right here it should search Emon or right here yeah so it searches Edmonton right here and then all you have is Edmonton right here so yeah it closes itself and it basically says oh yeah it worked we were able to search for everything there and yeah so it works so yeah so this is the video part two uh I hope this video was helpful we learned a little bit about weight statements different ways to handle Windows uh and ways to handle uh sequential commands in selenium and python if you found this video helpful please give the video a like And subscribe to our Channel thank you for listening and we look forward to seeing you next time
Info
Channel: Test Automation 101
Views: 358
Rating: undefined out of 5
Keywords: Selenium Python Tutorial, Automated Testing, Python Selenium Setup, Python Programming, QA Automation, Selenium Testing, Python Scripting, Interactive Web Elements, Selenium WebDriver, Exception Handling
Id: PbqUVMxj9Os
Channel Id: undefined
Length: 20min 31sec (1231 seconds)
Published: Mon May 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.