NullPointerException for driver when try to use RemoteWebDriver

1,635 views
Skip to first unread message

Artem Pol

unread,
Dec 21, 2014, 7:08:54 AM12/21/14
to seleniu...@googlegroups.com
Hi there,

I have the same issue, like there https://code.google.com/p/selenium/issues/detail?id=7055
But still can't to resolve it.

I have simple set of code, that is perfectly working when I use  driver = new ChromeDriver();
But when I changed code for:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);


I got NPE for this line: driver.get("https://www.google.com/");

Could you please tell me where is the problem?

PS: Browser is launching

Thank you!


NPE and code are below:

java.lang.NullPointerException
    at imagesearch.getFirstImage(imagesearch.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Here is the code:

public class imagesearch {

    public WebDriver driver;    //Create an instance of ChromeDriver
    @Before

    public void setUp() throws Exception {
 // driver = new ChromeDriver();
       DesiredCapabilities capabilities = DesiredCapabilities.chrome();
       WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void getFirstImage() throws Exception {
        //Open Home Page
        driver.get("https://www.google.com/");
        Thread.sleep(5000);
        driver.findElement(By.name("q")).sendKeys("nature");
        driver.findElement(By.id("gbqfb")).click();

        WebDriverWait wait = new WebDriverWait(driver, 5);

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}

Krishnan Mahadevan

unread,
Dec 24, 2014, 10:04:16 AM12/24/14
to Selenium Users
The problem is in your code.

public class imagesearch {

    public WebDriver driver;    //Create an instance of ChromeDriver
    @Before

    public void setUp() throws Exception {
       DesiredCapabilities capabilities = DesiredCapabilities.chrome();
       driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities); // I have altered this line
       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void getFirstImage() throws Exception {
        //Open Home Page
        driver.get("https://www.google.com/");
        Thread.sleep(5000);
        driver.findElement(By.name("q")).sendKeys("nature");
        driver.findElement(By.id("gbqfb")).click();

        WebDriverWait wait = new WebDriverWait(driver, 5);

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/a0c2784c-8ddf-485f-8704-4155e1659929%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rahul bhandwalkar

unread,
Mar 27, 2023, 4:25:09 AM3/27/23
to Selenium Users

package com.testng.examples1;

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

public class realexample1 {

public WebDriver driver;

@Test

public void testcase8()

{

assertEquals(true, driver.findElement(By.xpath("//*[text()='Last Name:']")).isDisplayed());

}

@Test

public void testcase9()

{

assertEquals(true, driver.findElement(By.xpath("//*[text()='Company:']")).isDisplayed());

}

@Test

public void testcase10()

{

assertEquals(true, driver.findElement(By.xpath("//*[text()='First Name:']")).isDisplayed());

}

@BeforeMethod

public void login()

{

System.out.println("Login");

//driver.findElement(By.name("user_name")).clear();

driver.findElement(By.name("user_name")).sendKeys("admin");

driver.findElement(By.name("user_password")).sendKeys("admin");

driver.findElement(By.name("Login")).click();

driver.findElement(By.linkText("New Lead")).click();

}

@AfterMethod

public void logout()

{

System.out.println("logout");

driver.findElement(By.linkText("Logout")).click();

}

@BeforeClass

public void LaunchApp()

{

System.out.println("LaunchApp");

System.setProperty("webdriver.chrome.driver", "C:/Selenium/SeleniumSoftware/chromedriver_win32/chromedriver.exe/");

ChromeOptions options = new ChromeOptions();

options.addArguments("--remote-allow-origins=*");

ChromeDriver driver = new ChromeDriver(options);

// driver = new ChromeDriver();

driver.get("http://localhost:100/index.php");

driver.manage().window().maximize();

}

@AfterClass

public void CloseApp()

{

System.out.println("CloseApp");

}

}

Reply all
Reply to author
Forward
0 new messages