2

While i run this below code i am getting Null pointer exception.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class GoogleTestTestng {
    WebDriver driver;

    @BeforeMethod
    public void setup(){
        System.setProperty("webdriver.chrome.driver", "E:/chromedriver_win32/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://google.com");
    }
    @Test
    public void googleTitleTest() {
        String title = driver.getTitle();
        System.out.println(title);
    }
    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}

Thanks.

  • 3
    What was the exception? What have you tried already? – trashpanda Jan 3 at 9:25
4
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class GoogleTestTestng {
WebDriver driver;  //  This is your first problem.

@BeforeMethod
public void setup(){
    System.setProperty("webdriver.chrome.driver", "E:/chromedriver_win32/chromedriver.exe");
    WebDriver driver = new ChromeDriver();  //  This is your second problem.
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("http://google.com");
}
@Test
public void googleTitleTest() {
    String title = driver.getTitle();
    System.out.println(title);
}
@AfterMethod
public void tearDown() {
    driver.quit();
}
}

Now, an explanation of what happened here.

WebDriver driver;  //  This is your first problem.

What you did here is perfectly fine to do. In fact, I use this approach with my own automated tests. Your code is ready to initialize a WebDriver object.

Now, here is where you ran into an issue:

WebDriver driver = new ChromeDriver();  //  This is your second problem.

Which driver instance do you want to use here? You initialize one at the top of your code then declare it a second time here? As Rao, had mentioned, the fix for your problem is the following:

WebDriver driver;
<...More code here...>
driver = new ChromeDriver();

What you have done here is removed the ambiguity between both driver objects you had created by declaring it once, and then instantiating it once you are ready to use it.

Oh, and as a side note, I noticed that your implicit wait is for a very long time. Be careful with how long you set your timeouts for if execution speed is something you are striving for in your tests.

3

you're trying to call method of an object that does not exist.

Try this out

 driver= new ChromeDriver();

change in your code

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class GoogleTestTestng {
    WebDriver driver;

    @BeforeMethod
    public void setup(){
        System.setProperty("webdriver.chrome.driver", "E:/chromedriver_win32/chromedriver.exe");
        driver= new ChromeDriver(); //this portion i have changed
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://google.com");
    }
    @Test
    public void googleTitleTest() {
        String title = driver.getTitle();
        System.out.println(title);
    }
    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.