Setting up Python IDEs to work with Automa

Automa can be used as a library from any Python IDE which is available on the market. The main steps in configuring an IDE to work with Automa consist of

  1. instructing the IDE to use the correct Python interpreter and
  2. adding Automa's library.zip file to the IDE's PYTHONPATH.

Prerequisites

In order to use Automa with any Python IDE, you need to have installed 32 bit Python version 2.7.6 and downloaded the ZIP version of Automa to your machine. For instructions on how to obtain these dependencies, please consult our page Using Automa as a Python library.

The remainder of this page consists of the following sections:

Setting up PyDev to work with Automa

Configure PyDev's Python Interpreter

To configure PyDev to use the 32 bit Python 2.7.6 interpreter that Automa requires, start Eclipse(/PyDev) and click Window->Preferences->PyDev->Interpreter - Python. You should see the following window:

PyDev Python Interpreters Configuration

Enter "32 bit Python 2.7.6" as the Interpreter Name and browse for the python.exe executable of your 32 bit Python 2.7.6 Python interpreter. If you installed the interpreter at the default location, then your screen will look exactly like the following:

PyDev Python Interpreter Configuration

Confirm the Interpreter configuration with OK. PyDev will then prompt you with a dialog "Selection needed" for the folders to be added to your system pythonpath. Simply leave the default values and press OK. Finally, close the Preferences window by again confirming with OK.

Create a new PyDev Project

Now that we have told PyDev about the Python interpreter we want it to use, we will create a new Python project that uses this interpreter. In Eclipse(/PyDev), switch to the PyDev perspective (Window->Open Perspective->Other...->PyDev) to get the following window:

PyDev Eclipse window

In order to start, create a new PyDev Project (File->New->PyDev Project) with the settings set as on the screenshot below:

New PyDev Project

Please note the selected interpreter 32 bit Python 2.7.6, which is the interpreter we added to PyDev in the previous step. Click Finish to create the project.

Add Automa's libraries to the PyDev project's PYTHONPATH

The last step is to import the Automa library to the newly created project. To do so, right-click on the project directory in the PyDev Package Explorer and select Properties. Then click on the PyDev - PYTHONPATH item on the left and choose the External libraries tab. Click on the "Add zip/jar/egg" button, navigate to the directory that you extracted Automa.zip into and choose the library.zip file. Assuming that the contents of the Automa.zip archive were extracted to C:\Automa, the configuration window should then look like this:

PyDev Project Properties

Click the OK button and you're ready to write Python sripts which use Automa's API!

Create an example script

To verify that you have done everything correctly, right-click on the Project name in the PyDev Package Explorer, select New and File. Type in any name (for example, hello.py).

You can write the Python code in the newly created source file. PyDev will support you with the code auto-completion features when you start typing the commands:

PyDev Code Auto-Completion

Once you finish writing the script, for example:

PyDev Example
from automa import api as automa
from automa.api import ENTER, ALT, F4
from datetime import datetime
 
items = ['Hello', 'Automa', str(datetime.now())]
 
# launch Notepad
automa.start("notepad")
 
# write all the items
for item in items:
    automa.write(item)
    automa.press(ENTER)
 
# save the text file
automa.click("File", "Save")
automa.write("HelloAutoma.txt", into="File name")
automa.click("Save")
 
# close Notepad
automa.press(ALT + F4)

You can run it directly from PyDev by right-clicking on the source file in the PyDev Package Explorer and selecting Run As->Python Run:

PyDev Run Script

(Optional) Writing UI tests using PyDev and Automa

It is very easy to write UI tests using PyDev as well. Let's assume we wrote two tests for Notepad:

PyDev Test Code
from automa import api as automa
from automa.api import Button, MenuItem, ALT, F4
import unittest
 
class TestNotepad(unittest.TestCase):
    def setUp(self):
        automa.start("notepad")
 
    def test_menu(self):
        self.assertTrue(MenuItem("File").exists())
        # failing assertion
        self.assertTrue(MenuItem("This Will Fail").exists())
 
    def test_save_button_exists(self):
        automa.click("File", "Save")
        self.assertTrue(Button("Save").exists())
        automa.click("Cancel")
 
    def tearDown(self):
        automa.press(ALT + F4)

This test can be run using PyUnit directly from Eclipse:

PyDev Test Run

After the test execution is finished, the report is produced in the Console and in the PyUnit tab:

PyDev Test Console Report
PyDev Test PyUnit Report

Setting up Aptana Studio to work with Automa

Aptana Studio uses PyDev as the Python programming environment. To set up Aptana Studio to work with Automa, you can therefore follow the instructions for setting up PyDev & Automa, above.

Troubleshooting

If you get an ImportError when trying to import automa.api, there may be two possible causes. The first is a general import error that may be caused by Automa's library.zip file not being on the PYTHONPATH used by your IDE. To troubleshoot this problem, it may be useful to test whether you can import automa.api directly in the python.exe console, as described on our page Using Automa as a Python library.

The second possible cause for an ImportError is that your Python IDE is not using the correct Python interpreter. To test whether this is the case, please create an empty Python script in your IDE project and paste the following code into it:

import sys
 
def get_version_info_string():
	is_64_bit = sys.maxsize > 2 ** 32
	bit = "64" if is_64_bit else "32"
	major, minor, micro = sys.version_info[:3]
	return "%s bit Python version %d.%d.%d" % (
		bit, major, minor, micro
	)
 
print "You are running %s." % get_version_info_string()

When run, this script should output You are running 32 bit Python version 2.7.6.. If you get any other result, please check your IDE's and project's interpreter settings.