Object repository in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone this is mukesh from learn automation comm welcome back to another video in this video will mainly discuss what is property file in selenium what is the usage of it and what is exactly this property file so before moving to the actual code let us talk about what exactly you will get after this video so you might have heard the different terminologies what is object proposed to in selenium what is property Philo configuration file so ultimately you will get to know all are same we need to use one property file in selenium which will behave like an object repository or you can use as a configuration file or in Java technology you will see it's a simple property file then we will talk about what is the use of it why we are using this file in selenium and do we need this then we'll talk about how to read this property file in Java and we'll use the same in selenium so you will notice will write a program so we'll create a small library so that we can reuse the code in every script and finally we'll use this property file in selenium screen so what exactly is this property file it is a plain text file which contains a data in a form of key and value pair so this key and value pair will be let us say I have specific ID so let us say if you say ID equal to 2 then you will get Mukesh ID equal to 5 then you will get some other name let us say selenium IDE equal to 8 it's equal to Akash so we have this key and value pair so we will use this key and value pair combination in selenium you will get to know in just few minutes what exactly I mean by key and value pair so same thing if you specify the key it will return you the value if you remember college stem every student having some roll numbers so if you call roll number 50 then when a will be give the present so you can use this as a configuration file now what is this configuration file regression file you can assume we have some settings file where you have some specific data and that data you want to use across your scripts so we get the complete detail how we can use this configuration file what I mean configuration in selenium and you can use this as an object repository as well so if you are not using page object model and if you are using the simple approach then you have to use this as object repository so the duplication of code will be less okay so let's assume it Oh what is the use of it so the usage will be let us say I have three scripts and every secret I'm using chromedriver so if you are working with selenium you know we have to download one driver and we need to specify the path of the driver so let us assume I have three script all these three scripts are using chrome driver so if I don't use any property file so what will happen if I change the path from one location to another location then I have to modify individual script I have to go to each script and modify the chrome path but that is again not a good way to do this we need a center location where we can keep all the configuration details like chrome path I draw a way I driver path excel file path and so on so what we can do we can keep everything in a separate property file and we'll keep all the details you can keep URL s well I generally use URL and I will tell you how I use this URL in property file so our approach will be will not hard code the chrome driver path in individual script will keep the path in property file and will read from the file so in case in future if any changes comes I won't update my test cases I will simply update the property file so don't confuse with property file it's just a naming convention if I using with as a configuration files it will be configuration file and if you are using as a object repository like you are storing locators then it will be object repository and in interviews as well you will get different technologies like have you ever used configuration file in selenium then don't get confused this is the one line answer yes property file we have used as a configuration file and still if you are not using page object model you have to use this object repository so let me show you what I mean so let's create a folder first right click on your project I already have one project I have the selenium library added so I have only one jar file selenium server standalone so I will create a folder called configuration click on finish you will get this folder now I will create the property file so I will right click on this click on file and I will say config dot property yeah so make sure the extension is property so when you click on this you will see a plain text file now here we will specify the key and value pair so let us say I have to specify the chrome path I will say chrome driver and I will specify some location let's say I want to specify ie driver as well and URL yes so let's talk about this URL part if you have worked with any application you might have noticed in every release you will get the URL different so now let's assume the same scenario if I have created 50 script for my old application and now suddenly URL has been changed so it is not a good way to modify individual script right you have to modify all your script go and buy one and update so better keep the URL in the configuration file so in every release even if URL is getting change you don't have to update your test simply go to this property file update here and it will be reflected in individual test cases so this is one of the way to make your test robust and maintenance will be very easy if you follow the same approach so you can keep multiple here like where excel file is located and so on so in this example I will keep this URL so let us say I want to specify my blog URL chrome path let me check where I have kept all the chrome driver and I driver so if I go to my C Drive somewhere I have the selenium drivers yes no I just moved yeah so this is the chrome driver path what I will do I will right-click I will press shift button right click you will get this option called copy s path you can see this option now if you go to your Eclipse simply C control V I will remove this double quotes and I will keep this one more slash if I want to use I driver I will go back I will get this AI path again I will press shift button right click on the side driver server copy s path yes okay you can give one and recommend as well so it will look clean right now I am NOT giving this Excel path so if you are using Excel you can update the path over here so this is the configuration file so let us close this now let us create a new test case so this does K let us say let me create a package as well so package I will give test config let me give package name is configuration demo class name I will Q test selenium block I will click on finish okay so before writing this actual test let me write a small program how to read this file then I will segregate this code and I will keep this in a separate library so let us create a method called test config and I will put at the red test symbol it will ask me to import either from j unit or test ng i am a big fan of testing G so let us take the test in G so now just a three lines of code to read this file I can join all these three line into one line as well but let us use the standard way first first you need to specify where your file is located so you need to specify specify the file path so when it says file it will ask you to import the respective package so don't get confused take file from Java dot IO now location so if I need to specify the location I have two ways I can go to this property file right click on this and if I go to property I can see it's giving me very lengthy location right but I know it is located under my workspace so I will put one dot operator so this dot operator rivers like the file is located under my workspace only now I have to only specify the folder name so I will say configuration then I will specify the file path with extension some say config dot property file sorry yeah property so now this will specify where the path is located now I will use file input stream F is equal to new file input stream and I will specify this source so I want to read this file so I have to specify the source now again it will ask you to import the respective package so put mouse over and it is asking me please you need to file input stream ok I just forgot this e over here now put mouse over and import now if you notice if you put mouse over it is throwing one exception called file not found exception so now you have two options either you strike at your throw so let's use throw as a flow and you can see all the options so I will use the base exception called exception class now we are almost done so it will specify the path this will convert into an input stream now in Java we have a property class to read the property file so I will simply create object of property file so I will say property and it will ask me to import the package so put mouse over and yeah so now you see I did I did a small mistake the class name is not property class limits properties so use this third option and this is coming from Java dot util by mistake if you take this one this won't work so change it to properties and take this third option which is coming from Java dot util and same changes I will make here it is a properties file now we are done so last thing you need to load the file just keep a minute I need to put my phone on silent so now when you create object of this property file that is pro so you need to Co call one method call load and when you say load you can see one option you can specify either reader file either file input stream we have used file input stream that is nothing but our FIS so this four lengths will load your property file and now can use it so let us take one example I want to get this chromedriver path from this property file what I will do I will say Pro dot cap property so now this property class have one method called get property what it does it will accept the key from you and it will return you the value and you can see the description as well it's search for the property file with the specific key which you will mention if the key is not found then it will return null okay but if it matches with some data it will return you the exact data so I need this chrome driver path so this is the key so left hand side will be the key right hand side value will be the value so I wants chrome driver and here we go so when you put mouse over you know it will return you as a string the data as a string so I will keep here I will say chrome path and as of now just for the testing purpose I will say print a chrome path is does equal to then I will say chrome path so now if you simply run this run as test in G test okay so now if you see here it is getting the exact location G Drive selenium folder chrome folder and chromedriver tot exe so you are able to read this property file now we need to use this in our script as well so this is just a dummy code the actual code will use now it will simply create a new script selenium in Chrome so let us say I have a separate script in this script I want to use this property file so this will be my script I will give any dummy name as of now test CRO and I will keep this destined G you let me write the code tri or equal to new Firefox driver so you can see I am getting these options I'm because I am using one command called control space so if I put driver dot get let me open my blog then and let me use this chrome chrome driver to use this Chrome we need chromedriver path if you have not visited my previous tutorial in which i have discussed how to start chromedriver why we need driver then please do visit the previous tutorial how to execute test cases in chrome so if you know we have one method called set property where we need to specify the property so the key and value webdriver or chrome road driver and here you need to specify the driver path if I want I can keep the static value here but let us take it from the property file so now I had two ways first way either I can complete copy paste this code from here so you can see it will throw your exception so let us say throws so this is one way like I will read the property file in the test case and I will pass the chrome path but it's not a good way you can see now if I have to use this five lines of code in every script my script now you can see the code duplication so we'll create a reusable method for this don't worry as of now let us run it just to check whether we are able to launch the Crone yes chrome started and you will notice here it started remaining stuff will be same like it will simply start my blog now why can't we create a library for this so that I will directly call them so let us go ahead create a new package call utility in this package I will create another class and I will say this file is config reader yep so here I will create a constructor so now why we need constructor so the constructor main purpose to initialize the data right initialize some values so I want to initialize this in the constructor I do not want to keep this code in every method so I will keep at least these four lines of code in the constructor so this constructor will simply load the file it will create object of file input stream and it will return me the object now you can see it's throwing some exception so now I don't use throws because if I use throw here again in the test cases I have to handle it so I will right-click surround with trycatch so we will notice it is giving me to catch block because one is coming from property file and one is coming from file input stream so I will remove one catch and this I will keep as a base exception and in case anything goes wrong I will directly print the exception whatever exception will be coming so exception is and whatever exception will come I will directly say e dot get message now you can see this property file I have loaded successfully but I need this pro object outside this constructor so I need to make this as a global variable so I will make this as a global variable and I will remove from here so when I load this property file so this Pro object will be able to read any data so I will call this get property method outside this constructor so now let me create a small method called get chrome path what this chrome path will do it will simply get the chrome path from the property file and it will return as well so let's make the return type as a string so I will say Pro dot get property and the key I will specify chrome driver I will get the path here and I won't print because printing will not make any sense for me now so I will directly say returned path from here so I have one utility this utility package heaven folder class called config reader and I here I have an constructor called configurator and I have one method called get Chrome path which will simply get the data from property file and it will return me that is the reason I have written the return type of string so now if you want to use it what you can do just remove all this stuff from here now so if you want to use I will directly first of all I will call this configurator and I will creat object of it so reader or let's say conflict directly configurative this is the class that we have created and i will use this conflict dog get chrome path that's all so you can see it's very handy to use right now I don't have to write all the code whenever I need any configuration details I will call this class I will create object of it and I will call the respective method which I want to use so as of now I have only one method right now but I can create multiple because I know your URL also I want to take so let's take URL as well in this file I will create one more method called public return type again string and again able to get application URL and let's do this directly I will say Pro dot get property say again you can keep in these two lines but again one unnecessary code I will store in a string variable then I will pass to the standard way without using much code you can directly say Pro dot get property specify the key in our case key is URL so I have one more method now which will return me get application application URL so now if I go to my script I will not use this anymore config dot ket application URL so you can see now our script is very abstract so if you know abstraction means showing the essential features and hiding the background details I do not want to show there Mac chrome driver is located so now you can see now no one can see where is chrome driver is located what is the actual application URL so the main purpose of property file itself is the keep the configuration deals separately in a separate file so now let us run this and let me make one statement driver dot quit I know this tutorial become lengthy but it's very good concepts so let's see I will further modify this code and you will get one new thing today one new annotation which I will use right now so you can see it started Chrome it means it is able to read the property file and it is able to read the URL as well so its opening my blog now it will load completely and it will quit yeah there we go so our task is completed so now this is all about the property file now can you can start creating methods so as you can see we have I driver Excel path so take this as an assignment take multiple methods which you want to use here like get Chrome path could get a path get actual path get application URL you can keep the URL in testan g dot xml as well it's up to you but i keep in my configuration file so the thing which i want to tell here like we have some multiplier notations in testan G so let us say if I create a method called setup so in setup I will keep all these details like loading the configuration file and all so this config file I will not keep in my actual tests or you can set this as well because this is not the part of the script this is you can say as a setup so setup part we will be doing in this setup method like loading the configuration file setting the chrome path so to do this you can use one a notation called before test what exactly it will it will execute this method before executing this test so if you put mouse over you can see it's coming from testing G so let us use it so this is actual test this is your configuration and if you want to give any command like setup is ready you can use this out for your test execution as well test started test closed and once why I am writing all this just to show you before executing this test it will run this so you will notice it will say setup ready then will start so let's run this and you can see it is running the setup first so it's a setup ready it means everything goes fine and now it will continue with the rest of the test case and yeah this is the new session so now you have noticed we use property file very efficiently and you must have noticed the benefit of it so if you are not using this property file in your current project then you have to use it it will save so much time of yours and it's a good practice to use a configuration file in qtp you have a separate file which is given by qtp but since selenium it's our open source so you need to create your own style so this is how we use it so once you move forward you will notice in hybrid framework as well I will use the same but in a more efficient way so that's all I have for this so let me check whether via cover everything or not so we have covered what is a work what is use of it read property file create librarian put use the so we are done now so if you still have any doubt you can drop an email just visit by blog if you want the complete code I have dated the same code in my blog thank you so much for watching the tutorial have a nice day
Info
Channel: Mukesh otwani
Views: 80,785
Rating: 4.9479556 out of 5
Keywords: Configuratio file or Object repository in Selenium Webdriver, Selenium, Automation, Selenium Webdriver Tutorial, Selenium Test Automation
Id: 4UsS3vERhEE
Channel Id: undefined
Length: 29min 19sec (1759 seconds)
Published: Sat May 21 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.