java.lang.NullPointerException Selenium 2类 [英] java.lang.NullPointerException Selenium 2 classes

查看:265
本文介绍了java.lang.NullPointerException Selenium 2类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本地计算机运行,而没有使用带有远程Web驱动程序的Selenium网格时,我的程序运行正常.但是,当我使用带有远程Web驱动程序的selenium网格设置相同的测试用例时.在日食中获取消息:

My program works fine when run from my local machine with out using selenium grid with Remote Web driver. However when i set up the same test cases using selenium grid with Remote Web driver . Get message in eclipse saying:

java.lang.NullPointerExceptionat PP_OBJ_Login.Adminlogin(PP_OBJ_Login.java:38)
at PP_Main.step01_Login(PP_Main.java:86)

现在我知道上面的意思是第38行和第86行是这两个类中的问题所在,我的问题是我不知道为什么在将硒网格与远程Web驱动程序一起使用时为什么会发生这种情况.

Now I know the above means that line 38 and line 86 is where the problem is in both classes my problem is i don't know why this is happening when I use selenium grid with Remote Web driver.

public class PP_Main {

     private static WebDriver driver;
     private static String homeUrl;
     //private String homeTitle ="Google";
     @SuppressWarnings("unused")
     private boolean acceptNextAlert = true;
     private static StringBuffer verificationErrors = new StringBuffer();


     @BeforeClass
     public static void setUp() throws Exception {

      //----------This works and envokes IE browser -------
      System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
      DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
      cap.setCapability(CapabilityType.BROWSER_NAME, DesiredCapabilities.internetExplorer());
      cap.setBrowserName("internet explorer");
      cap.setPlatform(Platform.ANY);
      RemoteWebDriver driver = new RemoteWebDriver(new URL("http://51.19.210.111:5555/wd/hub"), cap);
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      String url = "https://wfn-iat.adp.com/public/index.htm";
      driver.get(url);

     }
  @Test
public void step01_Login() throws Exception {
 PP_OBJ_Login.AdminVisiable(driver);
 PP_OBJ_Login.Adminlogin(driver).click();-- -> line 86
 PP_OBJ_Login.UserName(driver).sendKeys("NorfolkAutoUser6@adp");
 PP_OBJ_Login.Submitbtn(driver).click();
 PP_OBJ_Login.Password(driver).sendKeys("iatiat01");
 Thread.sleep(2000);
 PP_OBJ_Login.Submitbtn(driver).click();
 Thread.sleep(5000);
}


PP_OBJ_Login.Java


public class PP_OBJ_Login {

 private static WebElement element = null;

 // WebElement Adminlogin
 public static WebElement Adminlogin(WebDriver driver) {-- -- -> Line 38
  element = driver.findElement(By.id("adminLogin"));
  return element;
 }

 // WebElement input Field
 public static WebElement UserName(WebDriver driver) {
  element = driver.findElement(By.id("USER"));
  return element;
 }

我希望它可以使用Selenium网格和远程Web驱动程序来工作.有什么办法可以解决空指针问题?

I want this to work using selenium grid and remote web driver. Is there any way to resolve the null pointer issue?

推荐答案

您的问题是,您将'driver'定义为类成员,但没有实例化它.所以它一直都是空的.

Your Problem is, that you define 'driver' as a class member but you do not instantiate it. So it is null all the time.

 public class PP_Main {

    private static WebDriver driver;
    private static String homeUrl;
    //...

并且您在setUp()中实例化的驱动程序仅在方法本身内部有效.尽管名称完全相同,但它不是您全局定义的驱动程序".

And the driver you instantiate inside setUp() is only valid inside the method itself. Although it has exactly the same name it is NOT the 'driver' you defined globally.

@BeforeClass
public static void setUp() throws Exception {

    // ...

    cap.setPlatform(Platform.ANY);
    RemoteWebDriver driver = new RemoteWebDriver(new URL("http://51.19.210.111:5555/wd/hub"), cap);

    // ...   
}

以这种方式实例化

public class PP_Main {

    private static RemoteWebDriver driver;
    private static String homeUrl;
    //...


   @BeforeClass
   public static void setUp() throws Exception {

        // ...

        cap.setPlatform(Platform.ANY);
        driver = new RemoteWebDriver(new URL("http://51.19.210.111:5555/wd/hub"), cap);

        // ...   
}

这应该有效.

这篇关于java.lang.NullPointerException Selenium 2类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