Open lindalei opened 4 years ago
Hi,
you can use some kind of process objects pattern. Don't forget that it() are just functions, so you can define a higher level method like iSetupScenario: { it("setup:,..., it("create",... )}. You can place those functions in a separate module and simply require them in your spec.js
hi, in another js file, we define a high level function that calls methods and assertions from Page, such as iCreateControl: function () { When.onTheManageControlWorklistPage.iPressTheCreateButton(); Then.onTheCreateControlPage.iExpectTheBrowserToHaveTheCorrectTitle(); When.onTheCreateControlPage.iFillControlName("controlName"); When.onTheCreateControlPage.iFillValidFrom("20200811"); When.onTheCreateControlPage.iFillValidTo("20210811"); When.onTheCreateControlPage.iClickCreateControl(); Then.onTheCreateControlPage.iShouldSeeMessageToast(); },
and in spec file, we call this high level function like: it("Create Controls", function () { itHelper.iCreateControl(); }
this works well, but not sure if it's exactly what you mean
we also tried another way but not work: define a function in Page action and try to call assertion in the same page iCreateControl: function () { this.iFillControlName("test"); this.iFillValidFrom("20200811"); this.iFillValidTo("20210811"); this.iClickCreateControl(); it("iShouldSeeMessageToast", () => { Then.onTheCreateControlPage.iShouldSeeMessageToast(); }); } is it impossible to directly call assertion in action in the same page?
Yes, this is exactly what I mean.As for page objects, they are like "templates", at runtime we merge them in the Given/When/Then objects under the respective names. So you can directly reference an assertion from the action and the opposite. But what you can do is to "export" the actual code in local scope function like _iFillControlName and then call it both from the action and assertion. Actually some comlex test suites I have seen build a whole layer of such utilities. IMHO the best is to keep the Page Objects and the Process Objects layer separate and define them in separate files.
we know define some methods in "actions" section and some assertions in "assertions" section in Page, and call these methods and assertions in spec file will work, but now we have a requirement of defining a composite function to be reused which consists of both methods and assertions , such as iCreateControl: function () { iFillControlName("test"); //action iFillValidFrom("20200811"); iFillValidTo("20210811"); iClickCreateControl(); iShouldSeeMessageToast(); //assertion },
currently, we have a solution that, define some methods and assertions in a helper js file, and in Page, define a method (say compositeMethod) in "action" that calls these methods and assertions, and in different spec files, we can reuse this method (compositeMethod). It works but the disadvantage is that we have to move existing methods and assertions from Page to helper js, or maintain duplex. Is there any better solution for reuse purpose?