2

I have a large test suite to automate, with a lot of test cases. I followed the steps from many different tutorials on youtube but although the test case runs successfully to the end, Eclipse keeps pointing this error message: java.lang.nullpointerexception.

I'm using selenium to automate and then I export the file as a java/Junit/webdriver to eclipse; and then I remove the error due to file name at the top, just like on the tutorials.

So, this is the code: by the way, I had to change the code a bit. I don't know why, but selenium is recording my base URL as

baseUrl = "http://www(dot)vpl(dot)ca/"; 

and then it uses the "get()" like this

driver.get(baseUrl); 

so when the test runs, the url goes something like this:

http://www(dot)vpl(dot)ca// 

on the browser and I get an error. That's why I changed the code in order to make it run.

Well, this is how the test looks right now:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SearchForVirginia {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.vpl.ca/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSearchForVirginia() throws Exception {
    driver.get(baseUrl);
    driver.findElement(By.id("globalQuery")).clear();
    driver.findElement(By.id("globalQuery")).sendKeys("virginia wolf");
    driver.findElement(By.cssSelector("input.search_button")).click();
    driver.findElement(By.linkText("Virginia Wolf")).click();
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
     } finally {
      acceptNextAlert = true;
    }
  }
}

This is the stack trace:

nov 13, 2015 10:19:43 PM org.openqa.selenium.os.ProcessUtils killWinProcess
ADVERTÊNCIA: Process refused to die after 10 seconds, and couldn't taskkill it
java.lang.NullPointerException: Unable to find executable for: taskkill
at      com.google.common.base.Preconditions.checkNotNull(Preconditions.java:250)
    at org.openqa.selenium.os.UnixProcess.<init>(UnixProcess.java:62)
    at org.openqa.selenium.os.CommandLine.<init>(CommandLine.java:38)
    at org.openqa.selenium.os.WindowsUtils.killPID(WindowsUtils.java:178)
    at     org.openqa.selenium.os.ProcessUtils.killWinProcess(ProcessUtils.java:138)
    at  org.openqa.selenium.os.ProcessUtils.killProcess(ProcessUtils.java:81)
    at  org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.destroyHarder(UnixProcess.java:247)
    at org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.access$2(UnixProcess.java:246)
    at org.openqa.selenium.os.UnixProcess.destroy(UnixProcess.java:125)
    at org.openqa.selenium.os.CommandLine.destroy(CommandLine.java:155)
    at org.openqa.selenium.firefox.FirefoxBinary.quit(FirefoxBinary.java:259)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.quit(NewProfileExtensionConnection.java:204)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.quit(FirefoxDriver.java:364)
    at org.openqa.selenium.firefox.FirefoxDriver.stopClient(FirefoxDriver.java:310)
    at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:519)
    at B002.tearDown(B002.java:40)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
Pshemo
  • 113,402
  • 22
  • 170
  • 242
Natalia
  • 21
  • 2

0 Answers0