Selenium-会话ID为空。调用quit()后使用WebDriver吗?仅执行一项测试

时间:2019-02-10 02:42:38

标签: selenium selenium-webdriver selenium-chromedriver

我正在尝试使用Junit用Selenium编写。它只是一项测试。测试正在运行,但我收到此失败消息- 1)testTripPlannerJUnit(com.example.tests.TripPlannerJUnit) org.openqa.selenium.NoSuchSessionException:会话ID为null。调用quit()后使用WebDriver吗? 我了解问题出在拆卸上,但是我该如何解决。

package com.example.tests;
import static org.junit.Assert.fail;
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.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class TripPlannerJUnit {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
  ChromeDriverManager.getInstance().setup();
  driver = new ChromeDriver();
  baseUrl = "https://www.google.com.au/.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testTripPlannerJUnit() throws Exception {
driver.get("https://transportnsw.info/trip#/");
driver.findElement(By.id("search-input-From")).click();
driver.findElement(By.id("search-input-From")).clear();
driver.findElement(By.id("search-input-From")).sendKeys("North Sydney Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='North Sydney Station'])[1]/following::li[1]")).click();
driver.findElement(By.id("search-input-To")).click();
driver.findElement(By.id("search-input-To")).clear();
driver.findElement(By.id("search-input-To")).sendKeys("Town Hall Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Town Hall Station'])[1]/following::ul[1]")).click();
driver.findElement(By.id("search-button")).click();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}

@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}


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;
}
}
}

1 个答案:

答案 0 :(得分:1)

@After中,您首先编写了driver.quit(),然后在驱动程序上遇到了if条件,接着是implicit wait,但是直到代码到达为止隐式等待行,则驱动程序实例已被driver.quit()行退出。

因此要更正它,请尝试:

@After
public void tearDown() throws Exception {
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
   }
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.quit();
}

刚刚将driver.quit()行移到了代码的末尾,并提出了另外一条建议,在您的@After中,您已经隐式等待了,但是您并未与其中的任何webElement进行交互,因此可以删除隐式的代码等待行,因为它不是必需的。