java.lang.NullPointerException when running testcase via Selenium

后端 未结 2 356
礼貌的吻别
礼貌的吻别 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: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..:)

提交回复
热议问题