[MGNLTEST-23] Cucumber using pageObjects requires dependency injection Created: 28/Aug/19  Updated: 29/Nov/21  Resolved: 29/Nov/21

Status: Closed
Project: Magnolia Test Framework
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Neutral
Reporter: Christoph Meier Assignee: Unassigned
Resolution: Obsolete Votes: 0
Labels: QA&Testing, ui-test-fwk
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Template:
Acceptance criteria:
Empty
Task DoR:
Empty
Epic Link: core-TF-features-bugs-improvements

 Description   

TLDR

In Cucumber step-definition classes, we need the possibility to share the root pageObject among several methods. This requires dependency injection.


Cucumber context

In cucumber within one step definition class, there are typically a bunch of step definition methods for multiple scenarios of the same feature or even for several features of the same domain.

Each scenario consists of a set of methods. Sometimes one method can even be reused for more than one of the scenarios. The step def methods are executed in the order of how the scenario is defined in the feature file.

step def methods can not have parameters to share objects between methods (with the exception of cucumber.api.Scenario). Cucumber recommends using dependency injection (DI) to share objects among different methods.

Luckily, if a feature has multiple scenarios , the cucumber test runner creates a new instance of the step definition class for each scenario; and for each row of a scenario outline  the cucumber test runner creates a new instance of the step definition class.
(See https://cucumber.io/blog/polymorphic-step-definitions/ , https://stackoverflow.com/questions/48726448/cucumber-step-definition-class-initialization)

Sharing the root pageObject "ui" among the methods of the same scenario ...

Within one scenario the class must share the same instance of the root page object info.magnolia.testframework.PageObjects (="ui") among the methods of this same scenario.

... but  resetting the ui when starting a new scenario

When executing the 1st method of a scenario, the class must get a new, "fresh" instance of the ui.

Hooks 

The cucumber API provide "hooks" which are executed before the 1st step and after the last step.

  • cucumber.api.java.Before hooks run before the first step of each scenario.
  • cucumber.api.java.After hooks run after the last step of each scenario, even when steps are failed, undefined, pending, or skipped.

See https://cucumber.io/docs/cucumber/api/#hooks


Acceptance criteria

  • Provide a DI system to inject the root pageObjects, e.g. by injecting it into the constructor of the step def. class.
  • Provide method(s) which allow to "destroy" the shared root pageObject by using the After hook and to get a new one by using the Before hook, if required.

More resources


Mocked example

I have added another example covering all my "cases" (having multiple scenarios plus a scenario outline).
For the new example-test I have created the class info.magnolia.functionaltests.cucumberbasictests.DummyUI to mock PageObjects, to make sure I can run the test. DummyUI gets injected by picocontainer.
The examples are on my (latest) branch (currently named feature/MGNLCE_164-page-objects-api_chm-version_vi).

  • src/test/resources/info/magnolia/functionaltests/cucumberbasictests/configuration_app_availability_for_different_users.feature
  • info.magnolia.functionaltests.cucumberbasictests.ConfigAppAvailabilityTest_stepDef
  • info.magnolia.functionaltests.cucumberbasictests.ConfigAppAvailabilityTest

To trigger the test, run ConfigAppAvailabilityTest via Maven


Generated at Mon Feb 12 07:44:48 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.