CE-1068 - Add dumping console logs upon error - could help diagnose test fails faster hopefully

This commit is contained in:
2024-04-29 12:52:29 -05:00
parent 4c6955b6ed
commit e7d870a7fa
3 changed files with 154 additions and 4 deletions

View File

@ -33,6 +33,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
@ -43,6 +44,7 @@ import static org.junit.jupiter.api.Assertions.fail;
/*******************************************************************************
** Base class for Selenium tests
*******************************************************************************/
@ExtendWith(SeleniumTestWatcher.class)
public class QBaseSeleniumTest
{
protected static ChromeOptions chromeOptions;
@ -93,6 +95,8 @@ public class QBaseSeleniumTest
driver.manage().window().setSize(new Dimension(1700, 1300));
qSeleniumLib = new QSeleniumLib(driver);
SeleniumTestWatcher.setCurrentSeleniumLib(qSeleniumLib);
if(useInternalJavalin())
{
qSeleniumJavalin = new QSeleniumJavalin();
@ -197,10 +201,10 @@ public class QBaseSeleniumTest
qSeleniumLib.takeScreenshotToFile(getClass().getSimpleName() + "/" + testInfo.getDisplayName());
}
if(driver != null)
{
driver.quit();
}
////////////////////////////////////////////////////////////////////////////////////////
// note - at one time we did a driver.quit here - but we're moving that into //
// SeleniumTestWatcher, so it can dump logs if it wants to (it runs after the @After) //
////////////////////////////////////////////////////////////////////////////////////////
if(qSeleniumJavalin != null)
{

View File

@ -42,6 +42,8 @@ import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.assertj.core.api.Assertions.assertThat;
@ -735,4 +737,22 @@ public class QSeleniumLib
return (this);
}
/*******************************************************************************
**
*******************************************************************************/
public void dumpConsole()
{
Set<String> availableLogTypes = driver.manage().logs().getAvailableLogTypes();
for(String logType : availableLogTypes)
{
LogEntries logEntries = driver.manage().logs().get(logType);
for(LogEntry logEntry : logEntries)
{
System.out.println(logEntry.toJson());
}
}
}
}

View File

@ -0,0 +1,126 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2024. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.frontend.materialdashboard.selenium.lib;
import java.util.Optional;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
/*******************************************************************************
**
*******************************************************************************/
public class SeleniumTestWatcher implements TestWatcher
{
private static QSeleniumLib qSeleniumLib;
/*******************************************************************************
**
*******************************************************************************/
public static void setCurrentSeleniumLib(QSeleniumLib qSeleniumLib)
{
SeleniumTestWatcher.qSeleniumLib = qSeleniumLib;
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void testFailed(ExtensionContext context, Throwable cause)
{
if(qSeleniumLib != null)
{
System.out.println("Dumping browser console after failed test: " + context.getDisplayName());
System.out.println("----------------------------------------------------------------------------");
try
{
qSeleniumLib.dumpConsole();
}
catch(Exception e)
{
System.out.println("Error dumping console:");
e.printStackTrace();
}
System.out.println("----------------------------------------------------------------------------");
}
tryToQuitSelenium();
}
/*******************************************************************************
**
*******************************************************************************/
private void tryToQuitSelenium()
{
if(qSeleniumLib != null)
{
try
{
qSeleniumLib.driver.quit();
}
catch(Exception e)
{
System.err.println("Error quiting selenium driver: " + e.getMessage());
}
}
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void testSuccessful(ExtensionContext context)
{
tryToQuitSelenium();
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void testAborted(ExtensionContext context, Throwable cause)
{
tryToQuitSelenium();
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void testDisabled(ExtensionContext context, Optional<String> reason)
{
tryToQuitSelenium();
}
}