问题
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?
回答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.
来源:https://stackoverflow.com/questions/49989155/java-lang-nullpointerexception-selenium-2-classes