'Cant figure out why my Object repository isnt working - xpath is read as null

i am rather new to coding in general and Have gotten most of my code from my instructor who is on vacation at the moment

The problem i am having is i made my OR.properties and OR_Propertiesloader class

Inside the OR_Propertiesloader class i am able to print out the xpath

But when i put it into a test script it read the value as NULL

Here is my code so far

     package com.reuseables;
        
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.util.Properties;
        
        public class OR_PropertiesLoader {
        
            private static Properties OR_P_Obj;
        
        
            private static String userNameBox;
        
        
            public static String userNameBoxEle(){
                return userNameBox;
            }
        
        
        
            public static void load_OR_Properties() throws FileNotFoundException {
                try {
                    OR_P_Obj = new Properties();
                    OR_P_Obj.load(
                            new FileInputStream(
                                    new File(
                                            System.getProperty("user.dir") +
                                                    "/src/test/resources/ObjectRepository/OR.properties")));
                }catch (Exception e){
                    System.out.println("Unable to read property file. Pls check  the location of the file");
        
                }
            }
        
            public static String getProperty(String propertyName) {
                return OR_P_Obj.getProperty(propertyName);
        
            }
        
            public static void initialize_OR_Configurations() throws FileNotFoundException {
                load_OR_Properties();
                userNameBox = getProperty("userNameBox");
        
        
            }
        
            public static void main(String[] args) throws FileNotFoundException {
                OR_PropertiesLoader.initialize_OR_Configurations();
        
                System.out.println(OR_PropertiesLoader.userNameBoxEle());
        
            }
        
        }

The line im trying to use in my properties file is

userNameBox =//input[@id='txtUsername']

And this is the part of the test script im trying to throw the xpath into

package com.login.tests;

import com.reuseables.BaseTest;
import com.reuseables.ConfigPropertiesLoader;
import com.reuseables.OR_PropertiesLoader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;

public class personalDetailsValidationScenario extends BaseTest {

    @Test
    public void ValidatePositivePersonalDetailsScenario () throws  InterruptedException {



        // Go to URL
        driver.get(ConfigPropertiesLoader.getApplicationURL());

        //Enter Username
        driver.findElement(By.xpath(OR_PropertiesLoader.userNameBoxEle()));
        driver.findElement(By.xpath(OR_PropertiesLoader.userNameBoxEle())).clear();
        driver.findElement(By.xpath(OR_PropertiesLoader.userNameBoxEle())).sendKeys(Username);

...

I previously tried to throw a " System.out.println(OR_PropertiesLoader.userNameBoxEle());" but it also gave me a null before failing the test

If anybody is able to lend me a hand with this is would greatly appreciate it!



Solution 1:[1]

@pcalkins Oh my god, thank you! the god damn config was not loading

Im pretty sure i didnt do the fix exactly the way you meant it, as i said im seemingly not fully understanding the way java works sometimes as i mentioned im pretty new at coding

But basically the change i made was to the test script itself

package com.login.tests;

import com.reuseables.BaseTest;
import com.reuseables.ConfigPropertiesLoader;
import com.reuseables.OR_PropertiesLoader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.FileNotFoundException;

public class personalDetailsValidationScenario extends BaseTest {

    @Test
    public void ValidatePositivePersonalDetailsScenario() throws InterruptedException, FileNotFoundException {

        OR_PropertiesLoader.initialize_OR_Configurations(); // <<<<This is the change i made>>>>

        // Go to URL
        driver.get(ConfigPropertiesLoader.getApplicationURL());

        //Enter Username
        driver.findElement(By.xpath(OR_PropertiesLoader.userNameBoxEle()));
        driver.findElement(By.xpath(OR_PropertiesLoader.userNameBoxEle())).clear();
        driver.findElement(By.xpath(OR_PropertiesLoader.userNameBoxEle())).sendKeys(Username);

I'll figure out how Mystical secrets of Java one day

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vlad G.