Selenium Automation Framework- Create Data Provider and Custom Libraries

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey having this is some acacia or 21 second Foreman - an omission calm this is part four of the framework series and today in this video we are going to talk about how we can create data provider and libraries for our framework okay so let's quickly see what exactly we are going to cover in this video in this video we'll talk about how to create accelerator provider how to create configurator provider how to create custom libraries in our framework and how we can enhance our base class for more functionality now if you have gone through my previous videos then in our previous video we have created this script right and we also created this base class which has very very basic functionality which simply start the browser close the browser so today we are going to enhance this and we are going to create a couple of libraries as well now I do not get confused with the data provider which I have written here with the data provider which tests ng provide the stingy data provider it's a separate class altogether which will help you to create data-driven framework I am just given a name data provider because these library will provide the data by reading certain external configuration or some sort of database okay so this database or sorry this data provider name I have given because it fulfilled its purpose so it will simply eat the data but you can also give us the give this name as accelerating file config reading file that's all okay so do not get confused with a data provider class which comes along with testing J so let me quickly show you the meaning of this plus why we need this now if you talk about our base class we are taking this browser and this URL directly hard-coded type now if you want to take this kind of configuration from configuration file then we have to take help of properties class in Java you do not have configuration file okay in Java so we do the same thing where properties class property class itself will solve our purpose because properties class also works in the key value mapping pair so what will do will tree two key value pairs okay one key will be for browser and one key will be for the URL now you can also have multi URLs okay one can be for staging one fair before production one friend one for the UAT you can have multiple key value pair for this example for this framework I will be taking two key value pairs and if you go back to our test case this is our data which we again hard-coded okay and as we know the nature of test data it keeps on changing right for different environment for different sprint for different user it will change so we should ideally keep this also in a separate file so let's quickly create our file then we'll write the code so this is the conflict folder which we created in our second video so what I am going to do I am going to create a new file called config dot properties case this is the extension you cannot change dot property should be your extension now we already have discussed this how to read the configuration file how to read the coxal file so we already discussed this in our previous video so if you have not gone through with that videos I will put the link in the description you can go ahead and watch that video first and then you can come to this framework anyway I will show you or I will repeat the steps again but if you want in-depth tutorials then we have a separate video okay so for this I will keep browser and browser as of now I will say Chrome and let me give this name as staging or test URL or QA environment or QA URL now you can also have you Ettie okay you at a URL or fraud URL it can be anything depends on how many landscape you have how many environments you have right so it will keep on changing for the time being let me stick to three URLs so I will go back to again test case and we'll take browser we have taken and let me take this QA URL okay for the time being I will just keep them blank because anyways we are not reading or we can remove them similar way we can go to the test data and here actually we have to create one excel sheet which will actually help us to pass the data to a framework so I am going to create a new worksheet and this I will give as data or you can do test data here you can say we have multiple sheet so I will give this as a login and here I will keep the login so log information I will pass here login username is selenium underscore 50 and password will be ABC D underscore 1 2 3 4 5 6 ok and I'm gonna save this excel and as of now let's read the username password and later on we will also do a couple of more details once we log into the application will create contacts account then we'll read some more data so now we are done we have created our environment which is nothing but the property's friend which is nothing but configuration file in a similar way we also have created our test data where we have just username and password now let's create library for them so in the utility in the last class we already discussed how to create a browser factory which is simply dealing with the browser spot now let's create an Excel data provider as the name says it will try to read the data from Excel and will provide us the data so I am going to create one constructor here public Excel data provider now what exactly this constructor will do it will simply load the XL as soon as we create object of this class it will simply load the XL so we don't have to pass or we don't have to provide the Excel path again and again now before we start let's quickly check do we have dependency for a patch API because in order to work with Excel we need third-party library called Apache py so as of now we have selenium we have Destin G and we also have extent report so we need Apache peer dependences well so I will go ahead and download Apache apoE dependencies or I will simply copy paste in my bounded XML so search for Apache py and open first two links which is py and py oo XML only these two dependencies are enough you don't have to go through all the dependencies these first two dependencies are enough now important note here now if you are going to the version 4 then they have deprecated couple of libraries so few libraries will not work but they have added new ok same goes with this as well so in order to stick with the old code I will go ahead and use 3.17 because you can see it is a highly stable ok so I will go ahead 3.17 copy this go back to our coordinate XML and paste here similarly I will go ahead and download 3 dots 1 7 for this as well and that's all if you don't need this comment you can remove this comes by default when we do this copy paste from mbn deposited comm and you can also press control shift F if you want to format your XML so you can see it looks clean now so now we can start using Apache POS well now if you gone through the previous video then it will be very easy because same steps we are going to repeat here but in a more efficient way so what we do first step first step we always do finally write so file SRC equal to new file and we provide the path now path it is already in my home directory so I am going to put dot slash now we have to provide the folder name which is nothing but test data as of now and Excel name is data dot XLS X then second step we need file input stream where it will convert this file into a raw data and we will provide the source as well now it is throwing some exception so we will take care of this in order to read XLS X we need one class called XS SF workbook so we are going to create object of that particular class and will provide the file input a screen object now this WB object we need across this class outside this method so what we have went to do we are going to make this variable as global variables so that we can access outside this method as well now these two classes file input stream and X s SF workbook is throwing some checked exception ok so you can see this is throwing file not found exception so we have to handle it similar way these XS SF workbook also throwing IO exception so to avoid this I am going to surround these two classes with try catch and you can see these two exception came so I'm going to have only one exception block which will take care of both the exception so I am going to use x exception based class now again if you're completely new to what is exception how to handle the exception I have dedicated to videos on exception handling which I have already uploaded on my YouTube channel so you can watch them as well now in the catch block we will give a very meaningful message that unable to read excel file and they can print the exception log as well so I can say e dot get message so this constructor is done so this constructor will get called automatically as soon as you you create the object so that is our main intention that we don't have to load the file every time as soon as we create object this constructor will be called and it will load my file now we need the actual methods which will help us to read the data so let's create few methods one method which I will create called get the string data okay one method one more method I will create called public void get numeric data for the ramming only two are enough once enhance of a framework will also add couple of more libraries okay so these two methods are enough now we are going to use this WB object this WB object is workbook object now in order to read the data first we have to navigate to sheet right with sheet if we want to read then we can also use get row which row we want let's say as of now 0 which column we want so we'll say get cell as of now let's say 0 and get a string cell value so this will return your data in a form of the string so we can directly return the data from here which can be used in our test case so this method ok this particular statement will return you the data that data will return so we have it return keyword and return type we have changed to string because we are returning string data now what parameters we want we want sheet name which sheet we want to read so in our case we are going to read login sheet so this cost sheet name so chic name to 3 we are going to read sheet number sorry sheet name login so we'll pass this at runtime ok directly from library or from the test case then which row and which column so this is actually will keep on changing based on your data it will read the respect to row and column and it will return you the string cell value let's quickly override them so whatever sheet name you will pass it will come here whatever row name you have passed it will come here whatever column name you will pass it will come here one library is done which is getting data same thing we can do for numeric data as well okay so this is sheet name row column and this is exactly what we have to do the only thing is we don't have to say get string cell value we will say get numeric cell value now what exactly this numeric cell value will return this will return your double so you cannot store or return as a void it should be double okay in case if you want an teaser you can typecast into integer and then you can send okay now right now what we are doing we are passing the sheik name right now if you don't want to pass a sheet name but you want to brought the sheet index then we also have a method called get sheet at okay so if you just go to Apache py we already have a method called that sheet act where you if you pass the sheet index it will check the sheet index 0 1 2 3 and based on the indexing it will read now if you notice this thing this is called method overloading where method name is same number of parameter is same but type of the parameter has changed integer integer integer string integer integer so you can see this is called method overloading and the similar thing we are going to do for multiple libraries so you are finnaly going to get this kind of questions that can you tell me where you have used method overloading in your framework so this is one example similar way will increase many methods where you will see we are doing method overloading as well so you in our previous video we discuss how we did abstraction here we are discussing how we did method overloading in a similar way you can see how we use constructor in our framework in future videos you will see some more concepts that you can expect in interviews as well now let's quickly see whether this part is working or not now I have on bonus point for you let's see this first and then we'll go to convict part and I will tell you what exactly is missing in this framework okay that is optional but it is good to have so I will go back to my test case now this is a separate class right now if I want to use this method okay I have to create object of this class definitely so I can create either in the base class or in the test case so let me create now in this test case then will tell you what is the best practice to follow so as of now I will create object of excel data provider as soon as you create object excel sheet will be loaded at your test data will be ready and then I will say excel dot get string data sheet name as I said login first row first column now this particular method is going to return me the username so what I will do I will simply paste here okay and this method which is same row but different column I am going to pass here okay you can still do the same thing in your base class and you can access that excel object because you're extending the base class so let's see one by one now if I run the XML file it should run and it should log into my application because functionality-wise we are not changing anything it is just were changing approach now it is not hard-coded it is coming from Excel okay and it worked once the login is done successfully it will close the browser and execute it okay now if you want to keep this in base class that is also acceptable you can also create another setup suite okay this is another method which I am creating where you can have all kind of object creation which is required for your complete project okay so let's say I'm going to create this here and I want to use it here sorry in okay now it will ask you to change the visibility that's fine because here it is default let me make it public okay so when you want to run this now you can keep this in before sweet as soon as your test suite will start this will setup your accelerator provider and in your test case you can directly say excel dot a string cell data excel dot string numeric data or any other method which you have created so axle part is done when more enhancement is required that we will do let me quickly also show you how you can read the configuration file as well okay which is browser and q a URL for this let me create one class called config data provider now reading configuration file or property file is one of the easiest tasks initial two steps will be same which we have done for excel here also I am going to use constructor because constructor will help us to initialize the variable and objects so again file SRC equal to new file ok path I can give dot then I have to provide the folder name then I have to provide file name with extension so this is done next step is file input stream ok this again we have discussed in the videos step by step so you need to go through that videos as well and same thing in order to read the properties file we have a separate class called properties class we are going to declare a global variable here and here we are going to create object new properties now we are done we are loading the configuration file so we'll say Pro dot load and we will load the configuration file now again with this will throw you file not found exception this will throw you exception so I'm going to surround these three lines of code with try-catch and I'm going to give the same message here I will keep base class here so that I don't have to handle multiple catch blocks and in the cache block I will simply say not able to load config file and then we will print the log message now we don't have much methods here so I am going to create one method public return type will be string because this method will return the data and let me create one custom or one method which simply takes key and returns the value so I will say get data from config and one method which will return me get browser one method will return me get staging URL or test URL now here we already know that we are trying to refer browser so key is this and value should be this right so we will say pro pro is nothing but the object that we created for properties class and dot get property and since the return type is a string we are going to add a keyword called return same thing we are going to do for URL as well I will say Pro dot get property and what is the key here key is QA you are now if you have more custom key value pairs you can create methods like this as of now we have two qle pairs so I am going to create two methods and one more method which will generate take the key key to search and it will return the same so we'll say Pro dot get property and whatever key you will pass it will return me exactly like this okay if you want to test you can just give any name let's say first name I'm going to give my name is Mukesh so we can test all these three methods now this configuration file then config data provided also done so in the base class if you wish again you can just say see here we just need to pass rate key value pairs so again I can say here public config data provider config confetti cole - new conflict data provider so you're as soon as a kid object this will call the constructor sorry it will call this constructor your conflict file will get loaded and now you can use this now we have to pass here right this all these invites configurations were hard-coded so this time I will say conflict dot get browser and this time I will say conflict dot that's telling URL now your configuration file is separate excel file is separate your test cases or your base class is not hard-coded anymore everything is now separated okay now if you want to test because again test case wise we have not changed anything just we have changed the approach but still if you want to check just right click run as and if you want to test it I will recommend changed the browser part so that you will get to know let me change this to Firefox and if you run the test gate test case it should run on the Firefox ok now you can say something went wrong okay that's good what exactly it is searching for it is searching for gecko driver it means we the path which we have given okay you can see here we missed this part same thing I will do for I useful now let us run it again and you can see just we have changed the configuration file and it is getting affected and as usual the test will run and it will complete right so that is the power of config data provider and accelerator provider now if you don't want to create object again and again okay like it is anyways once but still if you wish that at runtime you should be able to call or directly you are able to call these methods like data provider factory dot get browser something like that again we can create one more class okay but if you're ok with this concept you can continue because ultimately our goal is to get the data which is done now let us quickly see how to create custom library ok now when it comes to custom library libraries it is up to you how you want to enhance what you want to enhance but there are some very basic or mandatory ok or must-have libraries which you should add enough framework without that it is like very difficult for you ok to achieve the maximum goal so I will list few okay as of now I will list them but in terms of coding part let's see how many we can cover today so already our package name is utility so I will keep this class name as helper class ok names you can keep whatever you like so I will list down the [Music] library name so one we need capture screenshot okay one we need to handle frames when we need to handle alerts okay maybe we can go one by one but I can keep a note here to handle the screenshots alerts frames multiple windows a normal method which will sync handle your sync issues and we can also create some custom libraries for Java Script executors okay and so on I will keep on adding for the time being let's see how to capture the screenshots and this is what exactly we are going to do we are enhancing our library one more step where it will capture the screenshot at runtime sorry if you once your test is done so in order to capture the screenshot we are going to use webdriver reference because this is what exactly will help us to capture screenshot now again if you are new in selenium and if you are not aware how to capture the screenshot then again I have separate video on this so what we are going to do we are going to do we have captured the screenshots so we have a separate interface called takes a screenshot which has one method called get a screenshot s so we have captured the screenshot as a file which will return as the object as file type okay we will import this from java dot io now we have to store this file or screenshot to a location right so for this we are going to use one class called file handler where we have one method called copy which will ask you from and two so from anyways we have SRC right because this is what our screenshot and we want to copy to our screenshots folder so what we'll do I will refactor copy this name and this has to be in file format so I put dot operator this folder name and the screenshot name let me put log in dot PNG as of now now again when you use this this will throw you one check the exception called IO exception so I am going to handle it with trycatch and in case any exception comes I am gonna print one custom message first that unable to capture screenshot and then we'll print the custom sorry the message which is coming from a object called a dot get message now one is small suggestion if you wish you can keep here all the method as static so that you can directly call them if you don't want to keep static again you can create object of help or a class in the base class and they can you and you can use that object to call these methods as of now I will keep this as static so that whenever you need you can capture the screenshot so in order to show you this I will go back to the test case and I will capture the screenshot before closing the browser so what I will do I will say helper dot capture screenshot now consider the scenario let's say if your test is failing here definitely it is not going to run this if your test case is done it will go and definitely going to capture the screenshot but it fails it will not execute this line and this will not work at all but my requirement is I need the screenshot either on failure or success or in both the situation in that case can go two ways glass and here you can again create one more method called public void teardown method and this you can call after method guys now you can see the importance of different an addition x' we have used before sweet before class after class we are again going to use after sweet as well but after method is very helpful when you want to run certain piece of code after every test case if we have 50 test cases and you want to run certain activity 50 times keep inside the after method this is what our task is as soon as our execution is done whether its pass or fail it should capture compare if failed capture the screenshot if passed then whatever you want to take the call you understand email you want to capture screenshot you want to generate report it is up to your requirement in this scenario I will take the screenshot on failure okay so inside this we are going to use one interface called a test result what exactly this interface will have as soon as your test case will complete this result variable will have all the information so what we exactly are going to do we'll check if result dot get status equal to equal to I test result dot failure okay runtime we are capturing the status if the status is equal to failure then capture the screenshot okay and if you want to check what you can do once the successful screen shot is done successfully you can print assess out message this is again optional guys it is just for you for the time being that it is capturing the screen shot well right screenshot captured now this is called enhancement in a framework as soon as dust is done if fails capture the screenshot now this part how it is coming I already have a detailed video on this as well which again I will put in the description how to capture screenshot on failure what exactly is this test result how this after method works now purposefully I am NOT going to fail my test okay first I will go back change this to Chrome the children fine [Music] yes it's happening properly and it is going to logout the browser and finally it is going to close it this is what we want now let me purposefully fail this so that we can see it should capture the screenshot or not so in order to fill the rest case what I will do I will just remove this six from here and do the ctrl s ok or maybe I will change this as well both I have changed now let me close this excel and let me run the same test this time I should capture the screenshot and it should store into this screenshot folder yes you can see okay now fight pass successfully because what we did we just entered the username password and we do not have any verification that successfully logged in or not so in this case what happened it actually did the steps whatever data we entered in it ran successfully we need to add validation whether login is done or not so no problem what we will do will purposefully going to change the locator so that it should fail this time let me change this user name to username one and let's sit on the same test [Music] okay this time you can see it is not entering the username right and now it is not failing because we had given implicit wait it is waiting for some time period okay if we go back to start application we have an implicit wait of 30 seconds so it is waiting till 30 second within this 30 second if it comes it will move to the next step otherwise after 30 second our test is going to fail so let's wait and it fail and you can see screenshot captured it failed the test and exception says no such element exception that's great and let's refresh this and we got one screenshot see beautiful so this is how we enhance our base class will keep on enhancing okay once we move forward we'll have a couple of more things to add but now you can see our framework looks little professional we have certain pages some test cases and few utilities as of now only one method which we have used will enhance it further now if you come back to this particular helper library you will notice one thing that we have given a static name here right now next time when my test fail let's say next time another test case is failing but it will stay here the same name called login dot PNG and it will keep on overriding my previous screenshots right but we don't want this we want to maintain the screenshots in case any analysis we need to that why our script is failing we need this a screenshot and we need our old reports as well so for this we are going to create a new library which will give us the date time stem in a proper format now if we use the current method which comes like get milliseconds it will give you the time in milliseconds which you cannot read okay you can read but again you need some calculation so what we are going to do we are going to create a method called public since we are going to return the time so I will put the return type as a string and I will say get current date time so what exactly this will do this will going to return us the date time in a standard format now if you just search for a simple date format okay we have a separate class altogether which is simple date format where you can see how many date formats we have okay so just go through this inbuilt class in Java which is very interesting and these are the patterns which you can use so I have already detailed post here so I generally take this format but if you want to change the date format you can change so let's create one object of date format this is the parent class and let's create object of simple date format and the pattern which I will give is this now sometimes this /n space not supported here so I am going to replace them with underscore so this will be the format month date year as minutes and seconds important note this is capital M this is capital H H okay now this will be the format but how to capture the rate to capture the date again we have a date class and make sure you are taking the date from Java dot util not from Java dot SQL so let's take the date once you create object okay your date will be ready or you can say current date okay now we got the date we got the format now how to format them now let's call format dot format better we give some name custom format custom format dot format is a method and we are going to format the current date so once you format definitely it should return you the date so that directly will return from here so this method will return you current date time step in this format which we are going to use here okay so let's put double quotes here plus plus we are going to call this method since we are on the same class I can directly call this and I will say plus and I will add dot PNG okay okay it says it's not static yes we forgot to add aesthetic so if it is static method directly you can add here so what it will do it will take the current date time stamp and then a progress dot PNG but it still if you want to give some name so you can give you a project name for the time being my project name is free CRM so this will be the standard for all the screenshots okay this will keep on changing now if you want to check this as well just run the same test again it is going to fail and this time screenshot will come in a proper format what is going to take again 30 seconds because we have given implicit wait of 30 seconds in the meanwhile we can also discuss now one important point which I want to mention that we are creating libraries we have created a browser Factory we created this this now and directly we are integrating with our framework right but if you ask me the standard approach either it's a development or testing you need to test your library cessful which is called unit testing so we are still missing the unit testing of our framework this is what we are doing we are just writing the code integrating and let's say after integration is something is failing you cannot identify you can identify but you need to struggle a lot you need to trace back where exactly to fail but if you do the unit testing of browser factory then you will be confident enough that yes browser factory is working fine independently it means this does not have an issue conflict data providers working fine this does not have an issue so at least you will be clear with the components which are working fine only with the new modules you need to verify so in the next video we'll see how to create unit test cases for the library so in future something went wrong then how you can debug easily ok so that unit testing is missing for our framework but again best practice is we should have some unit test cases but if you don't have it's okay but ideally should have okay so in the next video we'll see how to create unit test cases for our framework so that it will be easy for us in case any changes or in case of a framework fails now you can see it failed our test if i refresh the project I got this so you can see the date it is first month okay we just Jan 23rd 2019 this is time 8:00 p.m. 44 minutes and 29 seconds again you can see when space is missing that we can fix it it is just how you want to form it so I will put one underscore okay that's up guys for this video in the next video we'll create some unit test cases and again will enhance this framework so we'll create more libraries build test suite and many more okay so that's all I have for this video if you liked this video guys please share with your friends subscribe to my channel because we are going to upload many videos on the test automation in 2019 ok and if you still have any doubt you can ask in the comment section you can also send me as email my email id smoke a short one here at learn - attribution not calm this all guys have a nice day bye bye [Music]
Info
Channel: Mukesh otwani
Views: 26,901
Rating: undefined out of 5
Keywords: Create Data Provider and Custom Libraries for Selenium Automation Framework, Hybrid Framework, Framework, Selenium Framework, Test Automation, selenium framework using java, selenium framework from scratch, selenium webdriver, selenium (software), software framework (software genre)
Id: RvwMu7tG6TA
Channel Id: undefined
Length: 46min 15sec (2775 seconds)
Published: Wed Jan 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.