How to manage windows with Selenium Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video I'll be covering various different ways we can manage our window using selenia python first in this PowerPoint I'll cover the different commands that we can use in selenium python to manipulate our Windows to get different information about our windows and so on and then I'll show you some examples using Code how we can use this in our own uh sunum python project so the First Command we're going to be covering is get window size and like this says we're just getting the size of our window and one command you can use is driver. getor window size. get height or you can get width depending on what dimension you want you can also store these Dimensions using a variable by using get Windows size and not specifying what specific uh Dimension you want and then afterwards you can get the width and height after depending on what you need later on you can also set your window size uh and so what we do is we use set window size and this will specify how big you want the window on your screen and so this is 1,24 pixels by 768 pixels next thing you can do is you can access each dimension of the window position for example you can store it in window position by using driver. getet window position and then you can use position . get X position. getet Y to get the XY coordinates of the window you can also move the window to a specific position on your screen as well by using driver. set window position and so this is 100x 100 Y and you can also use window Max Min and full screen to manipulate your windows so window maximize window would be making it big minimize window would be making it small and then full screen window would be making it full screen you can also use page screenshot to take a picture of your current screen and what we do is we use driver. saave screenshot to do that you can also take screenshots of specific web elements on a web page for example if you only want a small portion of the web page being taken a screenshot of you can use the command uh image. screenshot after you define which element the image is in order to take the screenshot of that specific image on your page and finally you can also store the header element and how do you store the head header element well first you have to define the header element by its CSS selector and then you can execute some JavaScript to store that header element and so I'll be showing you how to do all of this in your own selenium python project so right here I have my project open up from previous videos um if you uh want to know how I got to this point in the project you can go ahead and pause this video right now and look at our previous videos to see how we came up with all of these different uh different Python scripts uh to get to where we are currently but we don't need to information or knowledge of these current previous scripts in order to understand what's going on today so without further Ado I can actually close all these windows and we can just focus on what we need to know and what we want to see today so the first thing I'll do is I'm going to create a new script in this and I'm going to go to this test folder right here and then I'm going to right click and then create a new python file and I'm going to name it testore selenium manage window and this is our new test script and notice how I always start all of my test scripts with testore and this is a requirement a p test and so uh for the sake of this video just know that we have to do this because this is a requirement of P test if you're interested why I suggest you go look at some uh some of our previous videos uh to understand why we do this but yeah so in this I'm going to go through each of the commands we talked about earlier uh and show you how we can use that in our own code so the first thing I'm going to do is I'm actually going to import some dependencies so I will go ahead and import a few things so import time import the web driver and then also import print options and so on the next thing I'm going to do is I'm actually going to create our first test and this first test is dealing with Windows size so uh what I'm going to do is I'm going to define a test one test one and what I'm going to be passing into test one is Chrome and for Chrome um if you're not familiar with what Chrome is uh Chrome is basically the web browser that we be using uh to start a web driver so it's not here currently but if we look at here for example uh if we look here so this is we used Firefox here but if we look at another example maybe this one this is Edge so let me look at this example this is Firefox if I look at this example this one is this conf test so if I go to my comp test right here I see that I've already specified what Chrome is and chrome is basically when we start a web driver using the Chrome web driver and I have the tag pest. fixture and it's in the file com test and what this allows you to do when you use the py test uh package is it allows you to pass this function as an argument into other um into other different functions within the same project and so if I go back to my selenium manage window here I can just pass Chrome directly into this function and so what I'm going to do is I'm going to print this first test by passing Chrome and I'm actually going to be telling it to do a few things so the first thing I'm going to do is I'm going to tell it to go to this web page and then I'm going to tell it to sleep for 3 seconds and then I'm going to define the width equal to chrome. getshape window size and then after that I'm going to say dog and I'm going to tell it to get the width and then I'm going to say that the high is equal to Chrome doget window size again uh but this time this is equal to get. height and once I have that I'm just going to tell it to print a few things for us uh so I'm going to tell it to print off what the height it got and what's the width that it got right here so that's the first test uh let me go ahead and run this and show you what it looks like so it opens up onto my other screen so I'll have to drag it over for the sake of this video right here and then it'll sleep for three seconds and then it grab the window width and the window height as we see here now let me create a second test but let me show you a different way we can grab these different features in different uh statistics so this will be Test 2 _ 2 and again we'll be passing in Chrome and uh we're going to be using um we're going to tell it to go to the same web page right here so we're keep this the same but this time what we're going to tell it to do is size is equal to Crow doget window size so we'll save this size parameter first and then we'll access the width and height separately so we'll do size. getet and this one we'll specify width and then the next one we'll say that height is equal to size. getet this will be height right here and again we'll tell it to print what the width and height that we got uh right here so like that so if I go ahead and run this again I run this test I'll drag it over from the other window I'll leave it for a few seconds and it should Clos by itself and then we see it gets its width and height again and it's the same quantities so next I'm going to show you the third test and in the third test we're going to do something different we're going to I'll show you but what we're going to do is we're going to set the window size so let me do this pass Chrome in again and then we'll say uh we'll go to the same web page same URL and then this time we're going to do chrome. set unor window size and it's going to we're going to set it as 224 968 and um since uh we're mostly doing the same thing as above I'm just going to copy and paste this over we're just basically going to get the width and height and see if we actually set the window size properly so this should be this uh and then now I can go ahead and run this again I'll have to drag it over before so that's why I sleep it for 3 seconds now after it sleeps for 3 seconds it'll manually increase in size as you saw briefly and then we'll grab the different quantities so you see right there okay so that's the third test let me show you the fourth test now uh we'll name this test four and again we'll pass in Chrome and in this test what we're going to do is we're actually going to get the X and Y positions of our window so let's first go to this URL right here and then next what we're going to do we're going to say chrome. getet window position and we're going to say that we can get that X right here and then the Y is equal to chrome. getet window position and this is get doy and then we're going to print these quantities again so right here and let's see if it does it properly so I guess depending on which location I drag and drop it on in this web page it'll make a difference so if I drag it there this is what it the difference is so this is our X and Y quantities so let me do this again but this time instead uh what I'm going to do is I'm going to get the positions using a different method so again I'll name it test five name it Chrome passing Chrome uh and then this time since it's very similar to we did previously I'm just going to copy and paste this entire code that I've written already over and so instead we save it into position and then we get it separately afterwards so go ahead and run this drag it over now it'll basically grab the quantity see I dragged it to a different location here so the different so the position should be different again so yeah the position is different as we see okay so now let's move on to test six uh so I'll name this test six passing Chrome and we'll do chrome. getet well actually I will do this I'll tell it to go to this URL again but this time what I'm going to do is Chrome do set window position so I'll set the window position to - 600 minus 200 and then I'll tell it to sleep for two seconds and then I will basically using the commands to get the position of the window I will get the position of the window using these commands right here let me run this again and see what happens so again I'll drag over but this time it should uh move by itself see it moves by itself because we're setting the position of the window using this command and you see that this is the XY position after we set it to those coordinates okay so let me move on to number uh that's seven uh this test will basically uh manipulate the window size so name a test seven and again I'll pass Chrome in and then this time we're going to go to this URL and I'm going to use a various different commands so first I'm going to do maximize window and now I'm going to tell it to sleep for one second then I'll tell it to minimize window and then I'll tell it to sleep for one second again and then I'll tell it to full screen the window and then I'll tell it to sleep for two seconds and let me show you what this looks like so I drag it over so the first thing I should do is maximize the window as we see it maximizes it then it minimizes it and now it's full screening it so that's our second uh seventh test the next thing I'll show you what we can do is some different things we can do for screenshotting so let's just show you a few things so I will paste this over here so in this one we're basically going to this URL but we're just using Dove screenshot in order to save a screenshot to to our file location and if I take a picture uh let me fix this file location so this should be Python tutorial and then now let me run this I drag this over it should take a screenshot takes a screenshot and then it says it's done and we see that saved right here open the screenshot this is what it looks like the next thing I can do is we can take a screenshot of a specific element on our web page so again let me call it def test take element uh element screenshot and again I'm passing my Chrome just P this over to save some time and this time I'm going to specify what element I want to take a screenshot of so I'm going to do chrome. find element and I'm actually just going to copy over this information so I don't mess up typing it but I'm basically using the X path value of the element and this is what it is and then after I find and spe specify which element I have I'm just going to do image screenshot and then this will take a screenshot of the image and again I'm going to take this and make sure I I have so I'm going to take this Command right here and make sure I get the right location to save it and this time I'm going to name it screenshot two and let me run this and show you what happens so go ahead uh drag it over and it'll specify the image uh or the uh the thing and then it'll take a screenshot and now let me open screenshot to it takes a screenshot of this portion specifically and yeah so that's what this part does and now let me show you the final test that we're going to do uh this one will be using header element so call it Dev test header Chrome uh and again I'll use uh do that and I'll say header is equal to chrome. find element and I'm going to say that this element is defined by its uh CSS Lector uh which is basically header one and uh what I'll do is I'll tell it to basically print this header using the JavaScript command so this will be execute execute script right here and this will be uh oh let's say let's do return arguments zero do inext so that's the command we're using and then let's say header so go ahead and run this I can drive this over it'll basically grab the header from this web page um so so it says no such element uh it'll keep running uh it's still collecting the element uh and afterwards it says the test is failed so let me see why it failed uh find element okay so uh actually I specified the wrong URL location because on our web page this specific header that we were looking for is at this URL so that was my bad let me set that there and let me try to run this again again drag this over we see that it has the header right here now and it passes the header web elements and locators and yeah so I've showed you a various different different commands we can use for managing your windows um if you found this video helpful please give this video a like And subscribe to our Channel and yeah thank you for listening we look forward to seeing you next time
Info
Channel: Test Automation 101
Views: 129
Rating: undefined out of 5
Keywords: Selenium Python Tutorial, Automated Testing, Python Selenium Setup, Python Programming, QA Automation, Selenium Testing, Manage windows, screenshots, window size and position
Id: oLZrVoiO8GE
Channel Id: undefined
Length: 19min 12sec (1152 seconds)
Published: Mon May 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.