【问题标题】:Selenium- Session ID is null. Using WebDriver after calling quit()? Only one test in executionSelenium- 会话 ID 为空。调用 quit() 后使用 WebDriver?仅执行一项测试
【发布时间】:2019-02-10 12:14:46
【问题描述】:

我正在尝试使用 Junit 执行用 Selenium 编写的代码。它只是一项测试。测试运行,但我收到此失败消息 - 1) testTripPlannerJUnit(com.example.tests.TripPlannerJUnit) org.openqa.selenium.NoSuchSessionException:会话 ID 为空。调用 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;
}
}
}

【问题讨论】:

    标签: selenium selenium-webdriver selenium-chromedriver


    【解决方案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 交互,所以隐式等待行的代码可以删除,因为它不是必需的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-06
      • 2022-01-27
      • 2021-10-10
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多