Selenium Testing - SPLessons

Selenium Test Frameworks

Home > Lesson > Chapter 17
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Selenium Test Frameworks

Selenium Test Frameworks

shape Description

Selenium Test Frameworks are thought to be a blend of set conventions, principles, measures and rules that can be taken as whole in order to influence the advantages of the platform gave by the framework. If frameworks wont exist, then it is hard to get appropriate reports, handle the checkpoints. There is a different scope of Automation Frameworks accessible now a days. These systems might contrast from another based on their backing to various key elements to do computerization like re-usability, simplicity of maintenance. Modular, Keyword, Data drivenHybrid  are the four prominent test computerization methodologies.

Data Driven

shape Description

Test information is isolated from test scripts and results are returned against the test information. Lastly if all the test information combo are pass, then just the experiment is dealt with as passed. In the event that any of the test information blend is fizzled, then the whole experiment will be dealt with as Fail.

Advantages

Following are the advantages of Data Driven:

Modular

shape Description

Module construct Testing Framework is based with respect to one of the famously known OOPS idea – Abstraction. The structure separates the whole "Application Under Test" into number of consistent and different modules. For every module, make a different and free test script. In this manner, when these test scripts taken together forms a bigger test script speaking to more than one modules.

Keyword

shape Description

Keywords are produced which are equivalent to a unit level usefulness. It is an application free structure using information tables techniques and keychwords to perform the activities.

Advantages

Following are the advantages of Modular Frame work:
Here is the basic Keyword driven structure sample

shape Step 1

Characterize a class called KeyWordExample, which will have all the resuable strategies, driver summon.

shape Step 2

Enter the below code [java] //This is an example for the Selenium Test Frameworks. package com.keyword.sample; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.WebDriverWait; public class KeyWordExample { static WebDriver driver; static WebDriverWait wait; public void open_Browser(String browserName) { try { if (browserName.equalsIgnoreCase("Firefox")) { driver = new FirefoxDriver(); } else if (browserName.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.chrome.driver", "D:/Jars/chromedriver.exe"); driver = new ChromeDriver(); } else if (browserName.equalsIgnoreCase("IE")) { System.setProperty("webdriver.ie.driver", "D:/Jars/IEDriverServer.exe"); driver = new InternetExplorerDriver(); } } catch (WebDriverException e) { System.out.println(e.getMessage()); } } public void enter_URL(String URL) { driver.navigate().to(URL); } public By locatorValue(String locatorTpye, String value) { By by; switch (locatorTpye) { case "id": by = By.id(value); break; case "name": by = By.name(value); break; case "xpath": by = By.xpath(value); break; case "css": by = By.cssSelector(value); break; case "linkText": by = By.linkText(value); break; case "partialLinkText": by = By.partialLinkText(value); break; default: by = null; break; } return by; } public void enter_Text(String locatorType, String value, String text) { try { By locator; locator = locatorValue(locatorType, value); WebElement element = driver.findElement(locator); element.sendKeys(text); } catch (NoSuchElementException e) { System.err.format("No Element Found to enter text" + e); } } public void click_On_Link(String locatorType, String value) { try { By locator; locator = locatorValue(locatorType, value); WebElement element = driver.findElement(locator); element.click(); } catch (NoSuchElementException e) { System.err.format("No Element Found to enter text" + e); } } public void click_On_Button(String locatorType, String value) { try { By locator; locator = locatorValue(locatorType, value); WebElement element = driver.findElement(locator); element.click(); } catch (NoSuchElementException e) { System.err.format("No Element Found to perform click" + e); } } public void close_Browser() { driver.quit(); } } [/java] Here the developer is using multiple browsers such as Google, Internet Explore and Firefox. [java] browserName.equalsIgnoreCase("Firefox") else if (browserName.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.chrome.driver", "D:/Jars/chromedriver.exe"); lse if (browserName.equalsIgnoreCase("IE")) { System.setProperty("webdriver.ie.driver", "D:/Jars/IEDriverServer.exe"); [/java] Here catch block has been used to describe the exception. [java] catch (WebDriverException e) { System.out.println(e.getMessage()); } [/java] The driver.navigate().to(URL); method is used to write any web site for the testing, here provided text method and link method and button method to process the exception. [java] public void enter_Text(String locatorType, String value, String text) public void click_On_Link(String locatorType, String value) public void click_On_Button(String locatorType, String value) [/java]

