Quantcast
Channel: froglogic
Viewing all articles
Browse latest Browse all 398

Squish tip of the week: Reuse script functions in BDD tests

$
0
0

With the introduction of BDD support, all the existing scripts aren’t lost – quite the contrary – existing scripts and functions still work as always, and now those functions can also be called by BDD tests.

Consider the following Test Suite Resource functions:

def invokeMenuItem(menu, item):
    activateItem(waitForObjectItem(":AB_QMenuBar", menu))
    activateItem(waitForObjectItem("{type='QMenu' \
    title='%s'}" % menu, item))
    
def addNameAndAddress(nameAndAddress):
    invokeMenuItem("Edit", "Add...")
    for fieldName, text in zip(("Forename", "Surname", \
                    "Email", "Phone"), nameAndAddress):
        type(waitForObject(":%s:_QLineEdit" % fieldName)\
             , text)
    clickButton(waitForObject(":AB-Add.OK_QPushButton"))

def checkNameAndAddress(record):    
    table = waitForObject(":Address Book_QTableWidget")
    for column in range(len(record)):
        test.compare(table.item(0, column).text(),
                     record[column])

A traditional Script Test Case would appear as follows, using the functions directly:
source(findFile("scripts", "sharedFunctions.py"))
def main():
    startApplication("addressbook")
    newEntry = ("Jane", "Smith", "jane@smith.com",\
                       "123.123.1234")
    invokeMenuItem("File", "New")
    addNameAndAddress(newEntry)
    checkNameAndAddress(newEntry)

A BDD Test Case would appear as follows:
Scenario Outline: A first scenario in which the feature can be exercised
  Given the application is running
   And a new address book is open
  When a new entry '<forename>','<lastname>','<email>','<phone>' is added
  Then the info should match '<forename>','<lastname>','<email>','<phone>'
  Examples:
      | forename  | lastname | email          | phone        |
      | Jane      | Smith    | jane@smith.com | 123.123.1234 |

The BDD steps are automated by the Test Suite Resource’s Implementation File, available for reuse to all other BDD Test Cases in the Test Suite, and can be recorded or manually created.

import __builtin__
source(findFile("scripts", "sharedFunctions.py"))

@Given("the application is running")
def step(context):
    startApplication("addressbook")

@Given("a new address book is open")
def step(context):
    invokeMenuItem("File", "New")

@When("a new entry '|word|','|word|','|any|','|any|' is added")
def step(context, forename, surname, email, phone):
    newEntry = (forename, surname, email, phone)
    addNameAndAddress(newEntry)

@Then("the info should match '|word|','|word|','|any|','|any|'")
def step(context, forename, surname, email, phone):
    newEntry = (forename, surname, email, phone)
    checkNameAndAddress(newEntry)

Why transition?

froglogic_cropped


Viewing all articles
Browse latest Browse all 398

Trending Articles