Selenium is a powerful testing tool in testing various test cases for many applications. This testing tool suits best for both manual as well as automated testing. Today many testers write test cases, for testing the application manually (or) through automation. Today in this article, I would like to elaborate on the different Selenium Framework. But prior to knowing about the different selenium frameworks, let us initially discuss.
Why do we need the Selenium Framework?
Prior to the selenium frameworks, test prefers to write the test cases as well as the data logic in a single place. This makes the other testers very difficult to read/modify the test case in the entire code. Moreover, running this long length will be the time taking process. So to overcome this, developers opt for Selenium Frameworks.
What is Selenium Frameworks?
Selenium framework is a code structure that helps to maintain the code easily. This framework helps in reusing the same piece of code again and again. A test case involves breaking the entire different fragments. Here each fragment is responsible for performing the particular functionality. In frameworks, the code is structured in such a way that the data set is separated from the actual test case. These test cases, test the functionality of the web application. The test cases can be written in different external applications like CSV. With this framework, people concentrate on writing different possible test cases rather than writing the same test cases multiple times.
Get hands-on experience of Selenium framework design from scratch at Selenium Online Training
Since we have got a brief idea regarding the selenium frameworks and their need, let us move forward towards the different frameworks in selenium
Selenium Frameworks:
These selenium framework types were classified into three as shown below. They are :
let us discuss one by one each in detail
Data-Driven Framework:
This framework is responsible for separating the data set from the actual test code. This framework completely depends on the test input data. Testers can get this input data from any external sources like Excel, CSV (or) any other database. Since the test case is separated from the data set, we can easily modify the particular functionality without making the whole change to the code. For instance, if you would like to modify any contact details of the testing, you can just modify that, rather than modifying the whole application. On the other hand, this framework, allows you to control the amount of data that needs to be tested. Besides, this framework also allows you to increase the parameters. The tester can do this by adding the required columns to the table.
Using Apache POI With Selenium WebDriver:
Selenium Web Driver is incapable of reading the data from the Excel file. Hence readers use Apache POI for reading/ writing any document. Testers used to get the Apache POI from the official website as the set of JAR files.
In this framework, the coordination of the main code and the dataset will be taken care of by the TestNG Data Providers. Basically, these TestNG Data providers will be part of the Apache POI JAR files.
Now, I would like to elaborate on this framework functioning with an example.
Considers the database / Excel containing the usernames and passwords.
Username | Password |
Rahul | OnlineITGuru |
Ram | ITGuru |
Shyam | Admin |
Naveen | Administrator |
Testers do validate the authentication using the following code
package DataDriven; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DDTExcel { ChromeDriver driver; @Test(dataProvider="testdata") public void DemoProject(String username, String password) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C:\\Users\\OnlineITGuru\\Downloads\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://newtours.demoaut.com/"); driver.findElement(By.name("userName")).sendKeys(username); driver.findElement(By.name("password")).sendKeys(password); driver.findElement(By.name("login")).click(); Thread.sleep(5000); Assert.assertTrue(driver.getTitle().matches("Find a Flight: Mercury Tours:"), "Invalid credentials"); System.out.println("Login successful"); } @AfterMethod void ProgramTermination() { driver.quit(); } @DataProvider(name="testdata") public Object[][] TestDataFeed() { ReadExcelFile config = new ReadExcelFile("C:\\Users\\OnlineITGuru\\workspace\\Selenium\\userauthentication.xlsx"); int rows = config.getRowCount(0); Object[][] credentials = new Object[rows][2]; for(int i=0;i In the code shown above, I have created a method named TestDataFeed(). In this method, I have created an object instance under the name ReadExcelFile. While instantiating the object, we have supplied the path that contains the Excel file. Moving further, it contains the for loop to retrieve the data from Excel. And the code for reading the data from the Excel file is shown below:package DataDriven; import java.io.File; import java.io.FileInputStream; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcelFile { XSSFWorkbook wb; XSSFSheet sheet; public ReadExcelFile(String excelPath) { try { File src = new File(excelPath); FileInputStream fis = new FileInputStream(src); wb = new XSSFWorkbook(fis); } catch(Exception e) { System.out.println(e.getMessage()); } } public String getData(int sheetnumber, int row, int column) { sheet = wb.getSheetAt(sheetnumber); String data = sheet.getRow(row).getCell(column).getStringCellValue(); return data; } public int getRowCount(int sheetIndex) { int row = wb.getSheetAt(sheetIndex).getLastRowNum(); row = row + 1; return row; } }In the above code, we have imported the Apache POI XSSF libraries. These libraries were responsible for reading / writing the data to the Excel files. Besides, it also contains the constructor to pass the sheet number, row number as well as column number. Hence like that, a Data-driven framework gets executed.||{"title":"Master in Selenium", "subTitle":"Selenium Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/selenium-training.html","boxType":"demo","videoId":"SsWf3LjoT5Y"}||
Keyword Driven Framework:
In this framework, the instructions and the operations to be performed were written separately. The Working of this framework is similar to that of the data-driven framework. In this case, the operations were nothing but the test cases that need to be executed. The major advantage of this framework is to control the functionalities that you want to test. Moreover, we can specify the methods that test the selenium application functionality in the Excel File. Hence, the main agenda of this framework is to divide the test case into four different parts.Those four steps were given below a) Test step b) Object of the Test step c) Action on the Test Object d) Data for Data ObjectTest step: It is the short description of the test step (or) the action to the performed on the test object. Test Object: It is the name of the Web page/ Element like the username/password. Action: It is the name of the action that needs to be performed on any object such as open browser, clicks as well as input. Test Data: The test data can be any value required by any object to perform various activities like Values for their fields( For example username value for the Username Field).An application is usually capable of performing several different operations. And each operation has a separate code to execute the business logic. And I would like to elaborate this with an example. For instance, consider an application login form containing multiple methods in the main test case. Here each test case does follow a certain functionality. And there could be one method for instantiating the browser driver and there could be another method for retrieving the file data like username and password and there could be another method for navigating to the webpage and so on. We can understand the practical working of this framework with the code shown belowData-Driven Framework Execution code:
package KeywordDriven; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class Actions { public static WebDriver driver; public static void openBrowser() { System.setProperty("webdriver.chrome.driver", "C:UsersVardhanDownloadschromedriver.exe"); driver=new ChromeDriver(); } public static void navigate() { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://newtours.demoaut.com"); } public static void input_Username() { driver.findElement(By.name("userName")).sendKeys("mercury"); } public static void input_Password() { driver.findElement(By.name("password")).sendKeys("mercury"); } public static void click_Login() { driver.findElement(By.name("login")).click(); } @Test public static void verify_login() { String pageTitle = driver.getTitle(); Assert.assertEquals(pageTitle, "Find a Flight: Mercury Tours:"); } public static void closeBrowser() { driver.quit(); } }When you observe the above code different functionalities that need to be tested were present in the separate waiting methods that need to be called. And these methods will be called by another class on the basis of the method present in the database (or) an excel. Similarly to read the Excel file and send back the results. we require the other piece of code with some other class as shown below:package KeywordDriven; public class DriverScript { public static void main(String[] args) throws Exception { //Declaring the path of the Excel file with the name of the Excel file String sPath = "C:\\Users\\Vardhan\\workspace\\Selenium Frameworks Demo\\dataEngine.xlsx"; //Here we are passing the Excel path and SheetName as arguments to connect with Excel file ReadExcelData.setExcelFile(sPath, "Sheet1"); //Hard coded values are used for Excel row & columns for now //Hard coded values are used for Excel row & columns for now //In later chapters we will replace these hard coded values with varibales //This is the loop for reading the values of the column 3 (Action Keyword) row by row for (int iRow=1;iRow<=7;iRow++) { String sActions = ReadExcelData.getCellData(iRow, 1); //Comparing the value of Excel cell with all the keywords in the "Actions" class if(sActions.equals("openBrowser")) { //This will execute if the excel cell value is 'openBrowser' //Action Keyword is called here to perform action Actions.openBrowser(); } else if(sActions.equals("navigate")) { Actions.navigate(); } else if(sActions.equals("input_Username")) { Actions.input_Username(); } else if(sActions.equals("input_Password")) { Actions.input_Password(); } else if(sActions.equals("click_Login")) { Actions.click_Login(); } else if(sActions.equals("verify_Login")) { Actions.verify_login(); } else if(sActions.equals("closeBrowser")) { Actions.closeBrowser(); } } } }Last, but not least, let us move on to theHybrid Framework:
As the name suggests this framework makes use of the above two frameworks. Here for Keywords, we make use of Excel files to maintain the test cases. And for the purpose of the testing framework, we make use of the TestNG Framework. Moreover, in this framework, we do not change anything in the Keyword driven framework. We just replace the Execute.java files with the HybridExecutive.java. Moreover, with the examples shown above, we can build the hybrid framework, by simply storing the methods to execute in an excel file(Keyword-driven approach) and these parameters to the JAVA class instead of if/else loop in the Driver script class. The Driver Script class code is shown below.Hybrid Framework Sample Code:
package HybridFramework; import java.lang.reflect.Method; public class DriverScriptJava { //This is a class object, declared as 'public static' //So that it can be used outside the scope of main[] method public static Actions actionKeywords; public static String sActions; //This is reflection class object, declared as 'public static' //So that it can be used outside the scope of main[] method public static Method method[]; public static void main(String[] args) throws Exception { //Declaring the path of the Excel file with the name of the Excel file String sPath = "C:\\Users\\OnlineITGuru\\workspace\\Selenium Frameworks \\dataEngine.xlsx"; //Here we are passing the Excel path and SheetName to connect with the Excel file //This method was created previously ReadExcelData.setExcelFile(sPath, "Sheet1"); //Hard coded values are used for Excel row & columns for now //Later on, we will use these hard coded value much more efficiently //This is the loop for reading the values of the column (Action Keyword) row by row //It means this loop will execute all the steps mentioned for the test case in Test Steps sheet for (int iRow=1;iRow<=7;iRow++) { sActions = ReadExcelData.getCellData(iRow, 1); //A new separate method is created with the name 'execute_Actions' //You will find this method below of this test //So this statement is doing nothing but calling that piece of code to execute execute_Actions(); } } //This method contains the code to perform some action //As it is completely different set of logic, which revolves around the action only, it makes sense to keep it separate from the main driver script //This is to execute test step (Action) private static void execute_Actions() throws Exception { //Here we are instantiating a new object of class 'Actions' actionKeywords = new Actions(); //This will load all the methods of the class 'Actions' in it. //It will be like array of method, use the break point here and do the watch method = actionKeywords.getClass().getMethods(); //This is a loop which will run for the number of actions in the Action Keyword class //method variable contain all the method and method.length returns the total number of methods for(int i = 0;iMoreover, instead of using the multiple, if Else classes, we can make use of a data-driven approach to read the data from the Excel file. Most of the testers today consider this framework as the most important framework. Besides this, there are some other frameworks that some testers do use. Hence, let us have the glace on these too... Linear Scripting Framework:
This framework involves, recoding (or) replaying the test scripts in a linear fashion. These frameworks execute the scripts with no other modification. And this framework is considered the first generation for automation testing. Besides, this framework has a simple approach for testing the applications. This framework is mainly used for testing the Application user interface. Here the testers record their step-by-step actions and playback the record test.||{"title":"Master in Selenium", "subTitle":"Selenium Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/selenium-training.html","boxType":"reg"}||
Modular Testing Framework:
This framework divides the application under test into the isolated modules. And all these modules were combined to make the large scripts. These frameworks break down the user-interaction with the application into a small test. And the tester recombines it to achieve the specific goals. This framework is easily scalable and comes with a higher level of modularization that results in easier as well as cheaper maintenance. Hence likewise, these frameworks were categorized to test different cases of application on the basis of the requirement.Benefits of using Selenium Frameworks
There are a few valuable benefits of Selenium frameworks in the IT world. These are-
Increase in code reuse
The different frameworks in Selenium help to increase the reusability of existing code.
Code Readability improves
The code writing becomes more precise and simple to understand. Thus, it improves the readability of the code snippet.
Higher rate of portability
The high rate of portability of Software like Selenium makes the usability of the same software in different ecosystems. Generally, portability needs the abstraction between the app logic and the system interfaces. It reduces the development cost of producing the software by using the same functionality for different platforms.
Minimized script maintenance
Due to the use of different frameworks in Selenium, it reduces script maintenance. By applying various strategies like UI/UX testing, build automation design, test execution, etc.
Conclusion
I hope this article, gave you enough idea regarding the various selenium framework with their usage and the advantages of each framework when compared to the other. Moreover, till now you people have gained enough theoretical knowledge on these testing frameworks and can get the same in a practical way at Selenium Online Course. In addition, you people also get the probable selenium interview question on our website. Follow those questions to clear the selenium Certification