shape Step 3

Characterize different class called KeyWordExecution, which assumes the liability of recovering the information from excel sheet, distinguish the locators and parameters and conjure the separate techniques in the "KeyWordExample" class. [java] //This is an example for the Selenium Test Frameworks. package com.keyword.sample; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class KeyWordExecution { public void runReflectionMethod(String strClassName, String strMethodName, Object... inputArgs) { Class<?> params[] = new Class[inputArgs.length]; for (int i = 0; i <inputArgs.length; i++) { if (inputArgs[i] instanceof String) { params[i] = String.class; } } try { Class<?> cls = Class.forName(strClassName); Object _instance = cls.newInstance(); Method myMethod = cls.getDeclaredMethod(strMethodName, params); myMethod.invoke(_instance, inputArgs); } catch (ClassNotFoundException e) { System.err.format(strClassName + ":- Class not found%n"); } catch (IllegalArgumentException e) { System.err .format("Method invoked with wrong number of arguments%n"); } catch (NoSuchMethodException e) { System.err.format("In Class " + strClassName + "::" + strMethodName + ":- method does not exists%n"); } catch (InvocationTargetException e) { System.err.format("Exception thrown by an invoked method%n"); } catch (IllegalAccessException e) { System.err .format("Can not access a member of class with modifiers private%n"); e.printStackTrace(); } catch (InstantiationException e) { System.err .format("Object cannot be instantiated for the specified class using the newInstance method%n"); } } public static void main(String[] args) { KeyWordExecution exeKey = new KeyWordExecution(); ReadExcel excelSheet = new ReadExcel(); excelSheet.openSheet("D:/testCaseSheet.xls"); for (int row = 1; row <excelSheet.getRowCount(); row++) { List<Object> myParamList = new ArrayList<Object>(); String methodName = excelSheet.getValueFromCell(0, row); for (int col = 1; col < excelSheet.getColumnCount(); col++) { if (!excelSheet.getValueFromCell(col, row).isEmpty() !excelSheet.getValueFromCell(col, row).equals("null")) { myParamList.add(excelSheet.getValueFromCell(col, row)); } } Object[] paramListObject = new String[myParamList.size()]; paramListObject = myParamList.toArray(paramListObject); exeKey.runReflectionMethod("com.keyword.sample.KeyWordExample", methodName, paramListObject); } } } [/java] The InvocationTargetException method is used if the underlying method throws an exception. ClassNotFoundException is one of the java startegy, From the name java.lang.ClassNotFoundException looks quite simple but underlying cause of it is always different and which classifies it as an environmental issue. System.err and System.out both are setup by OS when the Java code is executed.

shape Step 4

Selenium Test Frameworks make a different class to peruse the excel sheet. Use jxl library to persuse the information from excel, and additionally utilize 'Apache POI' to do likewise. [java]package com.keyword.sample; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class ReadExcel { Workbook wbWorkbook; Sheet shSheet; public void openSheet(String filePath) { FileInputStream fs; try { fs = new FileInputStream(filePath); wbWorkbook = Workbook.getWorkbook(fs); shSheet = wbWorkbook.getSheet(0); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getValueFromCell(int iColNumber, int iRowNumber) { return shSheet.getCell(iColNumber, iRowNumber).getContents(); } public int getRowCount() { return shSheet.getRows(); } public int getColumnCount() { return shSheet.getColumns(); } }[/java] Here just created the class ReadExcel and also created FileInputStream class that can obtain input bytes from the file, it is utilized to read the raw bytes such as image. The getMessage() or getSheet() techniques for Throwable class acquired by each special case class like ArithmeticException . The getMessage() technique prints just the message part of the yield printed by an object e. This style can be favored when the developer might not want to give his message to the real client.

shape Step 5

The output is appeared beneath in the excels document which has given four columns.

Hybrid

shape Description

Hybrid system is the blend of both data-driven and keyword driven testing structures. It permits information driven scripts to exploit the capable libraries and utilities in a keyword based methodology.

Advantages

Following are the advantages of Hybrid Framework:

Summary

shape Key Points

  • In Keyword driven structure, keywords are composed in some outer documents like exceed expectations record and java code will call this document and execute test cases.
  • Testing system is dependably application autonomous that it can be utilized with any application.