xuziyan 发表于 2018-4-20 15:15:40

运行报空指针异常,搞不懂

package ly.base;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class init {
   
    //打开浏览器,打开网址
    public static WebDriver init(String url){
      System.setProperty("webdriver.firefox.bin","E:\\auto\\Mozilla Firefox\\firefox.exe");
      WebDriver webdriver = new FirefoxDriver();
      webdriver.get(url);
      //放大浏览器
       // webdriver.manage().window().maximize();
      return webdriver;
    }
}

package ly.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {

    WebDriver driver ;

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

// 元素定位
    //账户
    @FindBy (xpath=".//*[@id='user_login']/div/div/span/span/input")
   
    public WebElement login;
    //密码
    @FindBy (xpath=".//*[@id='user_login']/div/div/span/span/input")
   
    public WebElement pw;
    //登录
    @FindBy (xpath=".//*[@id='user_login']/div/div/span/input")

    public WebElement dl;
   
   
   
// 业务逻辑和操作方法
   
    //登录方法
    publicvoid login(String username, String password){
      login.sendKeys(username);
      pw.sendKeys(password);
      dl.click();
      
    }
}


package ly.TestSuites;

import ly.base.init;
import ly.page.LoginPage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LoginPageTest {
    WebDriver driver;
   
   init init1 = null;

   
      @BeforeClass
public void beforeClass() throws InterruptedException {
      init1 = new init();
      init1.init("http://192.168.2.100/zhxg/index.do");
      Thread.sleep(2000);
         
      }
   
@Test
public void f() {
      LoginPage login = PageFactory.initElements(driver, LoginPage.class);
      login.login("admin", "1");
   
   
}

@AfterClass
public void afterClass() {
      System.out.println("loginTest finish");
}

}


FAILED CONFIGURATION: @BeforeClass beforeClass
java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy6.sendKeys(Unknown Source)
    at ly.page.LoginPage.login(LoginPage.java:38)
    at ly.TestSuites.LoginPageTest.beforeClass(LoginPageTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

SKIPPED CONFIGURATION: @AfterClass afterClass
SKIPPED: f

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================




页: [1]
查看完整版本: 运行报空指针异常,搞不懂