使用带有bDD的页面对象时的java.lang.nullpointerexception

时间:2017-05-02 04:28:25

标签: java selenium pageobjects

我在登录页面和实际步骤定义中得到nullpointexception

我有一个基页:

package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org
.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class basePage {
    protected WebDriver driver;
@BeforeClass
public void setupApplication() 
{
             System.setProperty("webdriver.chrome.driver", "C:/DRIVERS/Selenium Drivers/chromedriver.exe");
             WebDriver driver = new ChromeDriver();
             driver.manage().window().maximize();
             driver.get("http://salesforce.com");
          }

@AfterClass
public void closeApplication()
{
    driver.quit();

}
}

然后我有一个登录页面:

package pages;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class LogInPage extends basePage {
    private static WebElement element = null;
      public static WebElement txtbx_UserName(WebDriver driver){
            element = driver.findElement(By.id("username"));
               return element;
             }

       public static WebElement txtbx_Password(WebDriver driver){
             element = driver.findElement(By.id("password"));
               return element;
                    }
       public static WebElement btn_LogIn(WebDriver driver){
              element = driver.findElement(By.id("Login"));
                    return element;
                    }
            }

我从步骤定义文件中调用它们:

package Step_Defnitions;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pages.LogInPage;
import pages.MyDayAppTabsManager;
import pages.basePage;

public class SalesforceLogin{

    private WebDriver driver;

@Given("^User login to Salesforce application$")
     public void user_login_to_Salesforce_application(DataTable userCredentials) throws Throwable {

 //Write the code to handle data table
   List<List<String>> data = userCredentials.raw();
   LogInPage.txtbx_UserName(driver).sendKeys(data.get(0).get(0));
   LogInPage.txtbx_Password(driver).sendKeys(data.get(0).get(1));
   LogInPage.btn_LogIn(driver).click();
    }
}

我收到了nullpointerexception错误 LogInPage.txtbx_UserName(驱动程序).sendKeys(data.get(0)获得(0)); 以及在元素= driver.findElement的LogInPage中(By.id(&#34; username&#34;)); 我在初始化驱动程序时遇到了一些错误,我认为,任何想法。

感谢。

1 个答案:

答案 0 :(得分:1)

更改此行        WebDriver driver = new ChromeDriver();

要         driver = new ChromeDriver();