Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将页面对象与bDD一起使用时的java.lang.nullpointerexception_Java_Selenium_Pageobjects - Fatal编程技术网

将页面对象与bDD一起使用时的java.lang.nullpointerexception

将页面对象与bDD一起使用时的java.lang.nullpointerexception,java,selenium,pageobjects,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 base

我在登录页面和实际步骤定义中得到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();
    }
}
包装步骤定义;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.testng.Assert;
导入cumber.api.DataTable;
导入cucumber.api.java.en.Given;
导入cumber.api.java.en.Then;
导入cucumber.api.java.en.When;
导入pages.LogInPage;
导入pages.MyDayAppTabsManager;
导入pages.basePage;
公共类SalesforceLogin{
私有网络驱动程序;
@给定(“^User登录到Salesforce应用程序$”)
public void user\u登录到\u Salesforce\u应用程序(DataTable userCredentials)抛出可丢弃{
//编写处理数据表的代码
列表数据=userCredentials.raw();
LogInPage.txtbx_用户名(驱动程序).sendKeys(data.get(0).get(0));
LogInPage.txtbx_密码(驱动程序).sendKeys(数据.get(0).get(1));
LogInPage.btn_登录(驱动程序)。单击();
}
}
我在收到nullpointerexception错误 LogInPage.txtbx_用户名(驱动程序).sendKeys(data.get(0).get(0)); 在LogInPage中,element=driver.findElement(By.id(“用户名”)); .我在初始化驱动程序时出错了,我想,任何想法

谢谢。

更改此行
WebDriver=new ChromeDriver()


driver=newchromedriver()

可能重复的程序应该花一些时间学习如何调试自己的程序。一旦您学习了这项关键技能,您将能够通过单步执行自己的代码轻松解决类似的问题。