I recently joined to automated test Java project which uses Cucumber and Selenium. It is used to test web application. Project structure is divided into 3 layers:
- feature files - which contains cucumber tests scenarios and steps
- glue files - which are binding cucumber step and invoke Java methods
- page files - which contains fields that refers to web page elements and methods to access these elements.
Every glue file has method with @Before
tag and @additional
tag to make this method be executed before each test scenario in corresponding feature file. Appropriate feature file uses also @additional
tag to know which @Before
method should it invoke. Inside each method with @Before
tag there is a bunch of lines, which initializes some Page files objects, e.g :
Cucumber file:
@DesignPage
Scenario: some example scenario
Given HI im cucumber step method
Glue file:
@Before @DesignPage
public someBeforeMethod(){
...
designPage = new DesignPage();
managmentPage = new ManagmentPage();
...
lateron, glue files has methods definition for cucumber steps, which invokes some methods from page files, e.g.:
@Given("HI im cucumber step method")
public void HiImCucumberMethod(){
designPage.executeSomeDesignPageMethod();
}
Till now everything works fine.
Now lets say that I have another (@MainPage) feature file, but I want to execute exact same method from (DesignPage) feature:
@MainPage
Scenario: some example scenario2
Given HI im cucumber step method
Cucumber sees from here, that HI im cucumber step method
is already declared in DesignPage
glue file, so I want to reuse this method. Unfortunately I will recive NullPointerException - and the reason is clear. designPage
object which is used in this method was never initialized, because I never executed @Before
method from DesignPage
glue file. And the reason is because with this scenario I executed only @Before
method from different glue file - MainPage
glue file.
This architecture annoys me a little, because the only solution is to copy-paste HI im cucumber step method
method from DesignPage glue file to MainPage glue file, initialize design page object in MainPage
@Before
class, and then in addition I have to rename HI im cucumber step method
, because Cucumber sees now two methods with same reference and it don't know which one should use.
I spend on that some time and I have no idea how to change architecture, to be able in one feature reuse method from another feature. Any suggestions will be welcomed.