Thursday, 10 October 2013

Modular Framework

Modular Framework:
The all the separated functions are shown below along with Package name and Class name
Package name: com.Google.Common
Class name: BaseTest.Java  This class will initialize the firefox driver
package com.Google.Common;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;


public class BaseTest {
     
        public static WebDriver driver;
       
  @BeforeSuite
        public void setUp() throws Exception {
          driver = new FirefoxDriver();
        
          driver.manage().window().maximize();     
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        }
       
        @AfterSuite
        public void tearDown() throws Exception {
          driver.quit();
         
        }

}

Package name: com.Google.Common
Class name: SignOut.Java  The logout functionality is sepearted so that it can be used at any time from any screen of application.

package com.Google.Common;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class SignOut {
     
      public void LoginToApp(WebDriver driver)
      {
            driver.findElement(By.xpath("//a[contains(@href, 'https://plus.google.com/u/0/me?tab=wX')]")).click();
          driver.findElement(By.id("gb_71")).click();
                 
      }
}

The above two methods are the examples for the modular driven framework. Think which part of the functionality has chances to repeat again and again and divide the functionalities in to small chunks which can be called later point of time where ever required as shown above.

Note: Login functionality is also created as separate functionality but the script is added under Data driven because at this point of time understanding login script is little bit confusion.

Linear Framework


Understanding of each automation frameworks, by automating the google log in scenario. Using selenium 2 and TestNG with ant build. The below is the step by step representation of developing a automation framework.

Linear Framework:

1. Record the below mentioned scenario
·         Click on SIGN IN
·         Enter User name
·         Enter Password
·         Click on Sign in

After recording the Selenium IDE look like below.
 

2. Navigate to Options >> Format and select Java/Junit/WebDriver.

Note: 1. Observe the above screenshot that entering a value for “User name

“ and “Password” is not been recorded, so we have to spy the objects through the firebug to find the object Id or name and write the code for writing in textboxes.

2. As for TestNG there is no WebDriver script hence we are selecting Junit WebDriver script and will change accordingly in Eclipse.

After selecting the IDE will be displayed with the WebDriver script supported to Junit
 
3. Open Eclipse select the work space and do the below
·         Create project with project name as“GoogleMailProject”.
·         Right click on new project and create package name as “com.Google.Main” .
·         Right click on package and create class name as GoogleLogin.
·         Copy the WebDriver script from IDE and paste it in newly created class.
The WebDriver script generated by IDE is as below

 

package com.example.tests;

import java.util.regex.Pattern;

import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;

import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

