samedi 27 février 2021

java.lang.NullPointerException in Selenium Webdriver test

I am trying to better understand Selenium Webdriver, and writing some code based on instructions from a short training I did last year. The idea is to write a test that will check if Google browser opens up and searches for a word. The code below does not work as I get the java.lang.NullPointerException for WebElement in my InteliJ. Not sure what is wrong, I would appreciate some advice:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class startOverStartGoogle {
WebDriver driver;

@Before
public void beforeTest() {
System.setProperty("webdriver.chrome.driver", 
"C:\\Users\\karol\\OneDrive\\Pulpit\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://google.co.uk");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
driver.navigate().refresh();
System.out.println(driver.getTitle());

}
@Test
public void googleSearch() {
**WebElement** typeYourSearch = driver.findElement(By.cssSelector("a4bIc"));
typeYourSearch.sendKeys("Scotland");
**List<WebElement>** searchList = driver.findElements(By.cssSelector("q"));
assertThat(searchList.size()).isGreaterThanOrEqualTo(10);
}
@After
public void endTest(){driver.close();}
}

Aucun commentaire:

Enregistrer un commentaire