Skip to content

Creating custom Actions

guilhermechapiewski edited this page Sep 13, 2010 · 13 revisions

Pyccuracy comes with dozens of pre-defined actions to do many kinds of simple things like clicking on buttons, filling forms, navigating and so on.

You may also want to write your own custom Actions for many reasons:

  • Improve the readability of your tests;
  • Reuse of test code;
  • Synthesize pre-conditions (like the example below);
  • Do complex setups;
  • … and virtually anything that you could do with Python/Pyccuracy code.

For instance, suppose that you want to write a custom action that will be a pre-condition (Given statement) to be used in some of your tests the require a user to be logged in into the system, like this example:

As a user
I want to be able to search
So that I can find interesting content

Scenario 1 - Logged in search
Given
    I am logged in with username "test_account" and password "test"
    And I go to "/search.html"
When
    I see "search field" textbox
    And I fill "search field" textbox with "pyccuracy rocks"
    And I click "search" button and wait
Then
   I see "Search for pyccuracy rocks - My cool website" title

The following action is not bundled with Pyccuracy but you can easily implement it:

I am logged in with username "test_account" and password "test"

To do that, you will need to:

1) Implement a class that inherits from ActionBase

from pyccuracy.actions import ActionBase

class LoggedInAction(ActionBase):
    pass

2) Define the regular expression that will match your custom Action

from pyccuracy.actions import ActionBase

class LoggedInAction(ActionBase):
    regex = r'^(And )?I am logged in with username [\"](?P<username>.+)[\"] and password [\"](?P<password>.+)[\"]$'

Implement the execute method

Implement the execute method receiving the Pyccuracy context and the variables you defined in your regular expression as parameters (in this case, username and password):

from pyccuracy.actions import ActionBase

class LoggedInAction(ActionBase):
    regex = r'^(And )?I am logged in with username [\"](?P<username>.+)[\"] and password [\"](?P<password>.+)[\"]$'

    def execute(self, context, username, password):
        pass

Implement the Action

To finish, we will tell Pyccuracy to execute some regular actions inside the method body, to login into the website with the username and password informed in the test:

from pyccuracy.actions import ActionBase

class LoggedInAction(ActionBase):
    regex = r'^(And )?I am logged in with username [\"](?P<username>.+)[\"] and password [\"](?P<password>.+)[\"]$'

    def execute(self, context, username, password):
        self.execute_action(u"I go to \"/index.html\"", context)

        # if the user is not logged in already, we do the login process
        logged_in = False
        try:
            self.execute_action(u"And I see \"already have an account? sign in\" link", context)
        except ActionFailedError:
            logged_in = True

        if not logged_in:
            self.execute_action(u"And I click \"already have an account? sign in\" link", context)
            self.execute_action(u"And I wait for the page to load for 5 seconds", context)
            self.execute_action(u"And I fill \"username\" textbox with \"%s\"" % username, context)
            self.execute_action(u"And I fill \"password\" textbox with \"%s\"" % password, context)
            self.execute_action(u"And I click \"sign in\" button", context)

And that’s it!