Thursday, June 27, 2024

Read config.properties file

 

Read config.properties file :

 

Create Config.properties:

 

Steps to Create config.properties File:

  1. Right-click on your project/package.
  2. Select New > File.
  3. Enter the file name with the extension .properties (e.g., config2.properties).
  4. Click the Finish button.
  5. This will create a properties file in your project/package.

 

in Config. properties file contains the below info

 

browser,

username,

password,

QA1Env,

QA2Env ,

PreprodEnv,

ProdEnv

RunEnvironment

....

..... etc

-------------------

 

Define Data in config.properties:

---------------------------------

# Define browser,username,password,QA1Env,QA2Env, Dev ,PreprodEnv,ProdEnv

#   In config . properties file, comment should start with # symbol

#  or press  ctrl + /

browser = chrome

username =  admin

password = Admin123

 

 

QA1Env = https://opensource-demoQA1.orangehrmlive.com/web/index.php/auth/login

QA2Env = https://opensource-demoQA2.orangehrmlive.com/web/index.php/auth/login

DevEnv = https://opensource-demoDev.orangehrmlive.com/web/index.php/auth/login

PreprodEnv = https://opensource-demoPreProd.orangehrmlive.com/web/index.php/auth/login

ProdEnv = https://opensource-demo.orangehrmlive.com/web/index.php/auth/login

 

# comment RunEnvironment contains QA1, QA2, Dev or PreprodEnv,ProdEnv

RunEnvironment = QA1

 

Read data from "Properties" file :

Class -  Properties -

some methods

 

-----------------------

package Utilities;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

 

public class TestReadPropertiesFileBasics {

 

            public static void main(String[] args) throws IOException {

 

//         C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024\src\Utilities\Config.properties

 

 

                       

                        //   Read /get 'browser'  name from config .properties file

                        // Properties --predefined  class in Java

                        //  has some predefined methods can be used to read the property file

                        //  defined in java.util pkg

                       

                        //create obj for -Properties  class

                        Properties props = new Properties();

                        FileInputStream fis =  new FileInputStream("C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SeleniumMay52024\\src\\Utilities\\Config.properties");

                        props.load(fis);

                       

                        String browser  = props.getProperty("browser");

                        //                      chrome

               //    browser = chrome        

 

                        System.out.println("browserVal from config.props file=" + browser);

 

                        //browserVal=chrome

 

                        // Read 'user name'  from config.props

                        String username =  props.getProperty("username");

                        //                    admin

                        System.out.println("userName from config.props file=" +  username);

 

                        //userNAme=admin

 

                        //HW  Read 'passsword' from config.props

 

                        System.out.println("pwd= from config.props file=");

                        // pwd=mercury

 

           

                        //Note: if given key name / prop name is not available in props file, getProperty() returns 'null' value

                        // plz check given key name /props name is exist in props file

                        String pwd= props.getProperty("pwd");

                        //                    null

                        //  pwd= null

                        System.out.println("pwd="+pwd);

 

                        // HW Read 'QA1 Env' from config.props file?

 

                        // HW Read 'QA2Env' from config.props file?

 

                        // HW Read  'RunEnvironment"  from config.props file?

 

 

            }

 

}

 

 

-----------------

 

// HW Create new 'config2.properties' file  and define some data , read data

     ename = Ram

    City =  Bangalore

    Address =  Marathahalli

                       

FAQ How to read data from Properties file ?

 

FAQ where do you define the test data in your Project ?

 

We can define test data in config.properties also

 

Develop resuable method to read properties file ?

 

 String   readPropertiesFile(String keyname)

{

 

 

}

call:  readPropertiesFile("browser");// chrome

 

package PropertiesFileBasics;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

 

public class TestDevelopGenericMethodToReadPropertiesFile {

 

            //

//  Develop resuable method to read properties file ?

           

            // readPropertiesFile("browser");

            public static String readPropertiesFile(String keyName) throws IOException

            { //                                        browser   i.e keyName = browser

                        Properties  prop = new Properties();

                        String propsFilePath = "C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SeleniumMay52024\\src\\Utilities\\Config.properties";

                        FileInputStream fis = new FileInputStream(propsFilePath);

                        prop.load(fis);

                       

                        String keyVal = prop.getProperty(keyName);

                       

                        // handle if given key name is not there in config file

                        if( keyVal  == null)

                        {

                                    System.out.println("Plz check in Properties file="+propsFilePath+", Given key name/ property name='" +keyName+"' is not available");

                                    System.out.println("Read Properties file, keyname=" +keyName+" ,keyVal="+keyVal);

                                   

                        }

                        else

                        {

                                    System.out.println("Read Properties file, keyname=" +keyName+" ,keyVal="+keyVal);

                        }

                                                           

           

                                    return keyVal;

 

            }

 

 

package Utilities;

 

import java.io.IOException;

 

public class TestReadPropsFileMethod {

 

            public static void main(String[] args) throws IOException {

                        WebUtilites util = new WebUtilites();

                        String browser = util.readPropertiesFile("browswer");

//                     String browser = util.readPropertiesFile("browser");

                        System.out.println("browser=" + browser);

                       

                       

                        String username = util.readPropertiesFile("username");

                        System.out.println("username=" + username);

                       

                         // HW read password by passing values in method call

                         

                         //HW  Read QA1Env,QA2Env  by passing values in method call

                         

 

            }

 

}

 

 

 

Define all locator name and locator values in Properties file :

 

Create OR.properties file :

 

firstnameTxtbox=firstname_name

lastNameTxtbox =lastname_name

maleRadioBtn =//input[@id='maleid']_xpath

femaleRadioBtn =//input[@id='femaleid']_xpath

 

 

Read OR.props file:

 

package Utilities;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

 

public class TestORPropertiesFile {

 

            public static String readORPropertiesFile(String keyName) throws IOException

            { //                                        browser   i.e keyName = browser

 

 

                        Properties  prop = new Properties();

                        String propsFilePath = "C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SeleniumMay52024\\src\\Utilities\\OR.properties";

                        FileInputStream fis = new FileInputStream(propsFilePath);

                        prop.load(fis);

                       

                        String keyVal = prop.getProperty(keyName);

                       

                        // handle if given key name is not there in config file

                        if( keyVal  == null)

                        {

                                    System.out.println("Plz check in Properties file="+propsFilePath+", Given key name/ property name='" +keyName+"' is not available");

                                    System.out.println("Read Properties file, keyname=" +keyName+" ,keyVal="+keyVal);

                                   

                        }

                        else

                        {

                                    System.out.println("Read Properties file, keyname=" +keyName+" ,keyVal="+keyVal);

                        }                                                          

           

                                    return keyVal;

 

                                   

}

           

            public static void main(String[] args) throws IOException {

                        // Read "firstnameTxtbox from ORConfig.props file

                                   

                        String  firstnameTxtbox =       readORPropertiesFile("firstnameTxtbox");

                                   

                                                System.out.println("firstnameTxtbox="+firstnameTxtbox);

                                               

                                                // Read "maleRadioBtn' from ORConfig.props file

                                                String maleRadioBtn = readORPropertiesFile("maleRadioBtn");

                                                System.out.println("maleRadioBtnVal="+maleRadioBtn);

                                                ////input[@id='maleid']_xpath

                                               

//                                               xpath--- change

                                                //  100 classes --we have to changes

 

 

            }

 

}

 

 

Note:  We can define these reusable methods in "WebUtililties" class

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...