The desktoptesting library can also be used as a python library, without the need of using the framework.
ldtp and python-ldtp packages are needed to run the tests based on the testing library. Both packages are available in universe. They are installed automatically when installing the desktop testing library through the packages.
Enabling Assistive Technologies
It is necessary to enable the GNOME Assistive Technologies. The process is described at https://wiki.ubuntu.com/Testing/Automation/LDTP#How%20to%20enable%20GNOME%20Assistive%20Technologies
- bzr branch lp:ubuntu-desktop-testing
- cd ubuntu-desktop-testing
- sudo python setup.py install
Once installed we are going to create our first script to test Ubuntu. Our script will open the Gedit and will write the string "Hello World!" in the main gedit window. We will comment below the meaning of each of the lines.
1 # Necessary if we want to track performance  2 from time import time() 3 4 # The main library in the testing-library for Ubuntu  5 from ubuntutesting.ubuntu import Gedit 6 7 try: 8 9 # Gedit is the main class for GEdit tests.  10 gedit = GEdit() 11 12 # Start performance test 13 start_time = time() 14 15 # Open Gedit application and wait for existance  16 gedit.open() 17 18 # Write the selected text  19 gedit.write_text("Hello World!") 20 stop_time = time() 21 22 elapsed = stop_time - start_time 23 24 # Log time performance  25 log ('elapsed_time: ' + str(elapsed), 'comment') 26 27 except LdtpExecutionError, msg: 28 raise
: LDTP is written mainly in C, but it has two wrappers in python, ldtp (http://ldtp.freedesktop.org/user-doc/index.html) and ooldtp. ooldtp is not yet documented.
- : Right now the performance is done through the time module. It will be changed, eventually, to the desktop-testing-library module.
: Import the desktop-testing-library. The latest documentation of the testing library API is available at http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/
- : The applications are subclasses of Application ([../DesktopTestingLibrary/Application]). Check the available applications at the testing library API documentation.
: Open (http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/ubuntutesting.ubuntu.GEdit-class.html) is a method inherit from the Application class. It will try to open the application through its menu and will wait for the main window to appear.
- : write_text is a method for the Gedit class, that writes the given text to the main buffer.
- : log is a function of ldtp that writes to the log.
The Application Class
The Application class is one of the main classes of the Desktop Testing Library. Its aim is to include all the common behavior of the Ubuntu applications. If the application you want to test is not yet in the library, you can still use the Application class.
To open any application you can use the open_and_check_menu_item module function (http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/ubuntutesting.ubuntu-module.html#open_and_check_menu_item). This function will be move to Application class in future releases of the desktop-testing-library. You need to give the name of the menu item and the name of the window. I.e. to open the Calculator application we would do:
1 # Main LDTP python wrappers  2 from ooldtp import * 3 from ldtp import * 4 from ldtputils import * 5 6 # The main library in the testing-library for Ubuntu  7 from ubuntutesting.ubuntu import * 8 9 try: 10 11 # Open the Calculator and wait for its main window 12 open_and_check_menu_item('Calculator', 'Calculator') 13 14 except LdtpExecutionError, msg: 15 raise
If the application fails to open, an error will be logged in the test log.
The Application class at least can:
- exit: Given an application, it tries to quit it. It will work only if the application has a 'Quit' menu item.
- save: Given an application, it tries to save the current document. It will work only if the application has a 'Save' menu item.
- write_text: Given an application it tries to write text to its current buffer.
- remap: It reloads the application map for the given context.
Please, check the complete documentation of the class at the API documentation
Application Specific Classes
One of the objectives of the desktop-testing-library is to provide a simple way to create new test cases for the most common applications in Ubuntu. This is done by providing classes that inherit from Application and that provide common tasks for the given application.
Right now we have classes for Gedit, Update Manager and GkSu. If you want to contribute extending these classes or adding new ones, please refer to the guide on how to extend the library ../HowToExtendTestingLibrary .
The GEdit class (http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/ubuntutesting.ubuntu.GEdit-class.html) provide common functionality for the Text Editor application.
We will provide an example on how to use the GEdit class through an example annotated script:
1 # Main LDTP python wrappers 2 from ooldtp import * 3 from ldtp import * 4 from ldtputils import * 5 6 # The main library in the testing-library for Ubuntu 7 from ubuntutesting.ubuntu import * 8 9 try: 10 11 # Gedit is the main class for GEdit tests. 12 gedit = GEdit() 13 14 # Open Gedit application and wait for existance 15 gedit.open() 16 17 # Write text to the buffer. Right now the file does not have a name yet. 18 gedit.write_text("This is a text") 19 20 # Exit the application without saving 21 gedit.exit() 22 23 # Open again the application 24 gedit.open() 25 26 # Write text to the buffer. 27 gedit.write_text("This is a random text") 28 29 # Save the file 30 gedit.save('tempfile.txt') 31 32 # Write more text to it 33 gedit.write_text('new text') 34 35 # Exit the application, saving the changes 36 gedit.exit(True) 37 38 # Open again the application 39 gedit.open() 40 41 # Write text to the buffer. 42 gedit.write_text("This is a random text") 43 44 # Exit the application saving with a new name 45 gedit.exit(True, 'newfile.txt') 46 47 except LdtpExecutionError, msg: 48 raise
PolicyKit is the class that wraps the behavior of the GkSu dialogs. This application is used by others when they need to gain sudo access.
The available methods for this class are:
PolicyKit class main constructor. You need to pass the password for administrative activities as argument.
- It waits for the pop up window asking for the password to appear. It returns 1, if the gksu window exists; or 0, otherwise.
- It enters the password in the text field and clicks enter.
- It exists the gksu window withouth entering the password.
For examples, check the UpdateManager section.
UpdateManager (http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/ubuntutesting.ubuntu.UpdateManager-class.html) is the class that wraps the behavior of the Update Manager application (Ubuntu's application that manages packages and distribution updates).
We will provide an example on how to use the UpdateManager class through an example annotated script:
1 # Main LDTP python wrappers 2 from ooldtp import * 3 from ldtp import * 4 from ldtputils import * 5 6 try: 7 # Start the update manager application 8 updateManager = UpdateManager('adminpassword') 9 updateManager.open() 10 11 # Check Updates and Install Updates use internally PolicyKit 12 # to gain sudo access 13 updateManager.check_updates() 14 updateManager.install_updates() 15 16 # Close the application 17 updateManger.close() 18 19 except LdtpExecutionError, msg: 20 raise
Refer to the class documentation for the rest to the available methods: http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/ubuntutesting.ubuntu.UpdateManager-class.html
The Check module provides different ways to check if a test has passed or fail (http://people.ubuntu.com/~ara/ldtp/doc/testing_module_doc/toc-ubuntutesting.check-module.html).
All the subclasses of Check will have a method called perform_test that will return PASS, if the test passes; or FAIL, otherwise.
The available check classes right now are:
Test check for file comparison. If two files are equal, the test passes. Let's have a look through an example:
1 from ooldtp import * 2 from ldtp import * 3 from ldtputils import * 4 5 # Import ubuntu and check modules 6 from ubuntutesting.ubuntu import * 7 from ubuntutesting.check import * 8 9 try: 10 11 # Open the Gedit application 12 test = GEdit() 13 test.open() 14 15 # Write the "Hello World!" string and 16 # save the file as "test.txt" 17 test.write_text("Hello World!") 18 test.save("test.txt") 19 test.exit() 20 21 stop_time = time() 22 23 # Compare the new file against an existing one that 24 # already contains the same string 25 testcheck = FileComparison("oracle.txt", "test.txt") 26 check = testcheck.perform_test() 27 28 # If the test fails, log the error 29 if check == FAIL: 30 log ('Files differ.', 'CAUSE') 31 log ('Files differ.', 'ERROR') 32 33 except LdtpExecutionError, msg: 34 raise