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
-
First Release Candidate for Linux Kernel 6.14 Now Available
Linus Torvalds has officially released the first release candidate for kernel 6.14 and it includes over 500,000 lines of modified code, making for a small release.
-
System76 Refreshes Meerkat Mini PC
If you're looking for a small form factor PC powered by Linux, System76 has exactly what you need in the Meerkat mini PC.
-
Gnome 48 Alpha Ready for Testing
The latest Gnome desktop alpha is now available with plenty of new features and improvements.
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.