Selenium Framework for Beginners 17 | What is Properties File | How to use

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session on properties file I'm Yakov and today we are going to learn properties file from scratch so if you are a total beginner do not worry we are going to go very basic step-by-step and we will see what is a properties file how to create a properties file how to get data from properties file how to set data to a properties file and finally we will run our selenium tests using a properties file and we will also look at some useful tips so let's get started and any file with a dot properties extension is called a properties file for example config dot properties and in Java and related technologies these properties file are used to store some parameters or settings for example if you want to set your browser like Firefox you can set it the browser name you can set in your properties file and we can make our code to read this property or this parameter from the properties file and accordingly run our tests on that particular browser we will practically see how this is done let us see how to create a properties file so as we have seen any file with the dot properties extension is a properties file so in our Java project we will create a file with dot properties extension so we will create a conflict or properties file so let me go to my Eclipse and here in our project so this is a project we have been creating in the earlier session if you have watched those session it is good but also if you have not watched it you can still continue with this session because this is going to be a separate session for config file what I will do is in my SRC test in Java here because this is an even project you can also create this packages and properties file in your normal Java project I will do a right click here and I will first create a package so that my configurations are stored separately in a package and I will name this packages conflict and say finish and now I will do a right click on the conflict package I will say new and I am getting the option for file here if you do not get the option here you can go to others and then you can search for file option I will click on file and here I will say config dot properties and I will say finish so you can see our conflict or properties is created here and here is the file and here you can give any key value pairs for example I will say browser equals Firefox ok so this is the very first steps here to do is we have to create a config file and now let us see how to get data from our properties file or config dot properties file there are four basic step the very first step is we have to create an object for the class properties file so what I will do is in the same package I will create a new class I will create a class and I will call this class as properties file and I will also create a main method here and click on finish so here we have got this properties file with the main method and I will create our method public static void and I will say get properties I will name this method as get properties and inside this first I will have to create object for properties so I will say properties PR o P you can give any name to the variable equals no new properties and here if I hover over this I have to get properties from java.util so I will import this and this is now imported here and a semicolon at the end and this is the very first step so we have created a object for the class properties second step is we have to create an object for class input stream because we have to read it from a file so I will create input stream object and here I will say input stream I can use any variable name I will I am using input here equals new file input stream and if I press ctrl spacebar on my keyboard I am getting this autocomplete it I will use this file input stream and here in the inside these brackets I have to give the location for my properties file which is conflict dot properties and before that I will also import this input stream from Java dot IO and now I have to get this location so I will right click on my config dot properties file go to properties and here this is the location I will just copy this location I will copy it from here and in the class here in this particular method I will give it this location of the conflict or properties filed now of course this is an absolute location and if you take this project somewhere else this will break because it may not find the properties file at the same location so for that what I can do is I can get the location of my project by the using system dot get property and the property name is user dot dir this property gives us the location of this current project and I will store it in a variable called project path ok and now this project path I will use here so instead of all this until our project location which is selenium Java framework until here I can give this variable and then after that I can give this rest of the location so you can see this is selenium Java framework and here I will say + for concatenation and also you can change the double backslash with a single forward slash double backslash will work for on Windows but if you take it on any other operating system like Mac it will break so I'm changing all the double backslashes with a single forward slash and this is done okay so let me just also import this file input stream okay so it is already imported but we have to handle this exception either we have to add a throws declaration or surround with a try-catch so I will surround the entire code with a try-catch block I will say try here curly braces start curly braces top and this entire code will go inside the try block and then I will have a catch block here I will say catch and I am taking exception exp and just in case you do not know how to do this exception handling I have a separate session for exception handling and I will provide the link in that description you can watch that as well so I will say exp dot get message exp dot get caused and exp dot print stack trace so just in case we get any exceptions will be handled like this and I will print it out here like this and the same thing I will do for exp dot get caused sys Oh control spacebar on keyboard it will autocomplete and this system dot out dot println I am going to print it in case of any exception so this is done we are done with our step for file input stream and let me see why is this error I think we are missing ha curly braces here yes and then the next step is we have to load the properties file with this command or statement drop dot load and then whatever variable we have used so we have used input so we will say drop dot load and this is the input stream variable we have used ok so now at this point our property file is now available in this Java program and now we can read the properties file so to read we can say drop dot get property and whatever is the value or the name of the property so now I will say drop dot get property and it is Auto completing and the name is so here we have given browser I will say read the property browser and also we can store it in any string variable I will say string browser equals get it from the properties file and I can also print it out on the console so I will say browser here so this is the code to read the properties file let us try to run this and check it so in the main method I will call this function get properties and now I will do a right click run as java application and here you can see it is now printing firefox now we can also write to a properties file so let us see how to set data to a properties file again we have very simple four steps the first step is we again have to create an object of the properties class so now I will create another function here I will say public static void set properties and here I have to use this again so what I will do is instead of writing it multiple times in different function I will make this as our class variable so I will cut it from here and give it here at the class level and this should be fine so this is having an error because we are referring it in a static function so I also have to make it as static here so this is a static variable drop and it has been used here and I can also now use it in any other function so we are actually done with the first step properties tropicals new property and then the second function is we have to create an object of the output stream class so like we have done for input stream I will say output stream and any variable name I will say output equals new file output stream so this will help us in writing our data to our properties file and again we have to use the location of our properties file so I will just copy it from our earlier function and just give it here and a semicolon at the end and that's it so here again project path is not declared here so again this I can make it I can take it at a global level so I will cut it from here and give it at the class level so that it is available to all the functions and again I have to make it as static so yes this is now done so now output stream I will import from Java dot IO so now we don't have any errors let me just see there ok file output stream again it is asking us to surround with the trycatch so i will just use this try test that i have already used I will say try here and here I will just copy the catch block again and to identity I will control hey I will select everything and press control I on my keyboard so everything is now I dented and yes so I don't have any errors now so second step is done third step is now we have to set property using the set property method so I will say drop dot set property and I can set any property for example I can even set or change the existing property I can say browser and here I can give some other value I can say it should be set to Chrome ok and then doing this will not completely end our program we also have to store the properties using the prop dot store function so at the end I have to use drop dot store and here I have to use the variable that we used as output stream which is output and comments are optional you can give some comments here or if you don't want you can just say null and that's it so this will store this browser s Chrome and I will also call this here set properties and let us try to run this now I will do a right-click I can also rent it from here I will just turn it from here so you can see it first read Firefox but if you go now in your configured properties you can see now it is changed to browser equals Chrome so chrome is now written here and again if you let me just say Firefox here again and what I'm going to do is I am going to call the get properties again the third time okay so what it will do is it will first run get properties which will read the property browser and should print Firefox which because as of now it is Firefox and then set property method should change the property browser to Chrome and then when we call get properties again it should read chrome and print chrome so let us try to run this again and you can see it is first reading Firefox then after set property has changed it it is now reading again and now it has changed to chrome ok so this is how you can do this now let us go how can we use this into our selenium test so let me go to our earlier tests now if you have some other test you can use it there so you can just follow the concept I'm taking one of our test and G demo test that we used earlier so this is our test in G demo now what I'm going to do here this here I will make this so here we have a setup test what I am going to do is I will create a string variable or I will create a variable here at the top level I will say string browser name equals as of now I will call it as null and here in the set of tests I will say if browser name equals equals or let me use dot equals it in our case so even if there is some case sensitive thing it will be ignored and I will say if it is equals to Chrome then I will set it set the browser to Chrome and this is the statement to set it to chrome and this we have already seen earlier so I am just doing it very fast so system dot set property webdriver dot chromedriver equals to the location of the chrome driver dot exe which is here so in the drivers folder I have chrome driver and chromedriver dot exe is here so I am just setting this value here okay project path I have to take it up I will take it here ok and then then I will say else if I will just copy this again and I will say else if browser name dot equals ignore case Firefox then I have to set it to Firefox so for Firefox let me just take some other earlier class where I have set Firefox and yes it is here I am just copying it from here and I will paste it here ok so if the browser name is Firefox we will set the properties and Driver according to Firefox and this driver should be taken from our class variable and now it is done now what I have to is this browser name I have to set from the properties file so I have browser chrome here and we have this properties file here so what I'm going to do is in get properties I will call this test ng demo class and I will say test ng demo and I will say dot but do you see I am not getting any browser name variable here so for that what I have to do is I have to make this browser name public and I think public static okay now if I go here and I say test ng demo dot you can see I am getting the browser name and this I have to set to whatever is available in my properties file or conflict or properties file so I will make it equal to whatever is there in my config file I can also use this browser variable because I have already stored it here so I instead of writing this again I will just use this browser variable okay and so this whatever is set here in the console dot properties in browser that will come here in browser variable and then it will be stored in the destined to demo browser name variable which is this one this one and then in the setup test we are checking if the browser name equals chrome then run this and if the browser name equals Firefox then run this okay so again what I have to do is I also have to call this properties file function get properties so for that what I will do here is I will say properties file dot and I have to use and get properties so I will use this function so what will happen is it will come in setup test call this get properties go here and here it will get the browser property or the name which is chrome set it to the test in G demo browser name variable come back here and now this is set to whatever is available in our consider properties and the rest of the execution will go accordingly so let us try to run this and we know our browser is set to chrome so here I will do a right-click I will say run this test in G test and let us see so yes I think it has detected Chrome browser and it is running our test on a Chrome browser and everything is passed and yes everything is passed now let us test it with the Firefox I will go to my config dot properties make it equal to Firefox save it go back to my test in G demo and run as test in G test let us now see what happens so yes it has detected Firefox and now it is opening a Firefox browser and doing all the things and now it is done so everything is passed and I think there is some error although it ran on Firefox properly so but there is some error I think it is some error during teardown so let us see so here if you go to the so you can see this is the test which is being run it is going to google.com and then entering automation step by step in the search box and hitting on the return key or the enter key so this we have done earlier and here I think browser or driver dot cute is having a problem in Firefox I will comment this out and run as test in G test and let us now see yes it is running on a Firefox browser and yes everything is ok no it is find out also we have seen that we can also write our properties or parameters in a config file using the set properties so what I will do is I will write a new property result and I will say this is us ok this is past and what I will do is in our test energy demo in the teardown if everything goes fine and we are able to test run the test successfully I will again call properties file class dot I will now say set properties and this will call the set properties method which is this one and it will lock or put result equals pass in our configure properties let us now see so I will run this again and let us now see it is running on a Firefox opens a browser goes to google.com and it is done everything was very fast so let us go back and see our concluded properties and you can see result equals pass has come here okay so this is how you can use your concluded properties to set the properties for your execution and here now let us very quickly look at some useful tips so the very first tip is you can use hash or exclamation mark to put comments on the properties file and this is very important so you should always put comments in properties files so that it is very easy to refer and for a new person it is easy to know what is this particular parameter for so you can say this is or I should say here right this parameter sets the browser for test execution and similarly you can use different comments and you can use exclamation mark or a hash sign to write the comments the second tip is you can use the debug mode to troubleshoot in case of any errors so for example let me just show you here we have given this let me go up and here we are calling this properties file now just in case at any location in your program if you are finding any problem or any error you can go here and double click here and put a breakpoint so you can see this dot this is a breakpoint and if you go to this particular section and double click it will be a breakpoint and now what you can do is you can right-click and in stead of húrin s select debug s and Ike will debug as test in G test so what will happen is that execution will break I will come here and it will be paused here so you can see the execution has come until this point where I have put the breakpoint and it is now paused here and now I can step-by-step go inside by pressing the f5 or f6 key so if I have to go inside this particular function I will press f5 I am pressing f5 on my keyboard and you can see now it has gone inside the properties file get properties function now I am pressing f6 so it is step by step go and you can actually see what is happening so now if I hover over this browser you can see it has taken the value Firefox and now I will again press f6 and again press f6 and now if I hover over the browser name you can see now it has gone Firefox and then you can press f8 to resume and also you can press you can create multiple breakpoints so for example if I create a breakpoint here and then I create a breakpoint somewhere else so if I press f8 it will go from one breakpoint to other so this is very useful for our troubleshooting and you can use it at any time whenever you are finding any issues and this is a wiki page for property properties and this will be very helpful let me just show you so here you can see I will provide this link in the description or note section of this video as well so dot properties is a file extension for files mainly used in Java and also you can see you can create the properties or parameters in different formats like key equals value key space equals space value key colon value and so on so here you can see what we have used is key equals value but you can use any of these formats let us do a very quick recap today we learnt what is a properties file how to create a properties file how to get data from a properties file how to set data and how to run our test using a properties file I hope the session was very useful for you if you this session please hit the like button and share with everyone so that it can help everyone and don't forget to subscribe to this channel I will meet you in the next episode thank you for watching
Info
Channel: Automation Step by Step
Views: 19,342
Rating: 4.9322033 out of 5
Keywords: selenium what is a properties file, how to use properfies file, how to use config properties file in selenium java, selenium java beginner tutorials, selenium beginner tutorials, selenium webdriver beginner tutorials, selenium java framework for beginners
Id: 1GeVbi1uj_8
Channel Id: undefined
Length: 26min 29sec (1589 seconds)
Published: Thu Aug 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.