Protected by Copyscape
Powered By Blogger

Tuesday, January 21, 2020

Why we should use property file in TestNG framework? How to use it?

When you write a code with Selenium API then there are several places where you use the same value. For example you have "Delhi" as a value for "Place" variable in 10 different test cases and now you have to change the value of "Place" variable from "Delhi" to "Gurgaon" in all 10 test cases. To do the same you have to open each test case and make that required change and the same process will go on if some different value is required in future. So, to ease this work we can use Property file where you need to make a change at a single place only and the changes will reflect in each test case where we are calling that property file. Mainly, we use property file for configuration settings for your entire project like on which browser your code should be executed, how much synchronization wait time is required like below:

#Confing file
URL=https://www.goggle.co.in
Browser=Chrome
ImplicitWaitTime=20
ExplicitWaitTime=20


How to use it?
  • Create one Java project and then TestNG class under that project. I have already explained this process in my previous posts.
  • Create one folder with any name under project. In my case I have created folder 'PropertyFIles'.



  •  



    • Now Create a file under newly created folder i.e. "Config.properties" and place the below content in it. We store data in property file in "key=value" pair. To comment a line in property file we use "#" symbol.
              #Confing file
                URL=https://www.goggle.co.in
                Browser=Chrome
                ImplicitWaitTime=20
                ExplicitWaitTime=20









    • Let's see that how we can use this property file in TestNG framework.
      • Import this Jar in your TestNG Project "import java.util.Properties;"'
      • Create an object of "Properties" class i.e "public Properties configProp;"
      • To load that property file we have to create an object of "FileInputStream" class and for this we have to import Jar i.e. "import java.io.FileInputStream;".
    Look at the below code and their output after execution where I am extracting the values from the Property file.

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;

    import org.testng.annotations.Test;

    public class NewTest

    {
    public Properties configProp;
    public FileInputStream file;

      @Test
      public void FetchData() throws IOException
      {
      file=new FileInputStream("./PropertyFIle/Config.properties");
      configProp=new Properties();
      configProp.load(file);

      System.out.println("Value of Browser key is :"+ configProp.getProperty("Browser"));
      System.out.println("Value of URL key is :"+ configProp.getProperty("URL"));
      System.out.println("Value of Implicitwait key is :"+ configProp.getProperty("ImplicitWaitTime"));
      }
    }

    Output

    [RemoteTestNG] detected TestNG version 7.0.0
    Value of Browser key is :Chrome
    Value of URL key is :https://www.goggle.co.in
    Value of Implicitwait key is :20
    PASSED: FetchData

    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================


    ===============================================
    Default suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================


    Please comment if you have any doubts in it.

    Best of Luck!

    No comments: