java.lang.NullPointerException when running testcase via Selenium

后端 未结 2 354
礼貌的吻别
礼貌的吻别 2021-01-14 15:59

I am facing an issue with NullPointerException. I tried as much as possible but I\'m unable to resolve this. I am implementing a POM model(Selenium) for my project.

相关标签:
2条回答
  • 2021-01-14 16:45

    Your driver seems to be null what you can do to avoid this situation is to instantiate your driver reference.

    In your test class you can instantiate your driver.

    public class VendorsHomePageTest {
        public WebDriver driver;
        @Test
        public void verifyVendorsHomePageTest() throws Exception {
            driver = new FirefoxDriver(); // or whatever browser you want.
            // Here you can initialise the elements in `PageObject` class.
            VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);
            // Rest of your code
        }
    }
    
    0 讨论(0)
  • 2021-01-14 16:56

    You need to initialize your WebDriver first. As I seeing you are creating the reference of WebDriver but not initialized, try as below :-

    WebDriver driver = new ChromeDriver(); //or other driver which you want
    

    Note :- If you want to initialize ChromeDriver you need to download chromedriver and set system property before initialize driver as :-

    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    

    So details answer like as below :-

    public class VendorsHomePageTest 
    
    {
        public WebDriver driver;
        @Test
        public void verifyVendorsHomePageTest() throws Exception
        {
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver = new ChromeDriver(); //or other driver which you want
    
            LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);
    
            login.verifyLoginVendors();
    
            VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);
    
            //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
            Thread.sleep(3000);
    
            vhpapp.clickOnNotificationsTab();
    
            vhpapp.clickOnProfileTab();
    
            vhpapp.clickOnTendersTab();
    
            vhpapp.clickOnDashboardTab();
    
            vhpapp.clickOnFirstTender();
    
        }
    
    }
    

    Edited :- You need to create separate class which gives you WebDriver instance as below :-

    public class DriverInit {
    
        public WebDriver driver;
        private static DriverInit driverInit = null;
    
        public static DriverInit getInstance() {
            if (driverInit == null) {
                driverInit = new DriverInit();
            }
            return driverInit;
        }
    
        private DriverInit() {
            this.driver = new FirefoxDriver();
            this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            this.driver.get("******");
        }
    
        public WebDriver getDriver() {
            return this.driver;
        }
    

    now you can call it into LoginIntoVendors as below :-

    public class LoginIntoVendors 
    {
        @Test
           public void verifyLoginVendors() throws Exception
           {
               WebDriver driver = DriverInit.getInstance().getDriver();
    
               IntiationPage vendorInit=PageFactory.initElements(driver, IntiationPage.class);
    
               vendorInit.clickOnLoginButton();
    
               VendorsLoginAction Loginven=PageFactory.initElements(driver, VendorsLoginAction.class);
    
               Loginven.vendorlogin("***","***@gmail.com", "****");
    
               String title = driver.getTitle();
               System.out.println("Title is  :" + title  );
    
    
           }
    }
    

    and in the VendorsHomePageTest as below :-

    public class VendorsHomePageTest 
    
    {
        public WebDriver driver;
        @Test
        public void verifyVendorsHomePageTest() throws Exception
        {
    
            driver = DriverInit.getInstance().getDriver();
    
            LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);
    
            login.verifyLoginVendors();
    
            VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);
    
            //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
            Thread.sleep(3000);
    
            vhpapp.clickOnNotificationsTab();
    
            vhpapp.clickOnProfileTab();
    
            vhpapp.clickOnTendersTab();
    
            vhpapp.clickOnDashboardTab();
    
            vhpapp.clickOnFirstTender();
    
        }
    
    }
    

    Hope it helps..:)

    0 讨论(0)
提交回复
热议问题