0

my aftermethod refuse to run I dont what i am doing wrong. i tried couple codes but i am still getiing java.lang.NullPointerException any suggestion ?

    System.setProperty("webdriver.chrome.driver","/Users/eshanmostafa/eclipse-workspace/TestNG/chrome_ios/chromedriver ");
    WebDriver driver = new ChromeDriver();  
    driver.manage().window().fullscreen();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
    driver.get("https://www.google.com");

    }
    


    
    

@Test
public void test() {

String title = driver.getTitle();
System.out.println(title);

}
    


@AfterMethod
public void closeBrowswer() {
    
    try{ Thread.sleep(5000);
        driver.quit();
       }catch (Exception e){
          System.out.println("Nothing to do with it");
          } }
}

1 Answer 1

0

I'm not quite sure, what you want to achieve, because your example looks a little incomplete. I completed it, with my guesses, to a working, simple selenium test:

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyTest {

    private WebDriver driver;

    @Before
    public void doBefore() {
        System.setProperty("webdriver.chrome.driver", "/opt/chromedriver/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().fullscreen();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        driver.get("https://www.google.com");

    }

    @Test
    public void test() {

        String title = driver.getTitle();
        System.out.println(title);

    }

    @After
    public void closeBrowswer() {

        try {
            Thread.sleep(5000);
            driver.quit();
        } catch (Exception e) {
            System.out.println("Nothing to do with it");
        }
    }
}

Some notes on this:

  1. @AfterMethod That's not part of JUnit, but an annotation of TestNG. As Is there a alternative of @AfterMethod in junit? shows, @After is the corresponding method in JUnit

  2. NullPointerException I guess, you get a NullPointerException in your code, because you assign the variable driver in a block - and not as a member variable of the class. It's hard to say, because the start of the block is missing, but there is a closing curly brace, so I assume, it's originally in a block. A variable in a code block is limited to that block. The variable drive in the "After"-method is then an other variable - the member variable of your class and probably this variable wasn't assigned with a value. So you get an NPE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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