Search code examples
javaseleniumselenium-webdrivertestngtestng.xml

Test method is always failed if i run it using tesng


Please help me out on this, I am always getting this error tried multiple times

public class BaseClass {
    
    WebDriver driver;
    Properties properties;
    FileInputStream inputstream;
    @BeforeClass
    public void setup() throws IOException, InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        Properties properties = new Properties();
        FileInputStream inputstream = new FileInputStream("C:\\Users\\admin\\eclipse-workspace\\NiftyUAT1\\src\\test\\java\\com\\pks\\test\\Objects.Properties");
        properties.load(inputstream);
        }

    @Test
    public void test1() { 
        System.out.println(111);
        driver.get(properties.getProperty("URL"));
        driver.findElement(By.xpath("//input[@id='login-email']")).sendKeys(properties.getProperty("Email"));
        driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys(properties.getProperty("Pwd"));
        driver.findElement(By.xpath("//button[@id='login-submit']")).click();
    }
}

Solution

  • You declared driver and properties as local variables, shadowing the field you are using in the test method. Just remove the type in front of the declarations and it should work:

    public class BaseClass {
        
        WebDriver driver;
        Properties properties;
        
        @BeforeClass
        public void setup() throws IOException, InterruptedException {
            WebDriverManager.chromedriver().setup();
            
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            
            properties = new Properties();
            try (FileInputStream inputstream = new FileInputStream("C:\\Users\\admin\\eclipse-workspace\\NiftyUAT1\\src\\test\\java\\com\\pks\\test\\Objects.Properties")) {
                properties.load(inputstream);
            }
        }
        // the rest is the same
    }
    

    Plus, you probably don't need an inputstream field later on so just have the loading use it locally in try-with-resources.