簡體   English   中英

頁面工廠退貨 Selenium 中的 null

[英]Page factory returns null in Selenium

我有 Hooks.java 用於驅動程序 class,LoginPage.java 用於所有登錄頁面對象和步驟定義 ZABB1A2F2A29FDC402ABCB1A2F2A29FBC402。 我的頁面工廠 object 未在我的登錄 class 中初始化,由於 Z37A6259CC0C1DAE299A786648 驅動程序,它返回 null。 驅動程序 class 具有 @Before 標記並在所有類之前啟動,但驅動程序是 null。 當我使用 PageFactory.initElements(driver, LoginPage.class); 在鈎子 class 中,頁面工廠初始化並且程序成功運行。 我在 LoginPage 構造函數中使用它,只是想知道我做錯了什么。 為什么頁工廠返回 null? 如何使用頁面工廠。

Following is the error java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])" because "steps.LoginPage.txtUserNameBox" is null

請幫忙。 謝謝

Hooks.java 
package steps;


import Util.GetConfigProp;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java8.En;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;

import java.io.IOException;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class Hooks extends BaseUtil implements En {

    BaseUtil base;
    //    GetConfigProp prop;
    static GetConfigProp prop;

    static {
        try {
            prop = new GetConfigProp();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Hooks() {
        //Default constructor
        Before(() -> {
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.setBrowserName("chrome");
            cap.setPlatform(Platform.WINDOWS);
            cap.setCapability("marionette", true);
            ChromeOptions COptions = new ChromeOptions();
            COptions.merge(cap);

            String hubUrl = null; //get hub url from properties file
            hubUrl = prop.getHubUrl();
            base.driver = new RemoteWebDriver(new URL(hubUrl), COptions);

            base.driver.navigate().to(prop.getUrl()); //get url from properties file
            base.driver.manage().window().maximize();
            base.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//            PageFactory.initElements(driver, LoginPage.class);

        });
    }

    public Hooks(BaseUtil base) {
        this.base = base;
    }


    public WebDriver getDriver() {
        return base.driver;

    }

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

    }
}
LoginPage.java
    package steps;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class LoginPage extends BaseUtil {
    BaseUtil base;
    @FindBy(how = How.ID, using = "user-name")
    public static WebElement txtUserNameBox;

    @FindBy(how = How.ID, using = "password")
    public static WebElement txtPassword;

    @FindBy(how = How.ID, using = "login-button")
    public static WebElement btnLogin;

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


    public void login() {

        System.out.println("this is title" + base.driver.getTitle());
        System.out.println("this is getwindow" + base.driver.getWindowHandle());
        txtUserNameBox.sendKeys("standard_user");
        txtPassword.sendKeys("secret_sauce");
        btnLogin.click();
    }

public void loginPassword() {
   //to be implement

}
}
MyStepdefs8.java
    package steps;

import io.cucumber.java8.En;


public class MyStepdefs8 extends LoginPage implements En {

    BaseUtil base;
    LoginPage login = new LoginPage();

    public MyStepdefs8(BaseUtil base) {
        this.base = base;
    }

    public MyStepdefs8() {
        Given("the user is on landing page", () -> {
            login.login();
        });
        When("^the user clicks button$", () -> {
            System.out.println("this is second step");
            loginPassword();
        });
    }

}

在 class MyStepdefs8 中,當你創建 LoginPage 的 object 時,會初始化 LoginPage class 的默認構造函數。 因此,當您調用 WebElements 時,它會返回 null,因為它們尚未初始化。

LoginPage login = new LoginPage(driver);

在 MyStepdefs8 class 中創建登錄頁面的 object 時傳遞驅動程序實例。

LoginPage login = new LoginPage(driver);

否則,它正在初始化登錄頁面的默認構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM