Business Example
In this exercise, you will write OPA tests.
Note
SAP Business Technology Platform and its tools are cloud services. Due to the nature of cloud software, step procedures and the naming of fields and buttons may differ from the exercise solution.
Note
In the following exercises, replace <L> with the course group and ## with your user group.Caution
Be aware that in some cases the initial code or code of previous implementation steps is not shown. In the following steps, do not delete any existing code or code that you have added in previous steps if not explicitly asked. In addition, be aware that the screenshots show the solutions from student00 namespace; you should replace this with your own student## one.Task 1: Create the SAP Fiori Freestyle Application to Test
Steps
Log into your SAP Business Application Studio.
Perform this step as shown in a previous exercise.
Create a new SAP Fiori Freestyle project using the following information:
SAP Fiori Freestyle Project Information
Field Value Template SAP Fiori application Template Type SAP Fiori Template Basic Data sources None View name Main Module name ux402_opa Application title OPA Tests Application namespace student##.com.sap.training.ux402.opa Description An OPA-test Application. Project folder path /home/user/projects Leave the rest of the fields and radio buttons set to their default values.
Perform this step as shown in a previous exercise.
In the Main.view.xml file, add a sap.m.Button element in the header of the page, with the following attributes:
Attribute-value Pairs for the sap.m.Button Element
Attribute Value id headerButton text Header Button Open the Main.view.xml file with the Code Editor of your SAP Business Application Studio.
Add the headerContent aggregation to the Page element. Use the following code:
Code SnippetCopy codeSwitch to dark mode123<headerContent> </headerContent>Add an sap.m.Button element to the headerContent aggregation with the listed attribute-value combinations. Use the following code:
Code SnippetCopy codeSwitch to dark mode1<Button id="headerButton" text="Header Button"/>Save your changes
Add the following elements with the listed attribute-value combinations to the content aggregation of the sap.m.Page element.
Attribute-value Pairs for the Elements
Elements Attributes Value sap.m.Label text Firstname sap.m.Label id firstNameLabel sap.m.Input value John sap.m.Input id firstNameInput sap.m.Label text Lastname sap.m.Label id lastNameLabel sap.m.Input value Johnson sap.m.Input id lastNameInput If it is not already open, open the Main.view.xml file.
Add the elements with the listed attribute-value combinations to the contentaggregation of the sap.m.Page element. Use the following code:
Code SnippetCopy codeSwitch to dark mode1234<Label id="firstNameLabel" text="Firstname"/> <Input id="firstNameInput" value="John"/> <Label id="lastNameLabel" text="Lastname"/> <Input id="lastNameInput" value="Johnson"/>Save your changes.
Add an sap.m.Button element after the last sap.m.Input element to the content aggregation with the following attribute-value combinations.
Attribute-value Pairs for the sap.m.Button Element
Attribute Value id helloButton text Press me press onPress If not already open, open the Main.view.xml file in the Code Editor.
Add the sap.m.Button element with the attribute-value combinations in the table provided to the content aggregation. Use the following code:
Code SnippetCopy codeSwitch to dark mode1<Button id="helloButton" text="Press me" press="onPress"/>Your code should now look like the following :
Code SnippetCopy codeSwitch to dark mode123456789101112131415<Page id="page" title="{i18n>title}"> <headerContent> <Button id="headerButton" text="Header Button"/> </headerContent> <content> <Label id="firstNameLabel" text="Firstname"/> <Input id="firstNameInput" value="John"/> <Label id="lastNameLabel" text="Lastname"/> <Input id="lastNameInput" value="Johnson"/> <Button id="helloButton" text="Press me" press="onPress"/> </content> </Page>Save your changes and close the Main.view.xml file.
Implement the onPress event handler function in the Main.controller.js file for the press event of the sap.m.Button element of the Main.view.xml file. Add an oEvent parameter to the function. The event handler function should show an sap.m.MessageBox with the text Button pressed. Use the show function of the sap.m.MessageBox to display the message.
Open the Main.controller.js file, which is located in the controller folder of your project.
Add a reference to the sap.m.MessageBox in the module definition. Use the following code:
Code SnippetCopy codeSwitch to dark mode12345678sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/m/MessageBox" ], function (Controller, MessageBox) { "use strict";Add an onPress function with a parameter, oEvent, to the controller implementation. Use the following code:
Code SnippetCopy codeSwitch to dark mode123456789return Controller.extend("student##.com.sap.training.ux402.opa.ux402opa.controller.Main", { onInit: function () { }, onPress: function (oEvent) { } });Implement the onPress function. On the MessageBox reference, invoke the show function and pass the string literal Button pressed to the show function, as in the following code:
Code SnippetCopy codeSwitch to dark mode1MessageBox.show("Button pressed");
Run your application. Choose the Press me button. A dialog appears with the message Button pressed.
Open the context menu on your project folder (ux402_opa).
Select Preview Application.
Choose start fiori run –open ".." on the opened dialog.
If prompt, select Open on the dialog in the bottom right corner.
Task 2: Implement OPA Tests
Steps
Implement a test function with the name iShouldFindAButton in the webapp/test/integration/pages/Main.js file and check if the Main-view implemented in the previous task contains a control with the id helloButton.
Attribute-value Pairs for this.waitFor
Attribute Value viewName sViewName id helloButton success Callback function with one parameter named oButton errorMessage Did not find the hellobutton timeout 3 Open the webapp/test/integration/pages/Main.js file.
Implement the iShouldFindAButton function after the iShouldSeeThePageView function, using the following code:
Code SnippetCopy codeSwitch to dark mode123456789101112iShouldFindAButton: function () { return this.waitFor({ viewName: sViewName, id: "helloButton", success: function (oButton) { }, errorMessage: "Did not find the hello-Button", timeout: "3" }); }Implement the success function. Trigger the press event on the passed oButton object and print out an assertion of type ok. Use the following code:
Code SnippetCopy codeSwitch to dark mode123oButton.$().trigger("press"); Opa5.assert.ok(oButton.getId(), "Button with the given ID found");Save your work. Your implementation should now look like the following:
Code SnippetCopy codeSwitch to dark mode123456789101112131415iShouldFindAButton: function () { return this.waitFor({ viewName: sViewName, id: "helloButton", success: function (oButton) { oButton.$().trigger("press"); Opa5.assert.ok(oButton.getId(), "Button with the given ID found"); }, errorMessage: "Did not find the hello-Button", timeout: "3" }); }
Add the assertion iShouldFindAButton to the webapp/test/integration/NavigationJourney.js file.
Open the NavigationJourney.js file and add theiShouldFindAButton as a new assertion. Add the following code after the iShouldSeeThePageView() assertion:
Code SnippetCopy codeSwitch to dark mode1Then.onTheViewPage.iShouldFindAButton();Save your changes. Your code should look like the following:
Code SnippetCopy codeSwitch to dark mode123456789101112131415opaTest("Should see the initial page of the app", function (Given, When, Then) { // Arrangements Given.iStartMyApp(); // Assertions Then.onTheAppPage.iShouldSeeTheApp(); Then.onTheViewPage.iShouldSeeThePageView(); Then.onTheViewPage.iShouldFindAButton(); //Cleanup Then.iTeardownMyApp(); });
Start your application in int-tests mode. A new browser tab opens and the test case starts. When the test is complete, you can expand the test result to get more details. The results screen should look like the one shown in the following figure.
Open the context menu on our project folder (ux402_opa).
Select Preview Application.
Choose int-tests fiori run –open ".." on the opened dialog.
If prompted, choose Open on the dialog in the bottom right corner.
A new browser tab opens, and the test case starts. When the test is complete, you can expand the test result to get more details.