2

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)

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;
 }

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

Jonathan
  • 395
  • 2
  • 8
  • 25
  • The only other thing i can think of is that private static WebElement element = null; is causing the problem – Jonathan Apr 23 '18 at 20:15
  • I'd bet that "driver" is null. What is the result if you put a System.out.println(driver); right before "element = driver.findElement(By.id("adminLogin"));" ? –  Apr 23 '18 at 20:22
  • checking to see 1 sec – Jonathan Apr 23 '18 at 20:24
  • and your right it is (null) – Jonathan Apr 23 '18 at 20:26
  • Okay, then you have to find out why. Unfortunately, I don't know enough about Selenium itself to give you detailed tips at this point. –  Apr 23 '18 at 20:30
  • Is there a way around this?I know that this is the problem the only solution is to make driver not null – Jonathan Apr 23 '18 at 20:31
  • Where in your code do you instantiate driver? Can you provide the interesting part? Without 'driver' you won't be able to do anything as every single method derives from it. –  Apr 23 '18 at 20:33
  • will post it i sec – Jonathan Apr 23 '18 at 20:46
  • I added to my previous post where I instantiate the driver it in my main class – Jonathan Apr 23 '18 at 20:51

1 Answers1

1

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;
    //...

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);

    // ...   
}

Instantiate it this way instead

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);

        // ...   
}

This should work.