Test LibreOffice automatically
Initial Inspection
After LibreOffice has filled the file, a check can be performed to see whether the contents meets the expectations. The script unceremoniously imports the contents of the temporary text file in line 77 using txtFile.read()
. Line 78 then compares the contents of the file with the string Hello world!\n
. Only when the text matches, the script acknowledges this with an OK: ...
message (line 82) if the text matches. Otherwise, it generates a FAIL: ...
message in line 79 and sets the exitCode
variable to the value 1
.
The script terminates this first test run by closing the document in line 84 with the close()
method. The False
parameter prevents LibreOffice from asking the user whether it should save the document before closing.
Appearance
Administrators may rightly hope that this trivial test doesn't fail. The script now uses the previously established techniques in the last section to make a LibreOffice update within the given scenario – a number of departments with different Office form types – as risk free as possible.
The script again uses the simple text comparison; however, the check to see if the layout is faulty is far more difficult to automate. It is probably only possible to capture the correct layout using screenshots. And, the overhead means it doesn't really make sense to compare a document screenshot with a fixed target image. This test would also be extremely fragile. Because each pixel of the LibreOffice display naturally doesn't need to stay exactly the same from update to update.
The human eye is much better at detecting layout issues. The script therefore uses a shared approach to reasonably balance out overhead and benefit: Only the text contents of the files is subjected to the automated test. It generates a PDF that can be used for manually checking the layout.
It is important for this second test that admins put together the central forms from the various departments in a folder on the hard disk beforehand. The path to the appropriate directory can be passed to the script when called as a single argument on the command line. Only when this argument is present and points to an existing directory does the script perform the second test (line 87).
The script sets up the absolute path to the test directory in the testdir
variables in lines 88 to 92 from Listing 1. It also creates another folder in this directory. This folder is called Result_Files
and the script stores it in the resultdir
variables (line 89). If such a directory should already exist, line 91 deletes it using rmtree()
. The script then recreates the directory (line 92).
As LibreOffice also generates PDF files in this example, lines 95 to 97 prepare a corresponding PropertyValue
object which now has the value writer_pdf_export
instead of Text
.
In the following, the script assumes that the administrator has added the prefix Test_'
to all the forms to be tested in the test directory. With the function glob()
on line 99, the script now gets all files that match the naming scheme. The previously described function desktop.loadComponentFromURL()
on line 102 now loads all of the files. This time, however, the first argument is a reference to an already existing Office file. The URL creates it by prefixing file://
to the absolute path of the test file.
LibreOffice again opens a new window and manages the file there. The document
variable again manages access to this window. LibreOffice then automatically detects from the contents within the file to be loaded whether a Writer, Calc, or Impress window is to be opened. If the administrator comments out the --headless
option at the beginning of the script file, LibreOffice shows the file (Figure 2).
This time, however, the script doesn't write anything in the document but just exports it in two different formats. To do so, it generates the shortened file name for the test form in line 109 with path.basename(testfile)[5:-4]
. For example Department1_OrderForm
from Test_Department1_OrderForm.doc
.
Now the script stores the original form in the Result_Files
folder as PDF_Department1_OrderForm.pdf
(lines 111 to 112) and under Text_Department1_OrderForm.txt
(lines 114 to 116) in the appropriate formats.
If the admin has placed a particular matched text file in the actual test directory in addition to the forms to be tested – for example, Text_Department1_OrderForm.txt
– the script checks this file with the appropriate export in the results folder. This is done in line 120 with the aid of the filecmp.cpm
function. If the content fits, line 121 issues an OK:...
message (Figure 3). If there are differences, this results in a FAIL: ...
message in line 123. In this case, the exit code for the script is also set to 1
, that is, not a success.
If the administrator hasn't placed a text file in the test folder, the script notes this on line 126 with the message --: No comparison file for Test_Department1_OrderForm.doc
. Note that the easiest way to generate the text files is by running the script with the old version of LibreOffice before performing the update and copying the text file generated from the result folder into the test folder. The PDF is intended for the admin or the appropriate departments. Layout problems with the new version of LibreOffice should be easily identifiable using an expression.
The second section of the test ends every form test to close the associated open windows in line 127 with document.close()
.
Final Examination
Finally, the whole script ends by closing the LibreOffice desktop in line 131 with desktop.terminate()
. The script waits a little (line 135) until LibreOffice has properly finished. It then concludes the test run on line 137 and returns an exit code of
(for successful) – if all the tests were completed successfully. Otherwise, the exit code 1
indicates that one or more tests failed.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.