Selenium Python Small Sample Project 1 | Unit Test, HTML Reports

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session in this session we are going to create a very simple and small sample project in selenium Python and here we are going to create a test follower for Google search we will use implicit wait for ten seconds we will maximize the window create some unit tests using per unit or pythons inbuilt unit test library we will add some HTML reporting library so that we can generate HTML reports and finally we will run from the command line so let's get started and I'm going to use PyCharm as the IDE so I'm going to go to my Python and I have already have a project here I am going to create a new folder so I will do a right click new directory and I will name this as sample projects and inside this directory you can see this is here and inside the sample projects I will again do a right-click go to new and I will create our Python file and I will name this as Google search test and say ok and you can see it is here and the very first thing is I have to import selenium webdriver so I will say from selenium import webdriver now of course you should already have imported the selenium Python libraries that we have covered in the earlier sessions so now I can say driver equals webdriver dot I can use any browser I am using Chrome here and in the brackets I have to give the executable path of the chrome driver exe so here I have saved all my driver exe files in a folder called driver and here is my chrome driver exe I can copy the location you can copy the entire path or relative path and here I am saying dot dot this will take me to the root folder of this project which is selenium and after this we have to go to drivers folder and inside reverse fold er we will find chrome driver exe so once this is done I will say driver dot I can set implicit wait and I'm saying implicit wait for 10 seconds so this means that whenever they will be any delay in loading of any object that webdriver will keep on checking for every 500 milliseconds until the maximum time of 10 seconds and by default it is set to zero seconds but we can now say we can say driver or implicitly wait and we can set it for any duration I am saying I am setting the wait time for 10 seconds so we have said the implicit wait for 10 seconds and now I will say driver dot maximized window and now I can create our test so I have to go to google.com and then in the search box I will add some text and click on the Google search button so I will do a right click and do inspect to check the locators and here is unnamed locator named value or name property is available and the value is Q and similarly for Google search I will do a right click and inspect and here again the name attribute is available and the value is BTN K so I'm going to use these two so in the script I will say driver dot get and it is HTTP google.com so this will take us to google.com and then I will say driver dot find element by and the name is Q for the search box and dot the action is send keys and I am just going to type there automation step by step and after this I will say driver dot find element in you can see there are a lot of functions for locating elements I'm using find element by name and here the name attribute for the search button is BTN K and here the action is click so I am saying dot click and after this I will just close the browser I will say driver dot close and I can also put I will say driver dot quit and Here I am just going to print test completed so this is our simple test and we have done these steps so I can now do a right click and say run Google search test and let us see it opens a Chrome browser and it should maximize it now yes it is searching for automation step by step and closing the browser so our test is running fine and now we have to create a unit test from these tests so for that the very first thing is we have to import the unit test module I will say import unit test and this is the Python inbuilt unit test module so it will be available and then I will have to create a class I will say class Google search you can give it any name and in the brackets I have to say unit test dot test case so we are inheriting from the test case class of unit test and a colon and inside the class I will now create some methods so the very first test we have to create our setup method now you can see we have to set up methods one is set up and then set up class so set up method will run before every test method and set up class method will run only once before all the test methods so we need set up class method because we are going to open our driver a browser here so this needs to be done only once before all the methods so I will cut it from here and go inside our setup class and paste it here now make sure of the identity they should be these five spaces or a tab space so everything which is here is a block of setup class so you should take care of this identity in Python and because this is a variable CLS here you can give it any name I will say CLS dot driver CLS not driver and here as well CLS tour driver so now this method will run once before all the tests it will instantiate our driver and open our Chrome browser set implicit wait and maximize window now I can three it or test method I will say def and make sure that you start with the word test and then you can give it any name I will say search for automation step by step now it is very useful if you give some intuitive name so that you can know by just by the name what does this method is doing so it is searching for automation step by step and now inside this method I will cut these three statements and paste it inside this method I will cut it from here and paste it here again take care of the identity and Here I am using self as the variable so I should say self lot driver here again here self dot driver and here as well so in the test we are going to google.com searching for automation step by step and clicking on this button and then let me also create another test here I will copy this and paste it again so that there are two tests and Here I am just searching for my name I will say search for Raghav pal and here I can also rename this search this thing so I am renaming the function name so there are now two test methods and here again I will create our teardown method so I will say def teardown now again you can see there are two teardown methods one is teardown which will run after every test method and then we have teardown class which will run once after all the test methods so we want to close our browser only after all the tests are completed so we are using teardown class I will cut it from here and again paste it here and make sure of the identity and because this is class method we have a variable CLS I will use CLS dot and again I will use CLS dot and that's it so now we have our unit tests ready I can do a right click and I can say run unit tests for Google search test and I will run this and then let us see what happens so this is not running let us see it is saying failure and here it is saying missing one required positional arguments CLS so the problem is when we use the class methods in unit tests like set up class we have to give the annotation at class method here and again in the teardown method we have to give this annotation at class method and save this and do a right click and run unit tests and let us see so now it is running working the Chrome browser maximizing searching for automation step by step and then it is so it did not run the second one there was a failure so you can see the result here the first one is passed and the second one is fail and there is some error so let it be we will see it later but our unit test is working fine now we have to run it from command line as well so let us see how to run JUnit test from command line I will go to this file and copy the location I will do a right-click and copy the location so I will say copy path and go to my command line and first I will change the directory to the location of this Python file so this is under sample projects I will go here and now i have to say python and the name of the file which is Google search test dot dy and if I run this now you can see nothing is happening so we have to use a flag - M and unit test so I will say Python - M unit test and now the name of the python file and run this so they should run now it is opening a Chrome browser maximizing and running our tests here and this is running fine so you can see one passed and there was one error so if you go a little up you can see there is dot and E so dot means pass and if there is a failure it will say F and if there is an error it will say e so this is running now here but if you just want to run it with the earlier command which without using the flag and just sing Python Google search test or py for that we will have to add our condition here at the end so go at the end and say if underscore underscore name equals equals underscore underscore main then colon and say unit test dot main so now if we try to run this without using the minus M unit test flag I am trying to run this here so you can see it is still running so our tests are running fine here so this time it ran the next test case as well and this is running fine and two tests n in nine seconds so now we can run it from command line as well so let us now see the next step which is adding HTML unit or HTML library so that we can see our results in HTML file so for that what you can do is you can install so you can say flip install HTML - test runner and you can actually see it here if you go to your browser and Google and say Python installed HTML tester now you will get this website here HTML test runner and you can go here and you can use this with install HTML test Runner and if you want some more details you can see the description so a test runner in Python for human readable HTML reports and here is also given our usage of this file so I will go and I can say if installed HTML test Runner and say enter and you can see in my case it is already installed and I can see this by saying tip freeze and this is available in the Python libraries I can say hip list this will also show me the same list with all the PI Python packages and it is here and I can say pip show HTML - test runner it will show me all the summary and details for this file and I will say whip check HTML - test runner and it will show me if there are any broken dependencies so you can see it is saying no broken requirements found so this is fine I can use it so in my script I will import HTML test runner and then everything remains same at the end I will go to this condition which is unit test dot main and here I will say tester no equals HTML tester no dot HTML test runner and in the brackets I will give the output directory so I can give any location or any folder for saving the files and I have a reports folder here I will right-click and copy the location and I will give it here in the output so I'm basting the location of this folder and of course you can use a double backslash it if you are on Windows but it is always better to use a single forward slash which will work on Windows and other operating systems as well so I am changing all the back slashes with a single forward slash and yes this is done so now our script is ready and now we will go to the command line and run this again so I will use Python and the name of the python file and run this so it is going to grow and searching for our tests and it is running fine and let us see the output so it is same generating HTML reports so now you can go to the folder and you can see it has created this report just now and if I do a right click I can open in any browser I am selecting chrome here and you can see the two test case passed and also just to verify if it catches the error in the report we can make some deliberate error here so I am changing the name of the mint BTN K and I'm changing it to be TN k 1 which is invalid and this should fail in the second test case so let us save this and run this again and check if this error is shown in the reports as well so it is running the first test case and in the second test case it is not finding the button and also you can see it is waiting for 10 seconds because we have set our implicit wait for 10 seconds so it will wait for 10 seconds and finally it has thrown an error and let us see if this error is shown in the HTML reports so it is seen generating HTML reports I will go back and see you can see this is the third report getting generated here and I will say open in browser Chrome and you can see the second test case failed because of error and if I save you unable to locate the element with name BTN case so this is very intuitive and now everything is coming in our HTML reports so this was a very small and simple sample project in selenium Python I hope this was very useful for you thank you for watching
Info
Channel: Automation Step by Step
Views: 59,983
Rating: 4.9041915 out of 5
Keywords: selenium python sample project, selenium python project, selenium python unit testing, selenium python html reporting, selenium python reports, selenium python beginner tutorial, selenium python for beginners, learn selenium with python, selenium python by raghav, selenium python step by step
Id: H9HUVSA_78U
Channel Id: undefined
Length: 16min 39sec (999 seconds)
Published: Sun Sep 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.