mirror of
https://github.com/Kingsrook/qqq-frontend-material-dashboard.git
synced 2025-07-18 05:10:45 +00:00
Add better support (hopefully that works in CI) for downloads; update this test to use that.
This commit is contained in:
@ -1,6 +1,11 @@
|
|||||||
package com.kingsrook.qqq.materialdashboard.lib;
|
package com.kingsrook.qqq.materialdashboard.lib;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
|
||||||
import com.kingsrook.qqq.materialdashboard.lib.javalin.QSeleniumJavalin;
|
import com.kingsrook.qqq.materialdashboard.lib.javalin.QSeleniumJavalin;
|
||||||
import io.github.bonigarcia.wdm.WebDriverManager;
|
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
@ -11,6 +16,7 @@ import org.openqa.selenium.Dimension;
|
|||||||
import org.openqa.selenium.WebDriver;
|
import org.openqa.selenium.WebDriver;
|
||||||
import org.openqa.selenium.chrome.ChromeDriver;
|
import org.openqa.selenium.chrome.ChromeDriver;
|
||||||
import org.openqa.selenium.chrome.ChromeOptions;
|
import org.openqa.selenium.chrome.ChromeOptions;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -54,7 +60,15 @@ public class QBaseSeleniumTest
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void beforeEach()
|
public void beforeEach()
|
||||||
{
|
{
|
||||||
|
manageDownloadsDirectory();
|
||||||
|
|
||||||
|
HashMap<String, Object> chromePrefs = new HashMap<>();
|
||||||
|
chromePrefs.put("profile.default_content_settings.popups", 0);
|
||||||
|
chromePrefs.put("download.default_directory", getDownloadsDirectory());
|
||||||
|
chromeOptions.setExperimentalOption("prefs", chromePrefs);
|
||||||
|
|
||||||
driver = new ChromeDriver(chromeOptions);
|
driver = new ChromeDriver(chromeOptions);
|
||||||
|
|
||||||
driver.manage().window().setSize(new Dimension(1700, 1300));
|
driver.manage().window().setSize(new Dimension(1700, 1300));
|
||||||
qSeleniumLib = new QSeleniumLib(driver);
|
qSeleniumLib = new QSeleniumLib(driver);
|
||||||
|
|
||||||
@ -68,6 +82,57 @@ public class QBaseSeleniumTest
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
private void manageDownloadsDirectory()
|
||||||
|
{
|
||||||
|
File downloadsDirectory = new File(getDownloadsDirectory());
|
||||||
|
if(!downloadsDirectory.exists())
|
||||||
|
{
|
||||||
|
if(!downloadsDirectory.mkdir())
|
||||||
|
{
|
||||||
|
fail("Could not create downloads directory: " + downloadsDirectory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!downloadsDirectory.isDirectory())
|
||||||
|
{
|
||||||
|
fail("Downloads directory: " + downloadsDirectory + " is not a directory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
for(File file : CollectionUtils.nonNullArray(downloadsDirectory.listFiles()))
|
||||||
|
{
|
||||||
|
if(!file.delete())
|
||||||
|
{
|
||||||
|
fail("Could not remove a file from the downloads directory: " + file.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
protected String getDownloadsDirectory()
|
||||||
|
{
|
||||||
|
return ("/tmp/selenium-downloads");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
protected List<File> getDownloadedFiles()
|
||||||
|
{
|
||||||
|
File[] downloadedFiles = CollectionUtils.nonNullArray((new File(getDownloadsDirectory())).listFiles());
|
||||||
|
return (Arrays.stream(downloadedFiles).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** control if the test needs to start its own javalin server, or if we're running
|
** control if the test needs to start its own javalin server, or if we're running
|
||||||
** in an environment where an external web server is being used.
|
** in an environment where an external web server is being used.
|
||||||
|
@ -22,11 +22,16 @@
|
|||||||
package com.kingsrook.qqq.materialdashboard.tests;
|
package com.kingsrook.qqq.materialdashboard.tests;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import com.kingsrook.qqq.materialdashboard.lib.QBaseSeleniumTest;
|
import com.kingsrook.qqq.materialdashboard.lib.QBaseSeleniumTest;
|
||||||
import com.kingsrook.qqq.materialdashboard.lib.javalin.QSeleniumJavalin;
|
import com.kingsrook.qqq.materialdashboard.lib.javalin.QSeleniumJavalin;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.openqa.selenium.By;
|
import org.openqa.selenium.By;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -70,7 +75,7 @@ public class DashboardTableWidgetExportTest extends QBaseSeleniumTest
|
|||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
@Test
|
@Test
|
||||||
void testDashboardTableWidgetExport()
|
void testDashboardTableWidgetExport() throws IOException
|
||||||
{
|
{
|
||||||
qSeleniumLib.gotoAndWaitForBreadcrumbHeader("/", "Greetings App");
|
qSeleniumLib.gotoAndWaitForBreadcrumbHeader("/", "Greetings App");
|
||||||
|
|
||||||
@ -89,16 +94,18 @@ public class DashboardTableWidgetExportTest extends QBaseSeleniumTest
|
|||||||
.findElement(By.cssSelector("button"))
|
.findElement(By.cssSelector("button"))
|
||||||
.click();
|
.click();
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
qSeleniumLib.waitForCondition("Should have downloaded 1 file", () -> getDownloadedFiles().size() == 1);
|
||||||
// assert about the file that was downloaded - its name and some contents. //
|
File csvFile = getDownloadedFiles().get(0);
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
assertThat(csvFile.getName()).matches("Sample Table Widget.*.csv");
|
||||||
String latestFile = qSeleniumLib.getLatestChromeDownloadedFileInfo();
|
String fileContents = FileUtils.readFileToString(csvFile, StandardCharsets.UTF_8);
|
||||||
assertThat(latestFile).contains("Sample Table Widget");
|
assertEquals("""
|
||||||
assertThat(latestFile).contains(".csv");
|
"Id","Name"
|
||||||
assertThat(latestFile).contains("""
|
"1","Homer S."
|
||||||
"Id"%2C"Name"%0A"1"%2C"Homer S."%0A""");
|
"2","Marge B."
|
||||||
|
"3","Bart J."
|
||||||
|
""", fileContents);
|
||||||
|
|
||||||
qSeleniumLib.waitForever();
|
// qSeleniumLib.waitForever();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user