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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
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.