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.


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).

Figure 2: The script instructs LibreOffice to open all complex files.

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.

Figure 3: The script output for a complete test run.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • LibreOffice Macros with ScriptForge

    ScriptForge helps you automate LibreOffice by building portable macros.

  • Text to Speech

    Visually impaired users often find working with text and tables in office suites difficult. Pico TTS, a text-to-speech synthesizer, and the Read Text extension for LibreOffice and OpenOffice provide a solution.

  • LibreOffice 7 Now Available

    The LibreOffice 7 office suite is now available with important compatibility improvements.

  • The Clear Choice

    While LibreOffice and OpenOffice have a shared past, LibreOffice outstrips OpenOffice in contributors, code commits, and features.

  • LibreOffice 3.5

    PowerPoint shapes. We investigate how the two office modules work together.

comments powered by Disqus
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.

Learn More