I am new to the automated tests so please, bear with me
I've been trying to execute this Selenium-generated test, but with no success. It seems that there is a problem with the Firefox Driver instantiation process. Here is a copy-paste of my Stack, followed by the test itself.
testUntitledTestCase caused an error: java.lang.NullPointerException
java.lang.NullPointerException
at SomaTeste.tearDown(SomaTeste.java:47)
It also seems that it never reaches the "driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)". It proceeds to the "driver.quit()"(line 47) right after executing "driver = new FirefoxDriver()".
By analyzing a breakpoint placed on line 47 (which is the "driver.quit()" one), I've confirmed that the "driver" variable is set to null, which explains why i'm getting the "NullPointerException", but why is it not being initialized in the first place ? I can't seem to find an answer to this.
Does anyone have any idea on what might be causing this?
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.junit.Test;
import static org.junit.Assert.*;
public class SomaTeste{
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
baseUrl = "https://www.katalon.com/";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitledTestCase() throws Exception {
driver.get("http://localhost:8080/SomarParcelas/");
driver.findElement(By.name("p1")).click();
driver.findElement(By.name("p1")).clear();
driver.findElement(By.name("p1")).sendKeys("21");
driver.findElement(By.name("p2")).click();
driver.findElement(By.name("p2")).clear();
driver.findElement(By.name("p2")).sendKeys("12");
driver.findElement(By.name("calcular")).click();
driver.findElement(By.xpath("//h1")).click();
assertEquals("O resultado foi 33", driver.findElement(By.xpath("//h1")).getText());
}
@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;
}
}
}