Фабрика объектов страницы - NullPointerException - PullRequest
0 голосов
/ 24 мая 2018

Использование объектной модели страницы и факторинга страницы для страницы входа, где я получаю объекты в LoginPage.java, а действия - в LoginScript.java.Я получаю исключение java.lang.NullPointerException в строке "Ele_UserNameEdit.clear ();"Пожалуйста, помогите проверить код.Спасибо.

Это мой Loginpage.java:

package com.cos.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

public class loginpage {
    public String Logout_Xpath = "//nav[@class=\"menu_right ng-tns-c1-0 ng-star-inserted\"]/a[3]/div/img";

    @FindBy(id = "email_input") 
    WebElement Ele_UserNameEdit;

    @FindBy(id = "email_input")
    WebElement USERNAME_ELE;

    @FindBy(id="password_input") 
    WebElement Ele_PasswordEdit;

    @FindBy(xpath = ".//*[@class='theme_button blue log_in_btn']")
    WebElement Ele_LoginButton;

    @FindBy(xpath = "html/body/ngb-modal-window/div/div/selectcountry/div/div/div[1]/div/div[1]/div/div/i")
    WebElement Ele_ThailandRadioButton; 

    @FindBy(xpath = "//div[@class=\"theme_button blue pointer\"]")
    WebElement Ele_ExploreThePortalButton;      

    @FindBy(xpath = "//div[@class=\"theme_button blue logout_btn\"]")
    WebElement Ele_LogoutConfirmationButton;    

    public void HomePage(WebDriver driver){
        PageFactory.initElements(driver, this);
    }

    public void enterUserName(String UserName){
        Ele_UserNameEdit.clear();
        Ele_UserNameEdit.sendKeys(UserName);
    }

    public void enterPassword(String Password){
        Ele_PasswordEdit.clear();
        Ele_PasswordEdit.sendKeys(Password);
    }

    public void clickOnLogin(WebDriver driver){
        try {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            Ele_LoginButton.click();
            Thread.sleep(4000);
            Ele_ThailandRadioButton.click();
            Thread.sleep(4000);
            Ele_ExploreThePortalButton.click();
            Thread.sleep(4000);
            WebElement coslogoutLink;
            coslogoutLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Logout_Xpath)));
            Assert.assertTrue(coslogoutLink.isDisplayed());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void clickOnLogout(WebDriver driver) {
        //Logout
        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebElement coslogoutLink;
        coslogoutLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Logout_Xpath)));
        Assert.assertTrue(coslogoutLink.isDisplayed());
        coslogoutLink.click();
        Ele_LogoutConfirmationButton.click();
        // close Fire fox
        driver.close();         
    }
}

Это мой LoginScript.java:

package com.cos.test;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.cos.actions.LoginActions;

import excel.ReadDataFromExcel;

public class LoginScript extends BaseClass {

    @Test(dataProvider = "readFromExcel")
    public void testLogin(String userName, String password) {
        LoginActions actions = new LoginActions();
        actions.login(driver, userName, password);
        actions.logout(driver);
    }

    @DataProvider(name = "readFromExcel")
    public Object[][] readDataFromExcel() throws Exception {
        Object[][] dataFromExcel = ReadDataFromExcel.readDataFromExcel();
        return dataFromExcel;
    }
}

1 Ответ

0 голосов
/ 24 мая 2018

Прежде всего, вы продублировали @FindBy(id = "email_input").

публичная страница входа в класс должна иметь конструктор, когда инициализация вызывается с new loginpage();

PageFactory.initElements(driver, this);

, поэтому, когда вы заходите на страницу входа login = newстраница авторизации();он не инициализирует Ваш PageFactory, потому что Вы сделали его методом, а не конструктором.

Так что попробуйте вот так

public LoginPage(WebDriver webDriver) { 
    super(webDriver); //this is if You extend driver from super-class
    PageFactory.initElements(webDriver, this);
}

Но просто добавьте код в конструктор в классе pagelogin.

Надеюсь, это полезно.

...