无法解决指针问题

时间:2019-06-18 12:04:12

标签: selenium-webdriver cucumber-java

当我为页面对象模式创建单独的类时,我会得到Null指针异常,但是当我为页面对象模式调用或未使用单独的类时,则一切正常。

我创建了initialze类作为基类,在对象模式类中出现了问题,因为我也创建了它们的构造函数和init,但仍然出现空指针异常

 public class InitializationOfBrowser {
       public  Properties readBrowserProperties = new Properties();
       public  WebDriver driver;
       public InitializationOfBrowser() {
        }
        public WebDriver browserInitialization() throws IOException {
        FileInputStream fileInputStreamForBrowserReading = new 
        FileInputStream("src\\main\\resources\\Browsers.properties");
            readBrowserProperties.load(fileInputStreamForBrowserReading);
            String Browser = readBrowserProperties.getProperty("browsers");
            System.out.println(Browser);
            switch(Browser){
                case "chrome" :       System.setProperty("webdriver.chrome.driver","browsers\\chromedriver.exe" );
                    driver = new ChromeDriver();
                    break;
                case "firefox" :
                    System.out.println(Browser);
                    break;
                case "IE" :
                    System.out.println(Browser);
                    break;
                default:
                    System.out.println("Browser should be chrome, firefox or IE");
            }
            return driver;
        }
        public String openUrl(){
        String url = readBrowserProperties.getProperty("url");
        return url;
        }
        }

    page object pattern class

        public class LoginPageObjectPattern {
        WebDriver driver;
        public LoginPageObjectPattern(WebDriver driver){
            //initializationOfBrowser = new InitializationOfBrowser();
            this.driver = driver;
            PageFactory.initElements(driver, this);
        }

        @FindBy( className = "ico-login")
        WebElement clickOnLoginLink;
        public WebElement getClickOnLoginLink()
        {
            return clickOnLoginLink;
        }

        @FindBy(xpath = "//div[@class='inputs']/input[@class='email']")
        WebElement enterEmail;
        public WebElement getEnterEmail()
        {
            return enterEmail;
        }

        @FindBy(xpath = "//div[@class='inputs']/input[@class='password']")
        WebElement enterPassword;
        public WebElement getEnterPassword(){
            return enterPassword;
        }

        @FindBy(xpath = "//div[@class='buttons']/input[@type='submit']")
        WebElement clickOnLoginButton;
        public WebElement getClickOnLoginButton(){
            return clickOnLoginButton;
        }}

    Stepdef class

        public class StepDefsForRegistraion extends InitializationOfBrowser{
        LoginPageObjectPattern loginPageObjectPattern;
        WebDriver driverw = driver;
        public StepDefsForRegistraion() throws IOException {

         //  loginPageObjectPattern = new LoginPageObjectPattern(driverw);   either initialze this constructor or in userClickOnLogin function see null pionter exception

        }
        @When("User open browser")
        public void user_open_browser() throws IOException {
          browserInitialization();
        }

        @Then("User goto url")
        public void user_goto_url() {
          driver.get(openUrl());
        }
        @And("^User is on correct web page$")
        public void userIsOnCorrectWebPage() throws Throwable {
           String expectedTitle = driver.getTitle();
           String actualTitle = "Demo Web Shop";
           if(expectedTitle.equals(actualTitle)){
               System.out.println("User is on correct web page");
           }
           Thread.sleep(5000);
        }
        @When("^User clicks on Login$")
        public void userClicksOnLogin() throws Throwable {
          loginPageObjectPattern = new LoginPageObjectPattern(driverw);
          loginPageObjectPattern.getClickOnLoginLink().click();
    //    WebElement loginlink = loginPageObjectPattern.getClickOnLoginLink();
    ////  loginlink.click(); also try this way but have same result of null pointer}

null pointer exception

enter code here

尝试其他方法摆脱这种情况,但我失败了。需要您的帮助

0 个答案:

没有答案