Thursday, 10 October 2013

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.

No comments:

Post a Comment