我对自动化测试是个新手,所以请耐心等待
我一直在尝试执行这个由Selenium生成的测试,但没有成功。似乎Firefox驱动程序实例化过程有问题。下面是my Stack的复制-粘贴,后面是测试本身。
testUntitledTestCase caused an error: java.lang.NullPointerException
java.lang.NullPointerException
at SomaTeste.tearDown(SomaTeste.java:47)它似乎也从未到达"driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS)。在执行"driver =driver.quit FirefoxDriver()“之后,它立即前进到”driver()“(第47行)。
通过分析位于第47行的断点(即"driver.quit()“断点),我已经确认"driver”变量被设置为null,这就解释了为什么我要得到"NullPointerException",但是为什么它没有首先被初始化呢?我似乎找不到这个问题的答案。
有没有人知道这可能是什么原因?
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;
}
}
}发布于 2019-04-15 05:51:58
我无法评论,但当我在Windows上使用chrome驱动程序在Eclipse中运行您的代码时,它对我来说工作得很好。但是,当我使用firefox驱动程序时,它完全按照您所说的那样工作。它原来是我使用的selenium版本。我使用的是3.3.1,但3.9.1运行良好。
https://stackoverflow.com/questions/55680077
复制相似问题