Beginner Level
I hope that you have enjoyed the journey of “Ten steps to setting up the Selenium Automation Framework” so far and you have understood all the topics well. Let’s revise what we have covered and do a Practice Exercise on it.
What we have covered in the previous chapters:
1) Page Object Model
2) Object Repository
3) Modular Driven Technique
4) Function Parameters
5) Constant Variables
6) Data Driven Technique
7) Log4j Logging
8) TestNG Reporting
9) User Defined Functions
10) Exception Handling
It is almost the end of setting up the framework. We have already covered everything and what is remaining is expanding the test to cover End to End scenarios, Prioritizing the Test Cases, Preparing a Test Suite and Grouping of Test Cases.
Before jumping to the next level I would like to do an exercise on what we have learned so far on the Demo Application. What we have covered so far is the Login functionality only. I would like you to automate an end to end flow covering below steps:
1) Login to the demo application Online Store
2) Selecting a product category from Top Menu
3) Selecting a product and adding it to the cart
4) Go to payment details page and complete the order
5) Verify details from final Confirmation page
Selenium Automation Hybrid Framework
This framework is at very beginner level and very easy to understand. This implements the Page Object Model Technique, Data Driven Technique, Modular Driven Technique, Log4j Logging, TestNG Reporting & TestNG Reporter Logs.
Please download the code from here Selenium Automation Hybrid Framework
Or please read the code below.
Test Data Sheet
Test Cases Package
Test Case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
package testCases;
import org.apache.log4j.xml.DOMConfigurator; import org.openqa.selenium.WebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import pageObjects.BaseClass; import utility.Constant; import utility.ExcelUtils; import utility.Log; import utility.Utils; import appModules.CheckOut_Action; import appModules.Confirmation_Action; import appModules.PaymentDetails_Action; import appModules.ProductSelect_Action; import appModules.SignIn_Action; import appModules.Verification_Action; public class Framework_001{ public WebDriver driver; private String sTestCaseName; private int iTestCaseRow; // Following TestNg Test case pattern, and divided a Test case in to //three different part. // In Before Method, your code will always be the same for //every other test case. // In other way before method is your prerequisites of your main Test Case @BeforeMethod public void beforeMethod() throws Exception { // Configuring Log4j logs, please see the following posts to // learn about Log4j Logging // http://www.toolsqa.com/selenium-webdriver/test-case-with-log4j/ // http://www.toolsqa.com/selenium-webdriver/log4j-logging/ DOMConfigurator.configure("log4j.xml"); // Getting the Test Case name, as it will going to use in so // many places // The main use is to get the TestCase row from the Test Data // Excel sheet sTestCaseName = this.toString(); // From above method we get long test case name including package //and class name etc. // The below method will refine your test case name, exactly the //name use have used sTestCaseName = Utils.getTestCaseName(this.toString()); // Start printing the logs and printing the Test Case name Log.startTestCase(sTestCaseName); // Setting up the Test Data Excel file using Constants variables // For Constant Variables please see //http://www.toolsqa.com/selenium-webdriver/constant-variables/ // For setting up Excel for Data driven testing, please see //http://www.toolsqa.com/selenium-webdriver/data-driven-testing-excel-poi/ ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData, "Sheet1"); // Fetching the Test Case row number from the Test Data Sheet // This row number will be feed to so many functions, to get the relevant //data from the Test Data sheet iTestCaseRow = ExcelUtils.getRowContains(sTestCaseName, Constant.Col_TestCaseName); // Launching the browser, this will take the Browser Type from Test //Data Sheet driver = Utils.OpenBrowser(iTestCaseRow); // Initializing the Base Class for Selenium driver // Now we do need to provide the Selenium driver to any of the //Page classes or Module Actions // Will soon write a post on Base Class new BaseClass(driver); } // This is the starting of the Main Test Case @Test public void main() throws Exception { // Every exception thrown from any class or method, //will be catch here and will be taken care off // For Exception handling please see //http://www.toolsqa.com/selenium-webdriver/exception-handling-selenium-webdriver/ try{ // Here we are calling the SignIN Action and passing argument (iTestCaseRow) // This is called Modularization, when we club series of // actions in to one Module // For Modular Driven Framework, please see //http://www.toolsqa.com/selenium-webdriver/modular-driven/ SignIn_Action.Execute(iTestCaseRow); // This action is to select the Product category from the Top //Navigation of the Home Page // I have converted this in to a module, as there are so many //logics involved in to this selection // And it is always a best idea to keep your logics separate from your //test case ProductSelect_Action.productType(iTestCaseRow); // This action is to select the Product from the Product Listing Page // I have again converted this in to a module, as there are so many //logics involved in to this selection ProductSelect_Action.productNumber(iTestCaseRow); // This is to assigning Product Name & Price to the variables from the //Check Out page, so that it can be matched later for verification CheckOut_Action.Execute(); // Here we are calling the Payment Details Action and passing //argument (iTestCaseRow) // This action will provide all the personal detail and payment detail //on the page and complete the payment for the selected product PaymentDetails_Action.execute(iTestCaseRow); // This is to assigning Product Name & Price to the variables from //the Confirmation page, so that it can be matched later for verification Confirmation_Action.Execute(); // This is to match the Product Name & Price we have stored in //variables of Checkout & Confirmation page Verification_Action.Execute(); // Now your test is about to finish but before that you need to take //decision to Pass your test or Fail // For selenium your test is pass, as you do not face any exception and //you come to the end or you test did not stop anywhere // But for you it can be fail, if any of your verification is failed // This is to check that if any of your verification during the //execution is failed if(BaseClass.bResult==true){ // If the value of boolean variable is True, then your test is //complete pass and do this ExcelUtils.setCellData("Pass", iTestCaseRow, Constant.Col_Result); }else{ // If the value of boolean variable is False, then your test is fail, //and you like to report it accordingly // This is to throw exception in case of fail test, this exception //will be caught by catch block below throw new Exception("Test Case Failed because of Verification"); } // Below are the steps you may like to perform in case of failed test or //any exception faced before ending your test }catch (Exception e){ // If in case you got any exception during the test, it will mark your //test as Fail in the test result sheet ExcelUtils.setCellData("Fail", iTestCaseRow, Constant.Col_Result); // If the exception is in between the test, bcoz of any element not //found or anything, this will take a screen shot Utils.takeScreenshot(driver, sTestCaseName); // This will print the error log message Log.error(e.getMessage()); // Again throwing the exception to fail the test completely in //the TestNG results throw (e); } } // Its time to close the finish the test case @AfterMethod public void afterMethod() { // Printing beautiful logs to end the test case Log.endTestCase(sTestCaseName); // Closing the opened driver driver.close(); } } |
Application Modules Package
SignIn Action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package appModules;
import org.testng.Reporter;
import pageObjects.Home_Page;
import pageObjects.LogIn_Page;
import utility.Constant;
import utility.ExcelUtils;
import utility.Log;
import utility.Utils;
// This is called Modularization, when we club series of
//actions in to one Module // For Modular Driven Framework, please see //http://www.toolsqa.com/selenium-webdriver/modular-driven/ public class SignIn_Action { // iTestcaseRow is the row number of our Testcase name in the //Test Data sheet // iTestcaseRow is passed as an Argument to this method, //so that it can used inside this method // For use of Functions & Parameters, please see //http://www.toolsqa.com/selenium-webdriver/function-parameters public static void Execute(int iTestCaseRow) throws Exception{ // Clicking on the My Account link on the Home Page Home_Page.lnk_MyAccount().click(); Log.info("Click action is perfromed on My Account link" ); // Storing the UserName in to a String variable and Getting //the UserName from Test Data Excel sheet // iTestcaseRow is the row number of our Testcase name in the //Test Data sheet // Constant.Col_UserName is the column number for UserName column //in the Test Data sheet // Please see the Constant class in the Utility Package // For Use of Constant Variables, please see //http://www.toolsqa.com/selenium-webdriver/constant-variables/ String sUserName = ExcelUtils.getCellData(iTestCaseRow, //Constant.Col_UserName); // Here we are sending the UserName string to the UserName //Textbox on the LogIN Page // This is call Page Object Model (POM) // For use of POM, please see //http://www.toolsqa.com/selenium-webdriver/page-object-model/ LogIn_Page.txtbx_UserName().sendKeys(sUserName); // Printing the logs for what we have just performed Log.info(sUserName+" is entered in UserName text box" ); String sPassword = ExcelUtils.getCellData(iTestCaseRow, Constant.Col_Password); LogIn_Page.txtbx_Password().sendKeys(sPassword); Log.info(sPassword+" is entered in Password text box" ); LogIn_Page.btn_LogIn().click(); Log.info("Click action is performed on Submit button"); // I noticed in few runs that Selenium is trying to perform the next action before the complete Page load // So I have decided to put a wait on the Logout link element // Now it will wait 10 secs separately before jumping out to next step Utils.waitForElement(Home_Page.lnk_LogOut()); // This is another type of logging, with the help of TestNg //Reporter log // This has to be very carefully used, you should only print the //very important message in to this // This will populate the logs in the TestNG HTML reports // I have used this Reporter log just once in this whole module Reporter.log("SignIn Action is successfully perfomred"); } } |
No comments:
Post a Comment