-1

My code:


package test;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class TestNGgooglesearch {
    WebDriver driver = null;
        
    @BeforeTest
public static void setuptest(){
        
        //String projectpath = System.getProperty("user.dir");
        
    System.setProperty("webdriver.chrome.driver", "D:\\TEST CASES\\real tc\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    // driver.manage().window().maximize();
    driver.get("https://google.com");
}

@Test
public void googlesearch(){
    
    driver.findElement(By.xpath("//input[@class='gLFyf gsfi']")).sendKeys("automation step by step");
    driver.findElement(By.name("btnK")).sendKeys(Keys.RETURN);


//driver.findElement(By.name("q")).sendKeys("Automation step by step");
//driver.findElement(By.name("btnK")).sendKeys(Keys.RETURN);
//driver.findElement(By.name("btnK")).click();  
}
@AfterTest
public void tearDownTest(){
    
    driver.close();
    driver.quit();
    System.out.println("test successfully completed");
}
}

Error I'm getting:

FAILED CONFIGURATION: @AfterTest afterTest1
java.lang.NullPointerException
    at test.NewTest2Test.afterTest1(NewTest2Test.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
    at org.testng.TestRunner.afterRun(TestRunner.java:725)
    at org.testng.TestRunner.run(TestRunner.java:509)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: afterTest
java.lang.NullPointerException
    at test.NewTest2Test.afterTest(NewTest2Test.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================

1
  • 1
    Change WebDriver driver = new ChromeDriver(); in your setuptest to driver = new ChromeDriver();
    – Alexey R.
    Aug 24 '20 at 7:23
0

Some comments from my side:

  • @BeforeTest and @AfterTest should be in base class. Your test class extends base class and re-use the driver
  • Set up chromedriver as environment configuration, so that it does not rely on its location and not store in code
  • when this error (Failed configuration) is fixed, another will appear - it cannot find the 'Search' button. It is because two elements on the page with the same name. Try to use xpath or css to find unique element

Please find the code below. This test passes for me:

BaseTest.java - this is abstract class:

package com.test.base;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public abstract class BaseTest {

    public RemoteWebDriver driver;

    @BeforeMethod(alwaysRun = true)
    public void setUp()  {
        driver = new ChromeDriver();
        driver.get("https://google.com");
    }

    @AfterMethod(alwaysRun = true)
    public void tearDown() throws Exception {
        if (driver != null)
            driver.quit();
        System.out.println("test successfully completed");
    }

}

GoogleSearch.java - class with tests

package test.search;


import com.test.base.BaseTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;


public class googleSearch extends BaseTest {

    @Test
    public void googleSearchTest(){

        driver.findElement(By.xpath("//input[@class='gLFyf gsfi']")).sendKeys("automation step by step");
        WebElement searchButton = driver.findElement(
                By.cssSelector("div[jsmodel] div[jscontroller] > div[jscontroller]  + div input[name='btnK']"));
        searchButton.click();

    }
}

Also, 'test successfully completed' will be printed regardless test passed or failed. It should be changed to use assertions.

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