Selenium with Python | Unittest+ HTML Reports + Page Object Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so in the last session we have discussed about unit tests so python unit tests and as part of unit test we have just seen how to use our sessions how to create the unit test cases and today we are going to show you how to create HTML reports in unit test Python unit test framework and also we will see one test case which generates the HTML reports okay after that I will also show you form based so form is nothing but page object model so we'll create a sample project and inside this project I will take a few test case and I will use unit test framework page object model approach and I will automate the test cases and then we'll see the HTML report temperature so this is end to end will be covered in this particular part so first let us talk about how to generate HTML reports in PI you have you in unit test okay so to do this we need to install one plugin called HTML HTML iPhone test runner so this is a particular plug-in which we need to install on top of Python so how to install this one so let's go to command prompt and open this command prompt as an administrator and once you open this command prompt here you already have the Python it's on top of Python we can install this one so when you type this command it will list so this will give you what are a list of plugins which you have already installed on top of Python so this command will give you different type of plugins which are already installed on top on top of Maya so if you see here PI test - HTML so fire test I've not this one and this one so HTML - test runner so this is a plug-in which we need to install so this plug-in is already installed on top of Python in my system but if you want to generate HTML report by using unit test framework we need to add this particular plug-in ok so how do you install this one is very simple this type install just like other plugins however I have installed singularly pip install HTML iPhone test runner so this is a command we have to execute so this command will install this command will install HTML test runner rapport so how to install this this is a command which we need to run in that command so this will able to install HTML test runner so once it is done now we need to open the PI champ let me open my pycharm so in the PI jump also we need to install this particular plugin and then we can generate the reports so let me just start opening my PI charm alright so now my jumper is open right so here I'll open the project which have created earlier so python unit test project but this is the project we are currently working on so here in the pycharm we need to also have this partner plugin HTML - test runner so where we need to add is we need to just go to file settings and we need to select this project and go to the file settings so inside the settings go to project interpreter and here you just click on the plus button just hold on alright so we need to click on this plus button then here we need to search for HTML - test runner right so now you will get this so this is a plug-in you will get it so once you get this Poliquin just click on install package ok you need to click on install package so now it is trying to installing this particular package so now it is install successfully so once you have done this again go back now you can see a new option which is coming here then apply then ok ok so done so once you have done this now we are ready to generate HTML reports so now I will create one simple Python test case using unit test features and I will run the patellar test case and generate HTML reports so let's create a new test case so let's go to by champ and Here I am going to create a new test case by click new Python file so I will give some name to this and whenever you create a new test case in Python unit tests it should start with the test keyword okay so the same test case underscore underscore R and underscore HRM application how inch just case underscore origin report HRM HRM underscore login ok this is my test case I am going to create so like type dot Python so this is my test case I just created alright so I am going to use unit test features inside this particular test case so first we need to import that a number of packages so from selenium I'll import webdriver ok I'll import when drive from selenium we have to import webdriver so no Python interpreter convert for this project we need to just configure click on this and we need to select proper interpreter ok otherwise it will not work so select the proper interpreter and then ok ok so now i have just created one draws it again it is showing thanks so now I've set up this proper environment so here we can start writing our course so now let me import from selenium so from selenium so if you are not properly imported we need to just again make sure the package is properly installed let's go to this Python test project one more time and here selenium is available this and read a new file so click on the new python file so here I am creating test underscore or a number store HRM and Oscar login just dot e by ok so this is the test case I have created now we need to import is selenium so from selenium so this is the first package which we need to import if the selenium is not available we need to just add that plug-in also again go to settings one more time man here ceiling is there but somehow it is giving an error let me just remove this and I will install one more time so he installed let's go to list option agreement ring and just check for selenium and spite on bindings for sealing in then install this package ok now it is installed close this okay so now properly instance about not getting an issue so from selenium so we have the importer and after that I will also import unit tests so import unit test is a inbuilt feature in Python itself right so you need test li happening and then we have just installed it steam at test runner to generate the reports right so that is also we have the import HTML test run so this is also very important to generate the reports alright so now I am going to create a test is so my test case is [Music] orange login to test this math test case so let us see how we can do this the first thing is I'm going to use unit tests of features right so here I will create a few min there so the first method I will create is prerequisite so to launch the browser setting of the properties I will create one method and we refined the Homebase title I will create another method and a login test I will create another method and finally to close the browser I will create one mark so here the first one is I will write a method called launch browser this is the step 1 launch browser and step 2 I will verify homepage title so this is the second step and the third step is verifying login this is another step and the next one is close browser so this is complete test case so for every step I will create a method so launch browser is one time activity and close browser is also one time activity so I will define two different methods for these two steps I'll specify them as setup class and teardown class so in in unit test so if we create setup class method this will execute only one time before classes started and there is another method called teardown class when I create this method it will execute only one time after completion of all the methods all the test matters so we have discussed about these methods are in the previous sessions so setup class will exhibit only one time before all the steps have been executed teardown class will execute only one time after completion of test method so large browser will be I will put the script in setup class and close browser script I will put in the teardown class and rest of to our actual Testament worms okay so let's go to by Japan here I will create a class first class and I'll name it as orange HRM test and whenever you create a new class should be extended from unit test test is okay you need test unit test dot test this is a mandatory and then only this kill test is considered as a unit test so inside the class as I said I will create multiple methods so first method is def setup class setup class so these are a breed feathers in unit tests because these methods this Butler method will execute only one time the previous session a target about four different methods setup class teardown setup class setup module anti-air model right so there are multiple fixer methods so this method will exhibit only one time before actual test methods have started and inside this method I will write something called and here CLS is a default parameter so just like if it is a create a function in the Python will be taken self as a default parameter but if I create a same function within the method within the class it is called as a method that's the basic difference between function and method functions can be created without having class if I don't have any class if I create directly that's part of the function and if I insert the same function inside the class that is called as a method okay in function self is a default parameter and set up in class and teardown classes are P defined methods these are all predefined methods CLS is a by default will take parameter so whatever variables we are referring you have to eat for those variables by using CLS Eva so here CL s dot driver is equal to I'll specify the driver so webdriver dot Chrome and here we need to specify executable path is equal to underscore path and is equal to location of your chrome driver so currently I'm using this is your location of you are okay so once you specify this then I will maximize this window so to maximize this window is CL s dot driver so whenever you referring the driver we need to always use els dot rhymer so CLS rio dot - my Speedos this will be maximized so now i have just created two steps in the setup last this method will exhibit only one time before class is started after that I will create one more method to verify the home page title and verify the login so to do this in the second method I will write and every test every method in the Python file should start with the test keyword okay so that's the asper the unit test standard so test underscore test underscore homepage title verification okay and this is again self because this is a normal test method and this is a actual fixture class a fiction method setup class which is coming from unit test all right so test underscore home pasted actual all the test methods we'll start with the test key after that you can give any name so inside this method I will try to open the you are so selfish auto driver self door driver dot get and here I'll specify the URL of application this is a URL of application alright so then I will verify that idea so how to verify the title so in unit test we have a set commands so we can use a set like this l dot except equals asset equal we can verify expected and actual so what is our expected is the expected title of the page is orange HRM and we need to compare this with our actual how to get the actual self or daughter driver dot IT so this will give you driver dot idles actual command so we are using his variable inside the method so we need to use a self self dot driver or tied it this will give you actual title if both are equal my test method will pass otherwise test method will fail and if you want to write some use a different message also you can write as a third parameter so we paste at least not much if both are not matching the test method will fail and this Butler message will be painted as a consulting output ok this will verify the tank so these are another testament and the next one is i will verify the login process so to verify the login process i will create another method then test underscore to login so the test method will always start with the testicle so inside this again and every test is independent okay see this every test is independent so now again I will try to open URL in this test so I just copy the same thing again how do you open the URL and this time I need to find out the elements so just go to your web page and just open this URL so once you open this URL so you will see username and password first we need to identify these two then enter username you need to enter password then click on the login process so these things are seen so I'll have some oh here let me copy so self driver dot find element by name send kiseop admin find element by name passwords and guess of admin then find element by name submit button and then click so once we have performed the click again I want to verify the title of the page so how to verify the title of the page so I'll just copy the same stuff here so after login also the title is same so I'm just putting on a charm so this is a title of the page so we have done home page verification a homepage title verification and login test after that the next step is I want to close my browser so to do this I will create one more method def def and this method I will make it as in the setup class will exhibit only one time before testing methods have started so after completion of this test method I want to close my browser so how to do this I want to close all the drivers so here def teardown so here I'll say teardown method teardown class I will read so teardown class is a method so sorry here down class so here it will take CLS a zipper so teardown class method will execute only one time after completion of the test method from the class so inside this butler method I will just write CLS dart driver dot twitch so I want to read from all the browser's and here I will just write some print statement test completed so this is overall my test case so the test case is imported extended from unit test test case set up the class will exhibit only one time before actual test methods have started and teardown class will execute one time after completion of all the test methods so here this one and this one is exactly equal to before class and after class in test engine so in the testing you may have a before class and after class annotations right it is also same it is also same thing alright so now I have created two different methods so setup class will execute one time before tests have started teardown method will start execute only one time after completion of actual test methods this is actual test please now if I want to run this unit tests right so we have to write a command here at the below the command is if underscore underscore name is equal to so again underscore underscore if underscore underscore name underscore underscore is equal to in the single quotation underscore underscore main underscore underscore important so this is a command we have to so you need test you need test dot main so this is a command we have to add at the end of the unit test case and then the test case will execute so now let us execute and see the how this just guessing but still we haven't generated any reports for this ok so let me just exhibit this test case from as unit test firm and once you started execution and somehow it is getting failed so ramjirao test let me just crossed it again okay so it is getting failure let me close this and I will just run one more time and it is giving some issues class orange which are in test and test keyword is there and yeah so one more thing before what here is so set up a class and teardown class three different methods for unit test features so whenever you are using predefined methods like this we have to specify these are all class methods okay at the rate class method we have to specify so this one and also this one because these are all not normal methods these are the pictures so now let us execute okay now it is launching my unit test and launch to my browser so two tests have included one is verifying the title of the home page the other one is login taste now it is performing the logging okay I think it is done so tests are being completed so tests have been to Tessa run so one is home page title the other one is long so both are successfully fine so executed now I want to see the report actually how do you generate the report from this so currently doesn't it providing in the report here so if you want to generate a report so we already added one plugin right so in the settings if you see here so HTML test run so this plug-in package we already installed so we need to use that one so how to use it here is instead of unit test plot main instead of this command we need to pass some parameter here so the parameter is test runner is equal to test runner is equal to HTML test runner dot HTML HTML test runner so this is how we need to call test runner is equal to HTML testrunner dot HTML test runner we need to specify the location where we are going to generate the report so here I'll say how to put is equal to so this is the parameter so output is equal to output is equal to so here and say some folder names so let's say output is equal to I want to generate a report ok so wait I want to generate report I will just create one new folder inside click on the new directory and I will name it as a reports so I want to generate I want to store the reports inside this folder right so how we can store the reports inside this folder so output is equal to and in the single quotations or double quotation you can use anything and here dot dot slash is representing your current project directly so inside this project inside the report I want to store my HTML so reports so this is a folder name you have to provide okay so this is the houses dot slash slash is representing current project directory inside the reports your HTML reports will be generated so this is how we need to specify the location and save it now again let's run one more time so so we have added this report also right so let's tweet along this run you need test for tests so again it is started running your desk is alright so now it is executed first to test case now it is start in second and also demanding so two tests have been executed and everything is fast now let us see whether reboot is generated or not so there is no report generated there is no report generated so let's cross-check the path and let me use last last link this or if it doesn't work we can just you direct path so you can just copy and show in Explorer so this inside reports I want to store the a report so just get the location I think it is accepting any forward slashes let's try this one forward slashes so dot dot is representing current project directory so let's try one more time and see right click on it ok now it started running all right so now two test cases have been created but still it is not generated report so let's give the direct path so C colon slash slash users and double slash slash slash users admin by running the test for object and reports sorry I think we have it before so let's just remove this one reverse ok now I have given the path so let us try to execute it if still not created we need to just run through command prompt this to be less than this so currently report is empty directly doesn't have anything so let's run one more time ok now it is executed but still we didn't get the report so - target or engine test it is running but it is not giving any report let's refresh this one so this report folder should contains a report actually so it is not created a need report so let's see why it is not created in a default so here we need to just go to telling us because we can run our test case through command prompt then only it will be generated a new report so here there is option for terminal so it will give you the location of your project so in this inside this project so your test case is present here so this test case we need to execute so how to execute your test cases so here we have a command called I done all we can say unit test unit test and share name of the test case is unit test HRM got reviler seconds good unit test is not working then again it is started let's see okay now it is generated so generated HTML report so if you want to generate an HTML report we must run your test case through come on so what is the command we have to use it here is this one this is a command let me just copy this so this is a command [Music] run uni test test is to generate report okay this is vanity so if you really run you at recipes you cannot see them so now I have executed so it is saying the report is generated right so let us go and see the report so under report folder you can see the report here this is a new report which is generated according to the time step so open this show can open in browser any of the browser we can use it to open this report and just open this in chrome browser so now I have opened this now we can see there are two test methods we have in the test case so both are being passed so suppose if anything is failure it will also give the failure message let me just make it failure so one of them so here instead of orange HRMS a power is a charm one two three so my first a second estimator will be failed that's my expectation right now go to your command from so you can also run this terminal inside the terminal or I can also run through command prompt so you can use this command Brown also so let me open this command prompt from here also we can okay because by charm itself is given some title command from Denmark or else he can run from here first of all you need to go to the project location if you want to run through command prompt 'cd your project location so now we are there in the project location the same command we have to run from here I save Python test orange HR and test dot P by so when I execute this it will also execute through command so we can run either here in the PI champ it in the PI champ or we can run through coming from so now it is launches my browser so most of the times we will run test cases through command ok the first test case is failed because I have given in married hiding so now the second test case is fast so one is failed generated HTML report now let us go and see the latest report when I expand this the latest report I have been generated so which one is the latest the below one is the latest so again open browser with a chrome so now we can see the second test is getting failed if I click on the view it will give you the reason why it is getting failed so we are expecting this one but it is having orange HRM isn't dying so that's the reason it is getting failed all right so this is how we can generate test case by using unit test framework and we can generate the report so what are all things we need HTML test runner we have to install on top of Python using this command and inside the PI jump also we need to install this package and then we need to create a test case and at the time of running your test case we have to specify this parameter if I don't specify this parameter you will not get the report you can run the test case but he can he cannot get the report so this parameter is mandatory required and this is a location where you want to store your report so instead of given the complete path you can also use the short path just fine dot dot slash is representing project directory so you no need to give this complete path ok so let's try this one more time so just run one more time so in store for giving the complete path they can also give the direct path so slash slash dot dot slash is representing the project build path yes ma'am it is properly created HTML now just refresh your reports now you can see the new report is generated now can open this report using one of this browsers so now can see the same report is generated so this is a shortcut way so slack dot dot slash class is representing your project directory inside this inside the report folder I want to generate the report right so if you want to generate the unique test report you must execute it what excuse to come B so this is a command hot date this is the name of the test case Python is a keyword so command to run unit test case to generate so that location is user-defined wherever you want to generate respond again just specify the location okay so this is the first thing so we have seen how to generate HTML to install Python HTML report and then we have seen how to write selenium Python test case using unit test with HTML report so here we haven't used any framework in page object class anything so just it's a plain test case very basic test case now how we can convert this into pom based form based project Oh miss page object model so in the real time most of the time we use pH object model so why do we need to use page object model so this is very important topic which we need to understand why we need to do per page object model so every times whenever you use any kind of framework page object model should be followed so what is the importance of page object mode so let us see I have what this is one test is to add this case I have multiple test cases so in my application in my application I have multiple pages let us say this is page one this is page two and this is our page 3 page 1 page 2 and page 2 I have multiple pages now my test case one required one piece to be interactive and my test case to will interact with the two different pages and my test case 3 will interact with the three different pages so this is my requirement so in the test case one if you see this particular approach what I have done is I've identified these elements within the test case itself so in this approach the problem here is in the test case one I need to identify all the elements which are present in the page 1 and put we should put here and in the test case 2 again I should identify the elements from page 1 and page so page 1 elements are already identified test case 1 but again we need to identify them that is case 2 odds and again test case 3 also I need to identify the element from p1 p2 and p3 all the pages so here there are two problems the first problem is reputation so the same page elements we have to identify multiple times for every test case so that is a one problem so the second problem is suppose this page elements I have used in three test cases all it said in test cases suppose tomorrow if the element properties have changed in this particular page now what I should do now I need to go and modify each and every test case use those elements all the elements from all the test cases I need to model so these are the two important problems we have in that normal approach if you follow this particular approach if you have a single test case which contains a test methods along with this element identification methods so now what I am going to do is using page object model approach we can separate them test methods are different and page elements are different for every page in your application I will create one a separate file this is also Python file so i will read separate files for every page and this file contains only elements only elements and action methods means to perform certain operations on these elements I will perform I can also create the action method inside this inside this files so every page I will create one new file which contains only elements so what this file contains elements press action methods so action method in essence what is operation we are going to do on this element so those action methods I will create every file now in the test case I will create just only test methods suppose if I want interacted with the page one elements I can just call those elements from this okay the first class and in the test case to I want to use element from the page 1 and page 2 so I can interact with this file and this file because these two files contains elements of p1 and p2 and in the test case 3 I want to interact 3 different pages then I can use this particular file this file in this file ok so because these files contains only elements so if I create page elements page 1 elements in the FASTA file the same file I can use in test case 1 case 2 and test case so in this particular approach to problems are solved one is repetition so every test case we no need to identify again and again so once we have identified those element keep that in one file you can reuse those elements in all the test cases so one problem is solved the second problem is supposed to more if any element is modified then I can just modify that element property in one file here and that will automatically reflected on all other test cases so this is the main usage of page object model so let us convert the same test case using page object models so let us see how we can convert this into page object model so this time I am going to create a completely new project and I will take a test case and have used page object model approach and using page object model approach how we can create the test case and also how will generate the HTML I'll show you a small framework by using unit test so to do this what I am going to do is I will create a new poem based project okay so this is also unit test cross your unit tests but form based project I will clean always positive in create inside the project I will create a multiple I will create pages folder pages which contains only pages and after that okay so after that inside this I will create another folder cut test cases so this folder contains a only test cases and here I will create one more report folder so inside this I will create all the reports so each statement reports will be part of this so pages test cases and report so I will separate everything so pages are different test cases are different reports are different so let's create a small framework using unit test and page object model so these are the three components I am going to use so in this project I am going to use small project so in this I am going to use unit test page object model along with HTML reports so this is a structure I am going to create so first of all let us create a new project in PyCharm so how to create a new project so let me just create a new project so I will just name to this project as let's go to file new project this is completely new project okay so here I am giving some name Python unit test project underscore palm paste this is a completely new project then click on the create now it is asking to open this project in the same window or you want to open in the new window click on this new window then you can see the same project will be created new window in pycharm so here it is creating a new project this completely new project so again we have to install all the plugins which are required for this okay so we can just close the world window we don't require this fine so this is a completely fresh project Python unit test project palm base project this is a completely new project so if you see the settings here in the project in the project interpreter so you don't see anything here except pip and setup terms so this is completely new project so now we need to do all the settings so you need tests we need to add a bian selenium also we need to add selenium unit test package indium package for mrs. just a framework so this is page object page object model we don't need to install anything and then HTML also we need to test underscore HTML runner also we need to install so all these companies we need to have in this particular project so let's go to project interpreter so first let us click on the plus and set for selenium so I'm searching for selenium and then click on install package so I have installing a new selenium package now it is installed and after that installed what else HTML report if so HTML underscore HTML underscore test runner so this is H statement so install this package so unit test we don't require because unit test is inbuilt in Python right so now we have everything is installed just close this so now we got HTML you run our for generating the reports helium which contains all the packages and rest of the things are there so click on OK now our project is ready so now I'll create a very basic folder structure so pages test cases and then reports pages and test cases and then a big course I will create and also I will create the drivers drivers also at keeping the same projects drivers drivers also keep in the same project so this is a folder structure I am going to create drivers for justice cases and reports so let's go to pycharm so here I will create in this project a new folder and creating new directory and I will name it as drivers so inside the drivers I will keep for non just putting only chromedriver dot exe direct like appear here under drivers now similarly I will also create one board folder to store the page elements so go to new directory and here I will say page objects page objects page objects files everything will be stored here page objects is one more and to store the reports I will create another directory I'll say reports click on OK button after that I will create one more directly to store the test cases there is a test case ok ok now I have created folder structure so I can also rename this it is small it is because others also small it is right that's it trimers fine so drivers page objects report and test cases so I have created a folder structure so drivers pages test case assembly point now let us start create in the test case so driver is already copied so now come to the page object we need to create the page objects so for the same application orange HRM application just a moment ok so this time I am going to use some other application so my test case is this one so RNG charm you already seen right so I will take another test case so this is the test so the first step is launch launch Chrome browser launch Chrome browser second step is open URL of application this is a URL so once you open this URL you will get to login page so just go to login page now here you can see a login page so here username and password we need to provide so this is a username and then this is the password so once you provided username and password click the login and then we can check it the login is successful or not so enter username and password so the next step is enter or we can provide user ok that is this one email address and the next one is password provide password so the password is admin right so user is this one and password is this one and then click on login then what is our expected tightness the expected title will be this form dashboard page we will get so expected I tell is this one okay so once you login into the website you will get this - so this is application so let us try to automate this using pong page object model approach so this is our test case right so what I can do is first we need to create the page object class driver we already copied the next to says is pages they have to create a page object class under page objects so here I will insert the page I will just mean different name and you page objects okay because if you have the same name you don't get any counters so drivers page objects and reports we have a test case test cases alright so first we need to start with the drivers we already copied now I'll create a page object class so for this test case so how many elements we need to interact here as soon as I launch my page there are three elements one is email address password and then log so these three elements we need to identify and keep them in one file so we need to separate test methods and page elements in two different files now go here under page objects under page objects I will create a new file so I will create a new file under the page objects let me just create okay I will name it as a login page new Python file I will name it as a login page log beige dot be my role entails dot dy okay so inside this patil file I will have only the page elements okay so how we need to do this first we need to create a new class I will name it as a login page class and inside this I need to find all the locators and store them in a variable so here we need to identify the locators of all the elements locators of all the elements we need to identify so that's the first thing okay so what are the locators we have so let's go to this particular page identify the email and here ID is available for this element so I just capture the ID is called email and let's create one variable sure so you have to give a meaningful name so this is a textbox at I will say textbox underscore underscore user name underscore ID is equal to whatever I have captured fermentx DM I just put it store this ID into some variable so this is available textbox user name ID is this one similarly I need to also capture the other elements attributes so here have captured few elements so here so textbox password ID is this one and here login button XPath is this one and after that after logging in we have a logout link right so the logout link is this one so I have captured so these things I have captured these are the elements which we are going to interact with application now as soon as we capture this locators immediately we need to create one constructor to initialize the driver okay to initialize the driver we need to create one constructor because the driver is required to identify all these elements so first I will create one constructor how to create a constructor in Python def underscore underscore underscore underscore self , driver and this driver we will get later from the actual test case and once you get this driver and initiate this driver to local very good self pro driver is equal to time still for dot driver is equal to drive so this constructor will automatically invokes whenever we create an object for this login page leader so driver is initiated and then for every element we need to create an action method for every element we need to create an action method so how to create an action method so the first method I am going to create is on this user name what we what action we are going to perform so we need to set a user name we have to provide the user name into this particular field so I should give the meaningful name I will say def set user name so it just gives a meaningful name user name self comma I will just use one parameter called username so this parameter comes from actual test case so here self dot driver variable and means self dot Rio dot find an element by so find element we have use driver dot so it's not self dot driver dot find element it should come so let me just write it properly def set user name self comma username and or two columns inside this self dot driver I will pass later so driver dot find underscore element underscored by dot ID and here you already capture this idea of the email and this is a user name I am going to use this here so ID self dot this form so t but because all the variables and everything need to refer self keyword so salford or driver or find element by ID self dot txt box dot user name ID so this particular statement will identify this element and after that what we should do is we need to provide some value inside this element so what is the command we are send the keys where homes God send underscore he's our fan from where we will get this value send underscore peace these off this one so whatever value we are getting here they are to pass so this is actual method for setting they use a naming to use a name textbox similarly we need to also create other method so let me just create other method okay so now this is another method set password we will get this password from the actual test then salford or driver door find element ID and a for ID we already find out the element here so pass that ID dot send keys of bus and then we need to click on the login so create one more method click login self keyword and Driver dot find element by expert because I captured XPath here for signing button so driver door find element by XPath and self dot buttons log in XPath so this is available contains a locator so sparse this locator dot click action we need to perform after successful login there is a logout link so again I have identified this logout link text okay and then click on the log or so these both are different this is the link attacks this is the link text this both are different okay so I just use log out link text not this one okay so now this is your page object model so this is a page object class so which contains only the element identifications and action methods so now we need to create actual test case that's the next step so we have attached the driver we have created page object class for login test then we need to create test case so where we have to create the test case under test the cases folder so here I will create a new Python file I will name it as login test dot people now test case will be very very simple because already we have in page object last night so our test case will be very simple so this is the page object class now I am going to write the actual discus so let's create an actual disc case number so in the login test first we need to import to whatever stuff we require so first I have imported unit tests because unit tests feature is required so import the unit test and before that we need to check whether the unit test is already there or not in the package settings now project so here unit test is inbuilt in python right so we don't need to import anything so somehow it is giving some error import a unit test a spelling mistake so import unique nest and then we need to also import HTML s HTML tester and also we need selenium so from selenium from selenium we have to import import webdriver and after that if we need to use some time and everything you can import otherwise fine so you need test be required HTML report we require and selenium be required so all required packages we have imported so only those packages right your interest is in built in Python so psyllium is important HTML is report is also and selenium is also important so page object model also created so after that so he in this particular test case I am going to use this page object class right so we have to respond this page object class is present inside another package or another directive where it is present under page objects so we need to refer this one inside our test case so to prefer this one we need to create or import sis this is a system variable so environment variable we can set it here and CICIG sort of path we need to specify the location of not pertinent directory where we have created your login page dot p mine ok this dot path and append path a pen and here we need to specify the complete project path because the project path should be added to your environment variable section so the location of your project is showing Explorer it will show you here this is your complete project part and suppose if you just avoid these statements so you need to add that path to your project environment variable is not required if you specify here you don't drink wine so C : you to give you forward slashes users add mean Python unit tests watch it and form this so this is a project so what this statement will basically do is this statement will add this path to your environment variables these add this path to path variable so normally what we will do we will just right click on this PC go to properties wait so we do these act to decide go to advanced or system settings and in advanced system settings just go to environment variables here there is a path variable it is so here we are adding that path right so instead of adding our project path here what have done is I have used this command so since the dot path dot append and whatever bother have given here which will be added to your path variable in the system environment path variable it will be added but to do this we need to also import sis package so sis we happy and then only it is possible ok so this is the one we need to do import sis and they mean to specify the location of your sister dot path dot append is a command a pen and specify the location Python unit test project underscore palm base this is my project so these two steps are manually required because we are going to run this project through command prompt so that's the reason we need to add this path to path available if you want to run project directly here we don't require this but if you want to run through command prompt because why we need to run through command one because we should get the reports right so we need to add this project path to an environment medium and after that we need to import this login page from page objects that is also we have to do because we are going to use this page object classes a page object class inside this actual test case so now from page author sequence it will automatically show you from page objects dot login page is a class from that again we have to import login page so here what is this page objects is my directory or package inside this login page is my file so inside this login page file we have a class called login page so that login page be specified so then only I can call all these methods ok so with this we have done all the input it starts now I will directly write it test case so first of all I'll create a new class here + and say it login test and always in unit test class name should contains a test keyword okay and we need to extend this from unit test so you need test dot test case unit test dot test case so we need to give carefully class login test class login test then the unit test dot test case so inside this class I can just specify a few variables so what are the variables commonly be required base URL username password and these things we require right so I will just put them inside the class here so base URL it is fun this is a user named is a password and then I created the driver object so where is the executable path wadies my chrome driver is present now inside the drivers folder chrome driver is present right so what I can do is dot slash slash representing project directory we can just remove all this thing's still here and inside the drivers folder my chrome driver is present so you can just specify the executable path like this so these are the common things we require and after that I will create setup class def setup same thing whatever I have done in the previous example but only thing is here we are using page object class so we have to import web browser you already put it here ok fine so now def setup class def setup class we have used and what is this method this method is a predefined method which is in uniqueness so we need to specify this decorator for this class method okay and inside this class what are the steps I can include I can just open the URL and maximize this window because already specify the location and everything so in this particular setup a class will be executed this method will execute only one time before actual test method is started right so here I will just specify CLS because this variable is CLS is a parameter so CLS dot driver dot get here we need to space for the URL of application so URL a are you storing base URL so here I am referring this you are it will give you wrong why because this variables are created at the class level so we are using this variable here so that's the reason we need to specify the CLS CLS is representing the class okay so CLS is representing the class so base make URL is a class level variable so that we need to specify CLS dot base URL and after that CLS dot again I want to maximize the page so I'll say driver dot maximized window so I have just created two different methods after that now I will write actual tests so def test underscore login so this is the actual test okay so here I already have the page object class right so in the page object class what we have these methods set username set password login flower but before calling all this method we have to create an object for login page and through the adoption we can call all these methods so let us create an object for login page because we already imported that class so inside this test login I will create an object LP is equal to login page login page okay so this login page is having one constructor right so this constructor is expecting some tribal parameter so from here we need to pass a timer self or dot driver self dot driver we need to pass because in the login page having constructed so this constructor will be in vogue now LP equal to log in paste cell foot or driver so this will initiate your third driver after that using this LP object we can access all the methods which are define inside the page object lock so what are those methods set username set password click login click logon so these are the only methods so L P dot so what's the first method set username and here I will pass a user name so user name will be already created so here I will refer so what is a user name self or God user name I passed so this will set username method I have called by passing user name then immediately user name method will be called get the username here identify this element perform the action so similarly I will call another method L P dot send password cells to dot possum and what is the next methods tell P dot click login it doesn't require any parameters and after that I will wait for some times because it will take some time to open the dashboard so time dot I will you sleep for five seconds so when I use time here we need to also import time package here so import time okay so this test is done see now how much simpler it is now we need to verify the validation how to verify if if self dot driver dot title title is equal to what is equal to we can also directly write as session instead of writing conditional command I can render session camera how to write the assess in command so in the previous example we have read we have written the accession come and write same kind of SS in command we can right click here self dot asset equal so what is the title of the page I am expecting is - this is the title I'm expecting dashboard this is one so no commerce administration so this is the page I am expecting and this is our expected pace self or driver door title will give you an actual pitch now both will become then it will say web page title as not matching if both are not much and this command will make our test method fail we both are not saying okay so my test is done so Seany see this of my test is very very simple now because we already identified elements and action methods also created in the page object class no matters case will be very very simple and finally I will create another method to close the browser so asset teardown so teardown self ok this is not here don't clear down class because it should execute only once so this is the class level method and here also see here is driver dot close that's it so this is the overall test case okay now this test case using page object class now I need to also generate the recourse rate so here to generate the report coming out from this one and the condition is if underscore underscore name underscore underscore is equal to in the single quotation underscore underscore main underscore underscore this is the command in sir this we need to write unit tests with dot means so if I run like this it will not generate a report thanks good but will not generate any form so we need to pass a parameter what is the parameter test runner is equal to html4 dot HTML test run or again and here we need to specify the location output is equal to where I want to store my reports so we already created one reports folder here so inside the report folder I want to have the repo so here dot dot slash has a presenting project directory inside this I have a report folder so I'll say reports here reports all right so that's so this command will generate the report but when this will generate the report only if I run through command prompt then only it will Chan okay so if I Brantly right here I click here it will not generate a meaning so my test case is done so we have drivers keep it here page objects will be here and report will be generated at the runtime and the test cases will be part of here and page object is created so all other test cases also we can do like this we can create page object classes here we can K test cases here or reports will be generated inside this all right so let's execute this to come and run my login page is done login test is also done so you just execute through command so go to terminal here or you can also run here also this is also fine okay anywhere is fine so now go to come and Brahms currently we are in the current project so how to run this python and where is the actual test cases login test so where is this login test is present inside the test cases why do we need to specify that so inside that SPSS slash slash login login test dot P 1 because this login test is present inside the test cases these test cases are present inside the project right so once you run this it will execute your test case and test case will internally use page object class right so and teardown class we haven't put this one so class make the right so this is the class fine so everything is done so let's execute this and see whether it is able to generate report or not so can't we run PyCharm itself we can still run it but it will not generate the report if you want to generate the report we must run through terminal so terminal is present inside the by jump itself ok we cannot run this option so when I run this it will execute it without any issues but you cannot see the report so that is a limitation we have in unit test okay you can still run into using this option but you cannot see the report which we still want to see the report then we have to run in the terminal which is there inside the pie charts or you can also open your command prompt in your own and go to your project directory and you can just run it okay and why you are running through command grant only because of generating the reports all right now let us run this Python test cases last login test dot P PI let's run this now it is executed and it is giving some error let's read this error first of all and then we can understand so system cannot find the file specified the system cannot find the file specified let me just clear this once you clear this buffer and then again one more time I will execute Python test cases test cases slash login test dot P mail okay now it is getting fail so what is saying is selenium form an exception by private excerpts chromedriver ah TX executable needs to be in part okay it is not picking up the driver from this drivers folder so because some issue with the path which we have specified here let's create properly webdriver dot rule executable path C : so actually we need to say sequel and users and everything but here are directly specified the drivers dot chromedriver dot exe home driver dot exe so drivers dot dot slash is representing the project inside the drivers home driver or TX is business let us try to give the concrete path and then we will check one more time just giving the complete path wherever so this is a chrome driver dot exe so I will just given the complete path micro driver and let's run one more time close clear buffer and then from this one more time okay now it is started so the problem location of the driver so nobody is getting started so open the page entering user name entering the password login into the page and then it is also clicking on logout yes finally page is closed so now it is generated HTML reports so now let us see the reports so we have to see the report on the reports folder here you can see the reports just open just open this with browsers open browser so now we have only one test case test and whispered log in this is Comcast but if you have more number of test cases the report will be very very attractive alright so this is how we need to automate the test cases using page object model approach along with the unit test framework so in this example I have used selenium with Python unit test framework page object model approach and generator HT
Info
Channel: SDET- Automation Techie
Views: 32,552
Rating: 4.8834953 out of 5
Keywords: Python, Selenium with Python, Selenium webdriver, Automation, Testing tools, Selenium tutorials, Python tutorials, Manual testing, SDET, Java, selenium with java, big data, hadoop, web services, postman, soapui, rest assured, ETL testing, SQL, gcreddy, Software Testing material, selenium videos, python videos, automation vidios, jira, jmeter, performance testing, functional testing, QA, QC, Agile testing, SDLC, Unittest, Page Object Model
Id: 0kLoVGLTISg
Channel Id: undefined
Length: 71min 33sec (4293 seconds)
Published: Wed Mar 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.