-3

There is my code:

LoginPage Class:

public class LoginPage {

    private WebDriver driver;

    public LoginPage(WebDriver driver) {

        this.driver = driver;
    }

    private By loginField = By.xpath("//input[@id='login_field']");

    private By passwordField = By.xpath("//input[@id='password']");

    private By signInButton = By.xpath("//input[@value='Sign in']");

    private  By heading = By.xpath("//div[contains(@class,'auth-form-header p-0')]/h1[text()='Sign in to GitHub']");

    private By error = By.xpath("//div[@id='js-flash-container']//div[@class='container']");

    private By createAccountLink = By.xpath("//a[text()='Create an account']");

    public LoginPage typeUsername(String username){
        driver.findElement(loginField).sendKeys(username);
        return this;
    }

    public LoginPage typePassword(String password){
        driver.findElement(passwordField).sendKeys(password);
        return this;
    }

    public LoginPage loginWithInvalidCreds(String username, String password){
        this.typeUsername(username);
        this.typePassword(password);
        driver.findElement(signInButton).click();
        return new LoginPage(driver);
    }

    public String getHeadingText(){
        return driver.findElement(heading).getText();
    }

    public String getError(){
        return driver.findElement(error).getText();
    }
public SignUpPage createAccount(){
    driver.findElement(createAccountLink).click();
    return new SignUpPage(driver);
}

}

MainPage Class:

public class MainPage {

    private WebDriver driver;

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

    @FindBy(xpath = "//a[text()='Sign in']")
    private WebElement signInButton;

    @FindBy(xpath = "//a[text()='Sign up']")
    private WebElement signUpButton;

    @FindBy(xpath = "//input[@id='user[login]']")
    private WebElement userLoginInput;

    @FindBy(xpath = "//input[@id='user[email]']")
    private WebElement userEmailInput;

    @FindBy(xpath = "//input[@id='user[password]']")
    private WebElement userPassword;

    @FindBy(xpath = "//button[text()='Sign up for GitHub']")
    private WebElement signUpForGitButton;

    public LoginPage clickSignIn(){
        signInButton.click();
        return new LoginPage(driver);
    }

    public SignUpPage clickSignUp(){
        signUpButton.click();
        return new SignUpPage(driver);
    }

    public SignUpPage signUpForGitButton(){
        signUpForGitButton.click();
        return new SignUpPage(driver);
    }

    public MainPage typeUserName(String username){
        userLoginInput.sendKeys(username);
        return this;
    }

    public MainPage typeEmail(String email){
        userEmailInput.sendKeys(email);
        return this;
    }

    public MainPage typePassword(String password){
        userPassword.sendKeys(password);
        return this;
    }

    public SignUpPage register(String username, String email, String password){
        this.typeUserName(username);
        this.typeEmail(email);
        this.typePassword(password);
        this.clickSignUp();
        return new SignUpPage(driver);
    }

}

SignUpPage Class:

public class SignUpPage {

    private WebDriver driver;

    public SignUpPage(WebDriver driver) {

        this.driver = driver;
    }

    private By heading = By.xpath("//div[@class='setup-header setup-org']/h1");

    private By userNameInput = By.xpath("//input[@id='user_login']");

    private By userEmailInput = By.xpath("//input[@id='user_email']");

    private By userPasswordInput = By.xpath("//input[@id='user_password']");

    private By createAccountButton = By.xpath("//button[@id='signup_button']");

    private By mainError = By.xpath("//form[@id='signup-form']/div[@class='flash flash-error my-3']");

    private By nameError = By.xpath("//input[@id='user_login']/ancestor::dd/following-sibling::dd");

    private By emailError = By.xpath("//input[@id='user_email']/ancestor::dd/following-sibling::dd");

    private By passwordError = By.xpath("///input[@id='user_password']/ancestor::dd/following-sibling::dd");

    public SignUpPage inputUserName(String username){
        driver.findElement(userNameInput).sendKeys(username);
        return this;
    }

    public SignUpPage inputEmail(String email){
        driver.findElement(userEmailInput).sendKeys(email);
        return this;
    }

    public SignUpPage inputPassword(String password){
        driver.findElement(userPasswordInput).sendKeys(password);
        return this;
    }

    public SignUpPage registerInvalidCreds(String username, String password, String email){
        this.inputUserName(username);
        this.inputPassword(password);
        this.inputEmail(email);
        driver.findElement(createAccountButton).click();
        return new SignUpPage(driver);

    }

    public String getHeading(){
        return driver.findElement(heading).getText();
    }


    public String getMainError(){
        return driver.findElement(mainError).getText();
    }


    public String getnameError(){
        return driver.findElement(nameError).getText();
    }

    public String getemailError(){
        return driver.findElement(emailError).getText();
    }

    public String getpasswordError(){
        return driver.findElement(passwordError).getText();
    }


}

MainPageTest Class:

public class MainPageTest {

    private WebDriver driver;
    private MainPage mainPage;


    @Before
    public void setUp(){
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://github.com/");
        mainPage = new MainPage(driver);
    }

    @Test

    public void signInTest() {
            LoginPage loginPage = mainPage.clickSignIn();//returns Login page call methos sign in on main page
            String heading = loginPage.getHeadingText();
            Assert.assertEquals("Sign in to GitHub", heading);
    }

    @Test
    public void registerFailTest(){
        SignUpPage signUpPage = mainPage.register("testuser","[email protected]", "password");
        String error = signUpPage.getMainError();
        Assert.assertEquals("There were problems creating your account.", error);
    }

    @After
    public void tearDown(){
        driver.quit();
    }
}

Error in console:

java.lang.NullPointerException
    at MainPage.clickSignIn(MainPage.java:33)
    at MainPageTest.signInTest(MainPageTest.java:31)

java.lang.NullPointerException
    at MainPage.typeUserName(MainPage.java:48)
    at MainPage.register(MainPage.java:63)
    at MainPageTest.registerFailTest(MainPageTest.java:38)

Process finished with exit code -1

1 Answer 1

1

When you created the MainPageObject (mainPage = new MainPage(driver);) it was just inside the scope of the setUp method. Thus, when it ended, the object was sent to the Garbage Collector.

You would need to make this object an attribute of the MainPageTest object, the access it inside the tests: this.mainPage.clickSignIn()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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