Selenium WebDriver "Cannot instantiate class " error is coming



  • Below is my Base Class:

    package com.mercury.qa.base;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    
    import com.mercury.qa.util.TestUtil;
    
    public class BaseTest {
    
    public static WebDriver driver;
    public static Properties prop;
    
    public BaseTest(){
    
        try {
            prop = new Properties();
            //FileInputStream ip = new FileInputStream("C:\\Users\\Pdeshmukh1\\workspace\\MercuryToursPreet\\src\\main\\java\\com\\mercury\\qa\\config\\config.properties");
            FileInputStream ip = new FileInputStream(System.getProperty("C:\\Users\\Pdeshmukh1\\workspace\\MercuryToursPreet\\src\\main\\java\\com\\mercury\\qa\\config\\config.properties"));
    
            prop.load(ip);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void initialization(){
        String browserName = prop.getProperty("browser");
    
        if(browserName.equals("chrome")){
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Pdeshmukh1\\Music\\chromedriver.exe");    
            driver = new ChromeDriver(); 
        }
        else if(browserName.equals("FF")){
            System.setProperty("webdriver.gecko.driver", "C:\\Users\\Pdeshmukh1\\Music\\geckodriver.exe");  
            driver = new FirefoxDriver(); 
        }
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(TestUtil.Page_Wait_Timeout, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(TestUtil.Implicit_wait, TimeUnit.SECONDS);
    
        driver.get(prop.getProperty("url"));
    
    }
    }
    

    Below is my Page class:

    package com.mercury.qa.pages;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    
    import com.mercury.qa.base.BaseTest;
    
    public class LoginPage extends BaseTest {
    
    //PageFactory : Object Repository 
    
         @FindBy(xpath = "//a[text()='SIGN-ON']")
         WebElement SignOnLink;
    
         @FindBy(xpath = "//a[text()='REGISTER']")
         WebElement RegisterLink;
    
         @FindBy(xpath = "//a[text()='SUPPORT']")
         WebElement SupportLink;
    
         @FindBy(xpath = "//a[text()='CONTACT']")
         WebElement ContactLink;
    
         @FindBy(xpath = "//body/div/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/p[1]/img[1]")
         WebElement MercuryLogo;
    
        //Initializing the Page Objects:
         public LoginPage(){
    
             PageFactory.initElements(driver, this);
    
         }
    
         //Actions:
         public String ValidateLoginPageTitle(){
             return driver.getTitle();
         }
    
         public boolean ValidateSignOnLink(){
             return SignOnLink.isDisplayed();            
         }
    
    
         public boolean ValidateRegisterLink(){         
             return RegisterLink.isDisplayed();              
         }
    
    
         public boolean ValidateSupportLink(){          
             return SupportLink.isDisplayed();           
         }
    
    
         public boolean ValidateContactLink(){          
             return ContactLink.isDisplayed();           
         }
    
         public boolean ValidatepageLogo(){         
             return MercuryLogo.isDisplayed();           
         }
    
         public RegistrationPage OpenRegPage(){
             RegisterLink.click();           
             return new RegistrationPage();
         }
    
    
    }
    

    Below is my test Class

    package com.mercury.qa.testcase;
    
    import org.testng.Assert;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    
    import com.mercury.qa.base.BaseTest;
    import com.mercury.qa.pages.LoginPage;
    import com.mercury.qa.pages.RegistrationPage;
    
    public class LoginPageTest extends BaseTest{
    
    LoginPage loginPage;
    RegistrationPage RegPage;
    
    public LoginPageTest(){     
     super();
    }
    
    @BeforeMethod
    public void setup(){
        initialization();       
        loginPage  = new LoginPage(); //initializing Loginpage constructor
    }
    
    @Test(priority = 1)
    public void loginPageTitleTest(){
        String Title = loginPage.ValidateLoginPageTitle();
        Assert.assertEquals(Title, "Welcome: Mercury Tours");       
    }
    
    @Test(priority = 2)
    public void MercuryLogoTest(){
        boolean flag = loginPage.ValidatepageLogo();
        Assert.assertTrue(flag);
    }
    
    @Test(priority = 3)
    public void LinksTest(){
        boolean flag1 = loginPage.ValidateSignOnLink();
        Assert.assertTrue(flag1);
        boolean flag2 = loginPage.ValidateRegisterLink();
        Assert.assertTrue(flag2);
        boolean flag3 = loginPage.ValidateContactLink();
        Assert.assertTrue(flag3);
        boolean flag4 = loginPage.ValidateSupportLink();
        Assert.assertTrue(flag4);
    
    }
    
    @Test(priority = 4)
    public void OpenRegPage(){
        RegPage = loginPage.OpenRegPage();
    }
    
    
    @AfterMethod
    public void TearDown(){
        driver.quit();
    }
    
    }
    

    Below is the error i am getting:

    {

    org.testng.TestNGException: Cannot instantiate class com.mercury.qa.testcase.LoginPageTest at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38) at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387) at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299) at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110) at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:195) at org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:120) at org.testng.TestRunner.initMethods(TestRunner.java:409) at org.testng.TestRunner.init(TestRunner.java:235) at org.testng.TestRunner.init(TestRunner.java:205) at org.testng.TestRunner.(TestRunner.java:160) at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:144) at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:273) at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561) at org.testng.SuiteRunner.init(SuiteRunner.java:157) at org.testng.SuiteRunner.(SuiteRunner.java:111) at org.testng.TestNG.createSuiteRunner(TestNG.java:1272) at org.testng.TestNG.createSuiteRunners(TestNG.java:1259) at org.testng.TestNG.runSuitesLocally(TestNG.java:1113) at org.testng.TestNG.run(TestNG.java:1030) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:115) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:207) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:178) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29) ... 21 more Caused by: java.lang.NullPointerException at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at com.mercury.qa.base.BaseTest.(BaseTest.java:26) at com.mercury.qa.testcase.LoginPageTest.(LoginPageTest.java:18) ... 26 more }



  • Judging by this:

    Java.lang.NullPointerException at java.io.FileInputStream.(Unknown Source) 
        at java.io.FileInputStream.(Unknown Source) 
        at com.mercury.qa.base.BaseTest.(BaseTest.java:26) 
        at com.mercury.qa.testcase.LoginPageTest.(LoginPageTest.java:18) ... 26 more }
    

    The properties file cannot be found - you should fix the path, or create a folder resources and use Classloader to load the file like this:

    prop.load(this.getClass().getResourceAsStream("properties_file"));
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2