51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1137|回复: 0
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 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[2]/span[1]/span/input")
     
    public WebElement login;  
    //密码
    @FindBy (xpath=".//*[@id='user_login']/div/div[2]/span[2]/span/input")
     
    public WebElement pw;  
    //登录
    @FindBy (xpath=".//*[@id='user_login']/div/div[2]/span[4]/input")

    public WebElement dl;  
   
   
   
// 业务逻辑和操作方法  
   
    //登录方法  
    public  void 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
===============================================




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-23 10:24 , Processed in 0.062551 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表