http://www.softwaretestinghelp.com/selenium-framework-design-selenium-tutorial-21/
In the last tutorial, we familiarized you with the basics of test automation Frameworks, its components and types. The frameworks illustrated in the previous tutorial were a few amongst the most popular frameworks used by the testing fraternity.
We briefly discussed about Module based Frameworks, Library Architecture based framework, Keyword driven framework, Data driven Framework and Hybrid Framework. There are various other frameworks also in the place.
Please take a note that we would be adopting Data Driven Test Automation Framework for the rest of our tutorials.
In the current tutorial in this series, we would make you acquainted with a sample framework, the Excels which would store the test data and their Excel manipulations. On the same lines, we would move forward and introduce new strategies and resources to mature our framework.
So let’s learn:
- Framework creation strategy using a sample project
- Access the test data stored in the external data source
Moving ahead, we would start with the description of the project hierarchy that we would be creating in order to segregate the various project components.
Refer the below image for the project hierarchy created for the sample project. The below java project can be easily created within the eclipse the way we have created the projects in the earlier tutorials.
Selenium Project Folder Structure – Walkthrough
#1) src – The folder contains all the test scripts, generics, readers and utilities. All these resources are nothing but the simple java classes. Under the source (src) folder, we have created a hierarchy of folders.
a) test – The “test” folder is constituted of majorly two ingredients – testsuite and the folders representing the various modules of the application under test. Thus, each of these folders contains the test scripts specific to the module to which it is associated. Testsuite is a logical combination of more than one test scripts. Thus, the user can mark an entry of any of the test script within the testsuite that he/she desires to execute in the subsequent runs.
b) utilities – The “utilities” folder is constituted of various generics, constants, Readers and classes for implementing user defined exceptions. Each of the folders under utilities has got its own significance.
-
Excel Reader – A generic and common class has been created to read the test data (input parameters and expected results) from the Excel sheets
-
EnvironmentConstants – The folder are integration of the java classes that stores the static variables referencing to the paths and other environmental details. These details can be Application URL, URL to the Databases, Credentials for Databases, and URL to any third party tool being used. The disparate application URLs can be set for different environments (dev, prod, test, master, slave etc).
-
DataSetters – The folder incorporates the classes that implement the getters and setters of the test data fetched from the Excels. To lode multiple sets of Test data, we create ArrayLists.
-
UserRoles – The folder accommodates the classes that take care of the Role based access criteria if any for instinct users.
-
FunctionLibrary – The folder is constituted of the classes which contain functions and methods that can be shared and used amongst the multiple classes. Very often, we are suppose to perform certain procedures prior and aftermath to the actual test execution like login to the application, setting up environments, activities related to rolls, data manipulations, writing results, methods those generate pre/post-conditions to other methods. Since we tend to perform these activities for all or most of the test script. Thus it is always recommended to create a separate class for such activities instead of coding them repeatedly in each of the test script.
- PreConditionalMethods
- PostConditionalMethods
Very often, we are suppose to perform certain procedures prior and aftermath to the actual test execution like login to the application, setting up environments, activities related to user rolls, data manipulations, writing results, methods those generate pre/post-conditions to other methods. Since we tend to perform these activities for all or most of the test script, thus it is always recommended to create a separate class for such activities instead of coding them repeatedly in each of the test script.
CommonMethods
Like Pre and post conditions, there may be methods and functions those can be used by more than one test script. Thus, these methods are grouped together in a class. The testscript can access these methods using the object of the common class.
#2) excelFiles – The excel files are considered to be the data source/data providers for test script execution. These files store the test data into key value pairs. Make a note that we create a separate excel sheet for each of the test script i.e. each test script has its own test data file. The name of the test script and the corresponding test data files/ excel sheet has been kept same for the traceability perspective. Check out the sample test data format below:
Test Data Format
Each of the columns represents a key and each of the rows represents a test data/value. Specify the multiple rows in order to execute the same test script with multiple data sets.
Mark that the test data formats are solely user defined. Thus based on your requirements, you can customize the test data files.
#3) library – The folder acts as a repository/artifactory for all the required jar files, libraries, drivers etc to successfully build the test environment and to execute the test scripts. Refer the following figure to check out the libraries we would be employing within our project.
#4) logs – The folder contains a .txt file that stores the logging information upon each execution.
#5) testMaterial – The folder contains the actual test data that needs to be uploaded if any. This folder would come into picture when we come across test scenarios where the user is required to upload files, documents, pictures, reports etc.
#6) build.xml – The xml file is used by the “Ant Server” to automate the entire build process.
#7) log4j.xml – This xml file is used by a Java based utility named as “Log4j” to generate the execution logs.
Note: We would study more about the logs, user defined exceptions and Ant in detail in the upcoming tutorials. So don’t panic if you get confused between the notions.
Now, as we move forward let us understand the phenomenon where we access the excel files and populate the test data into our test scripts.
In order to comprehend the process easily, we would break down the process into the following steps.
Test Data Creation
Step 1: The first and the foremost step is to create the test data with which we would be executing the test scripts. Considering the aforementioned test data format, let us create an excel file named as “TestScript1”. Furnish the values in the elements.
Step 2: The next step is to download a standard java based API/Library named as “Java excel Library” (jxl) to be able to access the already created generic methods for Excel Manipulation.
Step 3: Create a generic excel reader class named as “ExcelReader.java”. Copy the below code in the ExcelReader.java.
3 |
import java.io.IOException;
|
4 |
import java.util.Hashtable;
|
7 |
import jxl.read.biff.BiffException;
|
10 |
* This is a utility class created to read the excel test data file before performing the test steps.
|
11 |
* This class loads the excel file and
|
12 |
* reads its column entries.
|
16 |
public class ExcelReader {
|
18 |
* The worksheet to read in Excel file
|
21 |
public static Sheet wrksheet;
|
23 |
* The Excel file to read
|
26 |
public static Workbook wrkbook = null ;
|
28 |
* Store the column data
|
31 |
public static Hashtable<String, Integer> dict = new Hashtable<String, Integer>();
|
33 |
* Create a Constructor
|
35 |
* @param ExcelSheetPath
|
36 |
* @throws BiffException
|
37 |
* @throws WeblivException
|
40 |
public ExcelReader(String ExcelSheetPath) throws IOException, BiffException {
|
44 |
wrkbook = Workbook.getWorkbook( new File(ExcelSheetPath));
|
45 |
wrksheet = wrkbook.getSheet( "Sheet1" );
|
46 |
} catch (IOException e) {
|
47 |
throw new IOException();
|
51 |
* Returns the Number of Rows
|
56 |
public static int RowCount() {
|
57 |
return wrksheet.getRows();
|
60 |
* Returns the Cell value by taking row and Column values as argument
|
64 |
* @return Cell contents
|
67 |
public static String ReadCell( int column, int row) {
|
68 |
return wrksheet.getCell(column, row).getContents();
|
71 |
* Create Column Dictionary to hold all the Column Names
|
73 |
public static void ColumnDictionary() {
|
76 |
for ( int col = 0 ; col < wrksheet.getColumns(); col++) {
|
77 |
dict.put(ReadCell(col, 0 ), col);
|
87 |
public static int GetCell(String colName) {
|
90 |
value = ((Integer) dict.get(colName)).intValue();
|
92 |
} catch (NullPointerException e) {
|
Step 4: Create a generic class –“CommonMethods.java”. Create a common method within the class that would read the cells from the excel sheet using the methods implemented in ExcelReader.java.
2 |
* Read the test data from excel file |
4 |
* @param data The TestData data object |
7 |
public void readExcelData (TestData data) {
|
8 |
ArrayList<String> browser = new ArrayList<String>();
|
9 |
ArrayList<String> username = new ArrayList<String>();
|
10 |
ArrayList<String> password = new ArrayList<String>();
|
11 |
ArrayList<String> element1 = new ArrayList<String>();
|
12 |
ArrayList<String> element2 = new ArrayList<String>();
|
13 |
ArrayList<String> element3 = new ArrayList<String>();
|
16 |
for ( int rowCnt = 1 ; rowCnt < ExcelReader.RowCount(); rowCnt++) {
|
17 |
browser.add(ExcelReader.ReadCell(ExcelReader.GetCell( "Browser" ), rowCnt));
|
18 |
username.add(ExcelReader.ReadCell(ExcelReader.GetCell( "User ID" ), rowCnt));
|
19 |
password.add(ExcelReader.ReadCell(ExcelReader.GetCell( "Password" ), rowCnt));
|
20 |
element1.add(ExcelReader.ReadCell(ExcelReader.GetCell( "Element1" ), rowCnt));
|
21 |
element2.add(ExcelReader.ReadCell(ExcelReader.GetCell( "Element2" ), rowCnt));
|
22 |
element3.add(ExcelReader.ReadCell(ExcelReader.GetCell( "Element3" ), rowCnt));
|
24 |
data.setBrowser(browser);
|
25 |
data.setLoginUser(username);
|
26 |
data.setPassword(password);
|
27 |
data.setElement1(element1);
|
28 |
data.setElement2(element2);
|
29 |
data.setElement3(element3);
|
Step 5: Create a new java class named as “TestData.java”. This class would act as a getter and setter for excel data. Copy and paste the following code in the TestData.java class.
1 |
package Utilities.dataSetters;
|
2 |
import java.util.ArrayList;
|
3 |
public class TestData {
|
4 |
private ArrayList<String> loginUser = null ;
|
5 |
private ArrayList<String> password = null ;
|
6 |
private ArrayList<String> browser = null ;
|
7 |
private ArrayList<String> element1 = null ;
|
8 |
private ArrayList<String> element2 = null ;
|
9 |
private ArrayList<String> element3 = null ;
|
13 |
public ArrayList<String> getLoginUser() {
|
19 |
public void setLoginUser(ArrayList<String> loginUser) {
|
20 |
this .loginUser = loginUser;
|
25 |
public ArrayList<String> getPassword() {
|
31 |
public void setPassword(ArrayList<String> password) {
|
32 |
this .password = password;
|
37 |
public ArrayList<String> getBrowser() {
|
43 |
public void setBrowser(ArrayList<String> browser) {
|
44 |
this .browser = browser;
|
49 |
public ArrayList<String> getElement1() {
|
55 |
public void setElement1(ArrayList<String> element1) {
|
56 |
this .element1 = element1;
|
61 |
public ArrayList<String> getElement2() {
|
67 |
public void setElement2(ArrayList<String> element2) {
|
68 |
this .element2 = element2;
|
73 |
public ArrayList<String> getElement3() {
|
79 |
public void setElement3(ArrayList<String> element3) {
|
80 |
this .element3 = element3;
|
Step 6: The next step is to create instances of “TestData.java” and “CommonMethods.java” java classes within the test script in order to access and populate the test data. Refer the below code snippet for object initialization, reading excel data and populating the values wherever required.
2 |
public ExcelReader excelReaderObj;
|
3 |
CommonMethods commonMethodobj = new CommonMethods();
|
4 |
TestData td = new TestData();
|
7 |
excelReaderObj = new ExcelReader(Path of the excel);
|
10 |
excelReaderObj.ColumnDictionary(); |
13 |
commonMethodobj.readExcelData (td); |
16 |
driver.findElement(By.id( "idofElement" )).sendKeys(data.getLoginUser().get( 0 ));
|
Therefore using the instance of testData.java class in conjunction with getters, any test data value can be populated within the script.
Conclusion:
The tutorial mainly revolved around the notions like Framework Creation and Accessing test data from the excels. We made you acquainted with the Framework creation strategy using a sample project. We briefly laid light on the various components and aspects of our framework.
In order to access the test data stored in the external data source, we used a java based API – jxl. We also created the sample code for reading and populating the excel data into the testscripts.
Next Tutorial #22: In the next tutorial, we would base our tutorial on the concepts of generics and their accessibility mechanism. We would create a few sample generic methods and then access them within the test scripts. We would also introduce you with the concept of Testsuite and the sample code development.
相关推荐
This guide will provide a step-by-step approach to designing and building a data-driven test framework using Selenium WebDriver, Java, and TestNG. The book starts off by introducing users to the ...
from selenium import webdriver def is_element_present(driver, locator): try: driver.find_element_by_locator(locator) return True except NoSuchElementException: return False # 初始化浏览器 ...
from selenium import webdriver driver = webdriver.Chrome(executable_path='path_to_chromedriver') ``` 5. **测试兼容性**:运行Selenium脚本,如果能正常打开并操作浏览器,说明版本匹配成功。 在实际开发...
Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....
自动化测试框架RobotFramework+Selenium2 自动化测试框架RobotFramework+Selenium2是结合RobotFramework框架和Selenium2Library实现自动化测试的解决方案。下面是该框架的详细介绍: 简介 Robot Framework是一个...
在“RFS——RobotFramework+Selenium2library安装包齐全”中,我们有完整的环境来搭建和运行基于Robot Framework和Selenium2Library的自动化测试项目。 首先,让我们深入理解Robot Framework。Robot Framework是一...
用于firefox录制的插件,是php语言的文件导出,使用firefox打开的话下载完成后直接安装。
Use the PageFactory pattern to build a robust and easy to maintain test framework Build data-driven tests and extend Selenium API to implement custom steps and checks Integrate and use ATDD/BDD tools ...
### RobotFramework + Selenium 自动化测试 - 齐涛博客教程整理 #### 一、引言及基础知识 RobotFramework(简称RF)结合Selenium Library,为Web自动化测试提供了一个强大的框架。这种组合允许测试人员使用一种...
**RobotFramework-Selenium2Library详解** Robot Framework是一个通用的自动化框架,它支持各种测试和任务自动化,而Selenium2Library是Robot Framework中的一个关键库,专门用于Web应用程序的自动化测试。这个库是...
Robot Framework Selenium2Library 1.5.0 是一个专门针对Web自动化测试的强大的库,它构建于Robot Framework之上,提供了一套简洁而易用的接口,使得测试人员能够高效地编写自动化测试用例。Robot Framework 是一个...
Selenium IDE是firefox的一个插件,可以帮助刚入门的自动化测试供测试,在脚本语言不太熟练的情况下,可以通过Selenium IDE实现脚本的录制、开发、回放。 众所周知,「Selenium IDE」是一种记录和回放工具。现在它将...
This book is intended for anyone who wants to create a test suite that is easy to maintain by expanding your knowledge until you feel truly confident and comfortable with Selenium. What You Will ...
Robot Framework+Selenium2Library 是一个高效且用户友好的自动化测试框架,主要用于Web应用程序的测试。Robot Framework 是一个通用的关键词驱动测试框架,而Selenium2Library(现称为SeleniumLibrary)则是Robot ...
标题中的"robotframework-selenium2library-3.0.0.tar.gz"是一个开源自动化测试框架Robot Framework的一个扩展库——Selenium2Library的3.0.0版本的压缩包。Robot Framework是一个通用的、基于关键字驱动的自动化...
**SeleniumJava:基本的SeleniumJava Framework** Selenium Java框架是自动化测试领域的一个强大工具,它允许开发者使用Java语言来编写测试脚本,以实现对Web应用的自动化测试。这个框架基于Selenium WebDriver,一...
selenium-java-3.141.59.jar selenium java自动化