您能建议,当测试运行正常时,在这种情况下我们该怎么办,但是在测试结束时,Selenium中存在错误并且堆栈跟踪很长?
package com.example.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AddGroupTests {
private WebDriver driver;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver","C:\\selenium-java-3.12.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testAddNewGroup() throws Exception {
openMainPage();
findFrame();
logIn();
goToGroupPage();
fillFormAndSubmit("Group1", "Head", "Foot");
verifyTextOnPage();
returnToGroupsPage();
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
}
@Test
public void testAddNewEmptyGroup() throws Exception {
openMainPage();
findFrame();
logIn();
goToGroupPage();
fillFormAndSubmit("", "", "");
verifyTextOnPage();
returnToGroupsPage();
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
}
private void verifyTextOnPage() {
try {
assertEquals("A new group has been entered into the address book.return to the group page", driver.findElement(By.xpath("//div[@id='content']/div")).getText());
} catch (Error e) {
verificationErrors.append(e.toString());
}
}
private void returnToGroupsPage() {
driver.findElement(By.linkText("group page")).click();
}
private void fillFormAndSubmit(String name, String header, String footer) {
clickAndClear("group_name", "group_name");
driver.findElement(By.name("group_name")).sendKeys(name);
clickAndClear("group_header", "group_header");
driver.findElement(By.name("group_header")).sendKeys(header);
clickAndClear("group_footer", "group_footer");
driver.findElement(By.name("group_footer")).sendKeys(footer);
driver.findElement(By.name("submit")).click();
}
private void goToGroupPage() {
driver.findElement(By.linkText("Группы")).click();
driver.findElement(By.name("new")).click();
}
private void logIn() {
driver.findElement(By.name("user")).clear();
driver.findElement(By.name("user")).sendKeys("admin");
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys("pass");
driver.findElement(By.xpath("//input[@value='Войти']")).click();
}
private void findFrame() {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("demobody"));
}
private void openMainPage() {
driver.get("https://www.softaculous.com/softaculous/demos/PHP_Address_Book");
}
private void clickAndClear(String locator, String locator2) {
driver.findElement(By.name(locator)).click();
driver.findElement(By.name(locator2)).clear();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
@SuppressWarnings("unused")
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
@SuppressWarnings("unused")
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
@SuppressWarnings("unused")
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;
}
}
}
堆栈跟踪如下:
java.lang.AssertionError: org.junit.ComparisonFailure: expected:<...to the address book.[]return to the group ...> but was:<...to the address book.[
]return to the group ...>
at org.junit.Assert.fail(Assert.java:88)
at com.example.tests.AddGroupTests.tearDown(AddGroupTests.java:112)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
at java.base/java.util.Iterator.forEachRemaining(Unknown Source)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.base/java.util.stream.ReferencePipeline.forEach(Unknown Source)
at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:83)
at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:74)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.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:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
试图清除所有导入并一一解决红色问题,但仍然存在故障。我看到消息:AssertionError,但是无法找到问题。有什么建议么?预先感谢
答案 0 :(得分:1)
AssertionError
发生在断言(您要验证的内容)失败时。当您收到AssertionError
时,并不表示您的代码失败,而是表示您要检查的条件不正确。
现在通常断言错误意味着您正在测试的东西存在一些问题。但这通常是因为您测试事物的方式有问题。
在您的示例中,堆栈跟踪非常有用:
java.lang.AssertionError: org.junit.ComparisonFailure: expected:<...to the address
book.[]return to the group ...> but was:<...to the address book.[
]return to the group ...>
您试图比较两个字符串,但是它们是不同的。有什么不同?突出显示:在您期望的字符串中("A new group has been entered into the address book.return to the group page"
测试中的verifyTextOnPage
),“ return”在点后立即出现,没有空格。实际文本显然在它们之间具有换行符(“返回”在换行符处继续)。这就是为什么比较失败的原因,也就是为什么您得到AssertionError
的原因。
请不要害怕错误消息并尝试理解它们:它们提供了有价值的信息。