0

Can somebody, please, help me to figure out what is wrong with my test? I'm getting that NullPointerException error in Eclipse all the time.

MyTestcase:

public class ExampleTest extends Basetest {

    private LoginPage loginPage;
    private DashboardPage homePage;

    @Before
    public void setUp() throws Exception {

        loginPage =new LoginPage(driver);
        homePage = new DashboardPage (driver);  

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {

        loginPage.load();
        loginPage.login();
        homePage.getWelcomeMessage();
        String actualWelcome=homePage.getWelcomeMessage();
        assertEquals("Welcome Admin",actualWelcome);

        homePage.logout();

    }

}

BasPage: public class BasePage {

public WebDriver driver;
public WebDriverWait wait;
public String pageUrl= "http:/hrm.seleniumminutes.com"; 

public BasePage(WebDriver driver) {


    this.driver=driver;
    wait = new WebDriverWait(driver, 5);

}

public void load () {

    driver.get(pageUrl);

}

}

LoginPage:

public class LoginPage extends BasePage {

    public LoginPage(WebDriver driver) {
        super(driver);

    }

    public void login() {
        // TODO Auto-generated method stub
        driver.findElement(By.id("txtUsername")).sendKeys("admin");;
        driver.findElement(By.id("txtPassword")).sendKeys("Password");;
        driver.findElement(By.id("btnLogin")).click();;
    }

}

DashBoardPage:

public class DashboardPage extends BasePage {

    public DashboardPage(WebDriver driver) {
        super(driver);
        // TODO Auto-generated constructor stub
    }

    public String getWelcomeMessage() {


    return wait.until(ExpectedConditions.presenceOfElementLocated(
    By.id("welcome"))).getText();


    }

    public void logout() {


     wait.until(ExpectedConditions.visibilityOfElementLocated(
     By.linkText("Logout"))).click();

    }

}

BaseTest:

public class Basetest {

    protected WebDriver driver;
    protected WebDriverWait wait;


    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
        driver = new FirefoxDriver();
        wait=new WebDriverWait(driver, 5);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @After
    public void basetearDown() throws Exception {
        driver.quit();
}}
  • 1
    Please read stackoverflow.com/questions/218384/… – AdrianHHH Mar 20 '18 at 23:42
  • what's the actual error message you're seeing? Is it always the same class/line number? Which one? – ernie Mar 21 '18 at 0:26
  • 1
    Show your error log – Shahid Mar 21 '18 at 3:45
  • Give us BasePage code and the stack trace you're getting with your exception – Alexey R. Mar 21 '18 at 7:52
  • Ok, I got 2 errors, both are java.lang.NullPointerException: 1. at pages.BasePage.<init>(BasePage.java:15) which correspond to: wait = new WebDriverWait(driver, 5); at pages.LoginPage.<init>(LoginPage.java:9) which correspond to: super (driver); at test.ExampleTest.setUp(ExampleTest.java:23) which correspond to: loginPage =new LoginPage(driver); 2 java.lang.NullPointerException at test.Basetest.baseearDown(Basetest.java:30) which correspond to: driver.quit(); – Nastia Mar 21 '18 at 14:37
1

driver declaration is missing in ExampleTest and DashboardPage class. Since you have WebDriver as protected in the Basetest class

protected WebDriver driver;

Its scope is limited to Basetest Class. Define the driver as public WebDriver driver in ExampleTest and DashboardPage class. below is the syntax

public WebDriver driver;
| improve this answer | |
0

you need split this in all cases:

return wait.until(ExpectedConditions.presenceOfElementLocated( By.id("welcome"))).getText();

as

//only waiting
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("welcome")));
// real value
return driver.find_element(By.id("welcome")).getText();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.