2

I've posted below some sample code which I've done so far and I'm getting an Exception java.lang.NullPointerException.

Base Class:

public class TestBase {
    public static WebDriver driver= null;

    @BeforeSuite
    public void initialize(){
        System.setProperty("webdriver.chrome.driver","...chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("abcd.com");
    }

    @AfterSuite
    public void TearDownTest()
    {
        TestBase.driver.quit();
    }
}

Login Page:

public class LoginPage {

    WebDriver driver;

    public LoginPage(WebDriver driver)
    {
        this.driver= driver;
    }

    @FindBy(how=How.XPATH, using="//*[@id='email_id']") WebElement EmailTextBox;
    @FindBy(how=How.XPATH, using="//*[@id='password']")WebElement PassworTextBox;
    @FindBy(how=How.XPATH, using="//*[@id='frm_login']/div[4]/input") WebElement LoginButton;

    public void setemail(String strmail)
    {
        EmailTextBox.sendKeys(strmail);
    }

    public void setpwd(String strpwd)
    {
        try
        {
            PassworTextBox.sendKeys(strpwd);
        }
        catch (TimeoutException e) 
        {
            System.out.println("Time out exception " + e);
        } 
        catch (ElementNotSelectableException e) {
            System.out.println("Element not selectable exception " + e);
        } catch (NoSuchElementException e) {
            System.out.println("No such element found " + e);
        } catch (ElementNotVisibleException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Something Wrong " + e);
        }
    }

    public void ClickLoginBtn()
    {
        LoginButton.click();
    }
}

LoginTest class:

public class LoginTest extends TestBase{

    @Test
    public void init()
    {
        System.out.println("In the init method");
        LoginPage lg=PageFactory.initElements(driver, LoginPage.class);
        lg.setpwd("123123");
        lg.setemail("abc@gmail.com");
        lg.ClickLoginBtn();
    }
}

I am getting the NullPointerException while setting the username and password. Could you please help me?

I am new to the POM with PageFactory so I dont have any idea How to resolve it but If anyone can help me with that it would be big help to me.

Ardesco
  • 7,281
  • 26
  • 49
vijayateke
  • 77
  • 1
  • 10

3 Answers3

1

You shouldn't initialise the WebDriver instance i.e. driver twice as follows:

public static WebDriver driver= null;

and

WebDriver driver=new ChromeDriver();

Keep the global declaration as:

public static WebDriver driver= null;

And channge the line as:

driver=new ChromeDriver();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Try to add a timeout after driver.get("abcd.com"); to be sure page has finished to load, and can find WebElements defining EmailTextBox and PassworTextBox.
Or use a WebDriverWait like this

new WebDriverWait(driver, 10))
   .until(ExpectedConditions.visibilityOf(By.id("someid")));
Ehcnalb
  • 466
  • 4
  • 16
  • An explicit wait is not going to fix a NullPointerException – Ardesco Jun 26 '19 at 09:53
  • If the problem comes from an undefined element due to absence of their id in page, it could be – Ehcnalb Jun 26 '19 at 12:19
  • @Ehcnalb No, that would throw `NoSuchElementException`. `NullPointerException` might be thrown if you didn't use `initElements` method of `PageFactory` class – Fenio Jul 01 '19 at 08:57
0

I hope you have initialized the page factory elements like so

public LoginPage(WebDriver driver) {           
  this.driver = driver; 
  PageFactory.initElements(driver, this);
}

In your Test class, I would place

LoginPage lg = new LoginPage(driver);

Read more here:
https://www.seleniumeasy.com/selenium-tutorials/page-factory-pattern-in-selenium-webdriver