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?
- Faster test creation
- Optimized test maintenance
- And tests written in a business-readable language
- Request your free 30 day Squish evaluation
- Learn more about Squish
- Other Squish Resources (videos, tech articles, user communities and more)
Not only are you reusing functions you’ve already implemented – but now you’re implementing new tests with even greater reuse capabilities – easily accessed using Statement Completion