Add a ImportVariousAndSavePdfWithOcr winappdriver test

This commit is contained in:
Ben Olden-Cooligan 2022-07-30 21:41:07 -07:00
parent ee887a475e
commit 354895bab8
5 changed files with 81 additions and 2 deletions

View File

@ -63,4 +63,18 @@ public class AppiumTests : ContextualTests
{
ClickAt(_session.FindElementByName(name));
}
protected void DoubleClickAt(WindowsElement element)
{
#pragma warning disable CS0618
// This is apparently obsolete, but the "correct" code is 10x as complicated so whatever
_session.Mouse.MouseMove(element.Coordinates);
_session.Mouse.DoubleClick(element.Coordinates);
#pragma warning restore CS0618
}
protected void DoubleClickAtName(string name)
{
DoubleClickAt(_session.FindElementByName(name));
}
}

View File

@ -0,0 +1,58 @@
using System.Threading;
using NAPS2.App.Tests.Verification;
using NAPS2.Sdk.Tests;
using NAPS2.Sdk.Tests.Asserts;
using Xunit;
namespace NAPS2.App.Tests.Appium;
[Collection("appium")]
public class ImportAndSaveTests : AppiumTests
{
[VerifyFact(AllowDebug = true)]
public void ImportVariousAndSavePdfWithOcr()
{
File.WriteAllBytes(Path.Combine(FolderPath, "word.pdf"), PdfResources.word_generated_pdf);
File.WriteAllBytes(Path.Combine(FolderPath, "image.pdf"), PdfResources.image_pdf);
File.WriteAllBytes(Path.Combine(FolderPath, "text.jpg"), BinaryResources.ocr_test);
var tessdata = Path.Combine(FolderPath, "components", "tesseract-4.0.0b4", "fast");
Directory.CreateDirectory(tessdata);
File.WriteAllBytes(Path.Combine(tessdata, "eng.traineddata"), BinaryResources.eng_traineddata);
ClickAtName("Import");
DoubleClickAtName("word.pdf");
ResetMainWindow();
Thread.Sleep(100);
ClickAtName("Import");
DoubleClickAtName("image.pdf");
ResetMainWindow();
Thread.Sleep(100);
ClickAtName("Import");
DoubleClickAtName("text.jpg");
ResetMainWindow();
Thread.Sleep(100);
ClickAtName("OCR");
ClickAtName("Make PDFs searchable using OCR");
ClickAtName("OK");
ClickAtName("Save PDF");
ResetMainWindow();
var fileNameElements = _session.FindElementsByName("File name:");
var fileTextBox = fileNameElements.Last();
ClickAt(fileTextBox);
fileTextBox.SendKeys("test.pdf");
ClickAtName("Save");
// Wait for the save to finish
WaitUntilGone("Cancel");
var path = Path.Combine(FolderPath, "test.pdf");
PdfAsserts.AssertImages(path,
PdfResources.word_p1,
PdfResources.word_p2,
ImageResources.color_image,
ImageResources.ocr_test);
PdfAsserts.AssertContainsText("ADVERTISEMENT.", path);
AppTestHelper.AssertNoErrorLog(FolderPath);
}
}

View File

@ -56,7 +56,7 @@ public static class Paths
public static string Components => EnsureFolderExists(ComponentsPath);
/// <summary>
/// Safely deletes the NAPS2 temp folder. If other NAPS2 or NAPS2.Console processes are running, the folder will be left alone.
/// Safely clears the NAPS2 temp folder. If other NAPS2 or NAPS2.Console processes are running, the folder will be left alone.
/// </summary>
public static void ClearTemp()
{
@ -70,6 +70,7 @@ public static class Paths
if (!otherNaps2Processes.Any())
{
Directory.Delete(TempPath, true);
Directory.CreateDirectory(TempPath);
}
}
catch (Exception e)

View File

@ -402,6 +402,7 @@ public class DesktopController
public void Import()
{
// TODO: Merge this into exporthelper/dialoghelper?
var ofd = new OpenFileDialog
{
Multiselect = true,
@ -419,6 +420,11 @@ public class DesktopController
MiscResources.FileTypePng + @"|*.png|" +
MiscResources.FileTypeTiff + @"|*.tiff;*.tif"
};
if (Paths.IsTestAppDataPath)
{
// For UI test automation we choose the appdata folder to find the prepared files to import
ofd.InitialDirectory = Paths.AppData;
}
if (ofd.ShowDialog() == DialogResult.OK)
{
ImportFiles(ofd.FileNames);

View File

@ -11,7 +11,7 @@ using PdfSharp.Pdf.IO;
namespace NAPS2.ImportExport.Pdf;
// TODO: We should have a "nicer" name (PdfImporter) for SDK users, or maybe have this be internal and have the public ScannedImageImporter (which should also maybe be renamed...)
// TODO: Add tests
// TODO: Add tests (specifically, currently it seems like importing a NAPS2 pdf is wrong, doesn't extract the image)
public class PdfSharpImporter : IPdfImporter
{
static PdfSharpImporter()