public class Untitled {

private WebDriver driver;

private String baseUrl;

private boolean acceptNextAlert = true;

private StringBuffer verificationErrors = new StringBuffer();

@Before

public void setUp() throws Exception {

driver = new FirefoxDriver();

baseUrl = "https://www.google.co.in/";

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Test

public void testUntitled() throws Exception {

driver.get(baseUrl + "/");

assertEquals("Google", driver.getTitle());

driver.findElement(By.id("gb_70")).click();

assertEquals("Google Accounts", driver.getTitle());

driver.findElement(By.id("signIn")).click();

assertEquals("Google Accounts", driver.getTitle());

assertEquals("Google", driver.getTitle());

}

@After

public void tearDown() throws Exception {

driver.quit();

String verificationErrorString = verificationErrors.toString();

if (!"".equals(verificationErrorString)) {

fail(verificationErrorString);

}

}

private boolean isElementPresent(By by) {

try {

driver.findElement(by);

return true;

} catch (NoSuchElementException e) {

return false;

}

}

private boolean isAlertPresent() {

try {

driver.switchTo().alert();

return true;

} catch (NoAlertPresentException e) {

return false;

}

}

private String closeAlertAndGetItsText() {

try {

Alert alert = driver.switchTo().alert();

String alertText = alert.getText();

if (acceptNextAlert) {

alert.accept();

} else {

alert.dismiss();

}

return alertText;

} finally {

acceptNextAlert = true;

}

}

}

4. To solve the errors
·         To solve error “Cannot be resolved of Type”install the below mentioned jar files
   o    selenium-server-standalone-2.35.0.jar
   o    reportng-1.1.2.jar
·         Create the folder “lib” under your project and copy and paste both files under lib folder
·         Set path for the copied jar files, Right click on Project select Build Path >> Configure Build Path
·         Select the Add External Jars.. button and navigate to the location where the jar files are kept and select the files and click on Open button.
·         Some other errors which occurs due to the TestNG we are using but the script we copied is Junit so need to change the script as per the TestNG annotations support and it can be solved easily by intelligence populated. After the change of script it looks as below (added the code for writing value in textboxes).

packagecom.Google.Common;

 

importjava.util.regex.Pattern;

importjava.util.concurrent.TimeUnit;

import staticorg.hamcrest.CoreMatchers.*;

importorg.openqa.selenium.*;

importorg.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

importorg.testng.Assert;

importorg.testng.annotations.AfterSuite;

importorg.testng.annotations.BeforeSuite;

import org.testng.annotations.Test;

public class GoogleLogin {

private WebDriver driver;

private String baseUrl;

private boolean acceptNextAlert = true;

private StringBuffer verificationErrors = new StringBuffer();

@BeforeSuite

public void setUp() throws Exception {

driver = newFirefoxDriver();

baseUrl = "https://www.google.co.in/";

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Test

public void testUntitled() throws Exception {

driver.get("https://www.google.co.in/");

Assert.assertEquals("Google", driver.getTitle());

driver.findElement(By.id("gb_70")).click();

Assert.assertEquals("Google Accounts", driver.getTitle());

driver.findElement(By.id("Email")).clear();

driver.findElement(By.id("Email")).sendKeys("abc@gmail.com");

driver.findElement(By.id("Passwd")).clear();

driver.findElement(By.id("Passwd")).sendKeys("XXX");

driver.findElement(By.id("signIn")).click();

Assert.assertEquals("Google Accounts", driver.getTitle());

Assert.assertEquals("Google", driver.getTitle());

}

@AfterSuite

public void tearDown() throws Exception {

driver.quit();

String verificationErrorString = verificationErrors.toString();

/*if (!"".equals(verificationErrorString)) {

fail(verificationErrorString);

}*/

}

private boolean isElementPresent(By by) {

try {

driver.findElement(by);

return true;

} catch(NoSuchElementException e) {

return false;

}

}

private boolean isAlertPresent() {

try {

driver.switchTo().alert();

return true;

} catch (NoAlertPresentException e) {

return false;

}

}

private String closeAlertAndGetItsText() {

try {

Alert alert = driver.switchTo().alert();

String alertText = alert.getText();

if (acceptNextAlert) {

alert.accept();

} else {

alert.dismiss();

}

return alertText;

} finally {

acceptNextAlert = true;

}

}

}

5. Right click and select option Run As >> 1 TestNG Test and the script runs perfectly.

This is just a linear approach also called as Linear scripting framework. We can convert this to a modular by dividing the Login, Logout and the driver initialization functionality separately.

Wednesday, 9 October 2013

Automation Framework

Selenium for beginners:

Automation framework nothing but a set of rules or guidelines and standards defined to follow, by following these rules and standards we can achieve beneficial results
A set of guidelines like coding standards, test-data handling, object repository treatment etc... Which when followed during automation scripting produces beneficial outcomes like increase code re-usage, higher portability, reduced script maintenance cost etc.
There are 5 different types of automation frameworks

1.     Linear Framework: It is script generated by tool when user records the particular scenario. If required can be changed to some extent to make it easily understandable.
2.     Structured or Modular Framework: The script uses control structures - like ‘if-else’,‘switch’, ‘for’, ‘while’. Reusable functionalities will be identified here and divide them as small methods which can be called anywhere within the test case independently whenever required.
3.     Data driven Framework: When the requirement is something like want to execute a particular functionality for the different set of data, changing the hardcoded values or variable values again and again it’s a tough work. Here where the concept of external files comes in to picture, we can easily maintain the set of data outside the test and can interact with those files through scripts. The data can be stored in a excel sheets, CSV files or data base.
4.     Keyword driven Framework: The Keyword-driven tests look very similar to manual test cases. In a keyword-driven test, the functionality of the application-under-test is documented in a table as well as in step-by-step instructions for each test. The main difference of Data driven and Keyword driven is, in Keyword driven we can have control over the execution of the test scripts whereas in data driven we don’t have control over the script execution outside the tests.
5.     Hybrid Framework:The combination of above any two frameworks makes the hybrid framework.
 
Understanding of each automation frameworks, by automating the google log in scenario. Using selenium 2 and TestNG with ant build. The below is the step by step representation of developing a automation framework.