130 likes | 143 Views
Learn effective UI testing strategies using Cucumber for Java and C++ languages. Understand best practices, scenario outlining, and handling dependencies to enhance your testing process. Discover methods for Java and C++ UI testing, avoiding common pitfalls and ensuring robust test suites.
E N D
Dr. Rob Hasker SE 3800Note 6UI Testing
More notes on Cucumber • Can replace Given/When/Then by bullets Feature: Refund item Sales associates required by law to refund purchases Scenario: Jeff returns a faulty microwave Given Jeff has bought a microwave for $100 And he has a receipt # don’t assume this is true! When he returns the microwave Then Jeff should be refunded $100
More notes on Cucumber • Can replace Given/When/Then by bullets Feature: Refund item Sales associates required by law to refund purchases Scenario: Jeff returns a faulty microwave Given Jeff has bought a microwave for $100 And he has a receipt # don’t assume this is true! When he returns the microwave Then Jeff should be refunded $100 Feature: Refund item Sales associates required by law to refund purchases Scenario: Jeff returns a faulty microwave * Jeff has bought a microwave for $100 * he has a receipt # don’t assume this is true! * he returns the microwave * Jeff should be refunded $100 So is this better?
More notes on Cucumber • Stateless scenario! • Each scenario must make sense by self • No dependencies between scenarios! • Eg: adding to account balance in one, using that balance in another • Dependencies: frequent failures, difficult maintenance • Solution • Before hooks can be set up to execute before scenarios • Write a step definition for a common case: “customer has a bag of groceries” • Step definition: set up bag object w/ predefined contents
More notes on Cucumber • Frequent problem: names become stale (out-of-step with goals) • Best practice: • name scenarios by Given, When clauses • These won’t change much! • Don’t name them by the Then clause • Generally what changes when requirements change!
UI Testing in Java • Assumption: painful but readily available • Research: lots of vaporware, abandonware • Many solutions for web, but not a lot for Java • FrogLogic, RAutomation, MarathonTesting, UISpec4J, Mspec, abbot • Method that works: • JavaWorldTestUtils; see also counter.zip
UI Testing in Java • Each component: call .setNamewith unique string • Test code: TestUtils.getChildNamed(frame, name-of-child) • Use .doClick, .value(), etc. to exercise code • Robust • Doesn’t depend on screen locations, specialized test frameworks • But no auto-capture/replay
UI Testing in C++ w/ Cucumber Feature: Subtraction In order to avoid silly mistakes As a math-challenged person I want to be told the difference of two numbers Scenario Outline: Subtract two numbers Given I just turned on the calculator When I press <button1> And I press subtract And I press <button2> And I press calculate Then the display should show <result> Examples: | button1 | button2 | result | | 2 | 3 | -1 | | 7 | 5 | 2 | | 9 | 1 | 8 | From https://github.com/cucumber/cucumber-cpp
UI Testing in C++ w/ Cucumber Feature: Subtraction In order to avoid silly mistakes As a math-challenged person I want to be told the difference of two numbers Scenario Outline: Subtract two numbers Given I just turned on the calculator When I press <button1> And I press subtract And I press <button2> And I press calculate Then the display should show <result> Examples: | button1 | button2 | result | | 2 | 3 | -1 | | 7 | 5 | 2 | | 9 | 1 | 8 | GIVEN("^I just turned on the calculator$") { cucumber::ScenarioScope<CalculatorWidget> calculator; calculator->move(0, 0); calculator->show(); QTest::qWaitForWindowShown(calculator.get()); QTest::qWait(millisecondsToWait()); }
UI Testing in C++ w/ Cucumber WHEN("^I press (\\d+)$") { REGEX_PARAM(unsigned int, n); cucumber::ScenarioScope<CalculatorWidget> calculator; QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait()); } Feature: Subtraction In order to avoid silly mistakes As a math-challenged person I want to be told the difference of two numbers Scenario Outline: Subtract two numbers Given I just turned on the calculator When I press <button1> And I press subtract And I press <button2> And I press calculate Then the display should show <result> Examples: | button1 | button2 | result | | 2 | 3 | -1 | | 7 | 5 | 2 | | 9 | 1 | 8 | WHEN("^I press subtract") { cucumber::ScenarioScope<CalculatorWidget> calculator; QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait()); }
UI Testing in C++ w/ Cucumber WHEN("^I press calculate") { cucumber::ScenarioScope<CalculatorWidget> calculator; QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait()); } THEN("^the display should show (.*)$") { REGEX_PARAM(QString, expected); cucumber::ScenarioScope<CalculatorWidget> calculator; BOOST_CHECK_EQUAL(expected, calculator->display()); QTest::qWait(millisecondsToWait()); } Feature: Subtraction In order to avoid silly mistakes As a math idiot I want to be told the difference of two numbers Scenario Outline: Subtract two numbers Given I just turned on the calculator When I press <button1> And I press subtract And I press <button2> And I press calculate Then the display should show <result> Examples: | button1 | button2 | result | | 2 | 3 | -1 | | 7 | 5 | 2 | | 9 | 1 | 8 |
Acceptance Tests & APIs • How to write acceptance tests for an API? • Our model: acceptance test = story/scenario • Issue: an API is not a user! • Solution: Cohn: Writing User Stories for Back-end Systems • Personify subsystems • Epic: “As a bank, I want to receive a file showing all checks to be cleared so that I can debit and credit the right accounts.” • “As a bank, I want to receive a 5300 file with correctly formatted single-line deposit entry records so that I can process them.” • Write test to the resulting story.
Review • UI Testing with Java: TestUtils • UI Testing in C++ w/ Qt: cucumber-cpp • Acceptance testing for APIs