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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.