当我试图运行硒测试时,第一个测试似乎工作正常,但随后我在后续测试中收到了这个错误,我将测试切换到电子应用程序,因此不确定这是否是在导致问题之后:
org.openqa.selenium.NoSuchSessionException:会话ID为null。调用quit((后使用WebDriver?构建信息:版本:"3.141.59",修订:"e82be7d358",时间:"2018-11-14T08:17:03">
我的设置页面是:
public class SetUp extends TestRail {
private static ChromeDriver browser;
public static String urlHost;
public static String testEnv;
public static PageConfig pageConfig;
@Before
public void initialBrowser() {
if(browser == null) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("start-maximized");
options.addArguments("--disable-gpu");
options.addArguments("disable-infobars");
System.setProperty("webdriver.chrome.silentOutput", "true");
options.setBinary("C:/Users/ElectronApp.exe");
browser = new ChromeDriver(options);
//--------------------
pageConfig = new PageConfig(browser);
}
}
//method: To get the url configs for the environment used to run the test
//@parameter: dev or test. demo doesn't not have a separate front end url
public void beforeTest(@NotNull final String env) throws Exception {
testEnv = env;
System.out.println("TEST ENVIRONMENT: " + testEnv);
switch (env){
case "qa-84" :
urlHost = "http://ukdc1-docker-mx:84/qa/#/login";
break;
case "qa-85":
urlHost = "http://ukdc1-docker-mx:85/qa/#/login";
break;
default:
throw new Exception("Incorrect environment passed");
}
}
//method to close the browser after every cucumber scenario
@After
public void closeBrowser(Scenario scenario) throws IOException, APIException {
if(scenario.isFailed()){
try{
TakesScreenshot screenshot = (TakesScreenshot)browser;
File source = screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("logs/screenshots/" + scenario.getName() + ".png"));
System.out.println("Screenshot taken");
} catch (Exception e){
System.out.println("Exception while taking screenshot " + e.getMessage());
}
}
browser.quit();
writeResultToTestRail(scenario);
}
public void writeResultToTestRail(Scenario scenario) throws IOException, APIException {
String tesCaseID = new String();
for(String s: scenario.getSourceTagNames()){
if(s.contains("TestRail")){
int size = s.length();
int startOfID = s.indexOf('"');
tesCaseID = s.substring(startOfID + 1,size - 2);
}
Map data = new HashMap();
if(scenario.isFailed()){
data.put("status_id", 5);
data.put("comment", "Test Env: " + urlHost + "nn" + logError(scenario));
}else {
data.put("status_id", 1);
}
postTestCaseStatus("add_result_for_case/","3914", tesCaseID, data);
//3914
}
}
protected void getTestCaseDetailsInConsole(String testCaseId) throws IOException, APIException {
System.out.println(getTestCaseDetails(testCaseId));
}
private static String logError(Scenario scenario) {
try {
Class klass = ClassUtils.getClass("cucumber.runtime.java.JavaHookDefinition$ScenarioAdaptor" );
Field fieldScenario = FieldUtils.getField(klass, "scenario", true);
if (fieldScenario != null) {
fieldScenario.setAccessible(true);
Object objectScenario = fieldScenario.get(scenario);
Field fieldStepResults = objectScenario.getClass().getDeclaredField("stepResults" );
fieldStepResults.setAccessible(true);
ArrayList<Result> results = (ArrayList<Result>) fieldStepResults.get(objectScenario);
for (Result result : results) {
if (result.getError() != null) {
System.out.println(result.getError() + "nn" + result.getErrorMessage());
if(result.getErrorMessage().length() > 3100) {
return result.getErrorMessage().substring(0, 3100);
} else {
return result.getErrorMessage();
}
}
}
}
return "Fetching error logs from the scenario ran into some issue, please check jenkins logs";
} catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
return e.getMessage();
}
}
}
我的页面配置:
public class PageConfig {
public ChromeDriver browser;
public PageConfig(ChromeDriver browser){
this.browser = browser;
}
//method: Get instance method can be called on any class, it helps to avoid to add a line every time a new PO is added to the project.
//@parameter: ClassName.class
//@example: PageConfig.GetInstance(LoginPage.class).onLoginPage(); where onLoginPage is an method of
LoginPage.
public <TPage extends BasePage> TPage GetInstance (Class<TPage> pageClass){
try {
return initElements(browser, pageClass);
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
}
错误显示您的驱动程序在执行代码之前关闭,所以@After的代码直接执行,所以我认为您忘记在设置页面中使用@Test
,因为有@Before
和@After
,但我看不到@Test
。