Monday, 16 September 2013

Selenium Automation Framework Design (Data Driven Framework)

Data Driven  Frame Work 

Data Driven framework  means using a single test to verify many different of possible inputs which is given by the end user.Data-driven testing to separate test logic from test data by replacing hard-coded values in your automated tests with the variable. We can use use Spread sheets or 
Sql for same.
 
How to access excel sheet data by using java code

1.If we need to test  Log in functionality with different 2  user id and password then  we create  xls file and create odbc connection .
2 How to established odbc Connection

-start>>Control pannel>>Administrative tools>>Datasource>>User Dsn>>Add >>Driver>>driver do Microsoft xls>>select your excel file >>save
3.Java Selenium code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

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

public class Excel {
   
   
    public static void main(String[] argv) throws Exception {
       
        WebDriver driver=new FirefoxDriver();
        String baseUrl = "http://excilysbank.aws.af.cm/";
        driver.get(baseUrl);
       
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:excel");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("Select * from [Sheet1$]");

           
           
            String UserId,Password;
            while (rs.next()) {
       
           
            UserId=rs.getString("UserId");
            System.out.println(UserId);
           
           
           
             
            Password=rs.getString("Password");
            System.out.println(Password);
       
           
             driver.get(baseUrl + "/public/login.html");
                driver.findElement(By.id("username")).sendKeys(UserId);
                driver.findElement(By.id("password")).sendKeys(Password);
                driver.findElement(By.cssSelector("input.button.blue")).click();
                driver.findElement(By.linkText("PERSONNAL_CHECKING")).click();
                driver.findElement(By.linkText("November")).click();
                driver.findElement(By.linkText("December")).click();
                driver.findElement(By.linkText("January")).click();
                driver.findElement(By.linkText("February")).click();
                driver.findElement(By.linkText("Log out")).click();
           


       

        }
           

            st.close();
            con.close();
            } catch (Exception ex) {
            System.err.print("Exception: ");
            System.err.println(ex.getMessage());
            }
       
       
     
    }
     

  }



Thursday, 5 September 2013

About Selenium Web driver

Selenium is UI and function testing tools so we test our web site ,web application  using selenium.Selenium is open source tools Download here 
Now let we discuss about  selenium web driver.
Selenium WebDriver is the successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenese, or via a Client API) and sends them to a browser. This is implemented through a browser-specific browser driver, which sends commands to a browser, and retrieves results. Most browser drivers actually launch and access a browser application

 Requirment
 Linux ,windows ,Eclipse for java ,Visual studio for C#(Nunit tools for test C# test case)

Step

1.Download Rc server Jar files
2. Start eclipse
3.File >> New Java Projects
4.Create Class
5.Build path>> Configure Path >>Add Libraries>> Junit
6.Build path>> Configure Path >>Add Externals Jar>> seleinum jar
7. Write A Test Case in a Class 
8, Test case Run as Junit


Code:
public class Test {
  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 = "http://excilysbank.aws.af.cm/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void test() throws Exception {
    driver.get(baseUrl + "/public/login.html");
    driver.findElement(By.id("username")).sendKeys("user1");
    driver.findElement(By.id("password")).sendKeys("password1");
    driver.findElement(By.cssSelector("input.button.blue")).click();
    driver.findElement(By.linkText("PERSONNAL_CHECKING")).click();
    driver.findElement(By.linkText("November")).click();
    driver.findElement(By.linkText("December")).click();
    driver.findElement(By.linkText("January")).click();
    driver.findElement(By.linkText("February")).click();
    driver.findElement(By.linkText("Log out")).click();
  }

  @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;
    }
  }
}

Functional Testing With Selenium

Assertion is the technique to compare the test result .

There are 2 way  to assert the text in Web Driver.
 1.driver.getPageSource().contains("String")
 2.WebElement el = driver.findElement(By.xpath("xpath"));

Example: 
Code:

   public void test() throws Exception {

        driver.get(baseUrl + "/public/login.html");
        driver.getPageSource().contains("Welcome to Excilys Bank");

  WebElement el = driver.findElement(By.xpath("/html/body/div/div/div[2]"));
     Thread.sleep(3000);
       
        driver.findElement(By.id("username")).sendKeys("user1");

        driver.findElement(By.id("password")).sendKeys("password1");

        driver.findElement(By.cssSelector("input.button.blue")).click();

              }

Or

 public void test() throws Exception {

        driver.get(baseUrl + "/public/login.html");
       

  WebElement el = driver.findElement(By.xpath("/html/body/div/div/div[2]")); 

     Thread.sleep(3000);
       
        driver.findElement(By.id("username")).sendKeys("user1");

        driver.findElement(By.id("password")).sendKeys("password1");

        driver.findElement(By.cssSelector("input.button.blue")).click();


 driver.getPageSource().contains("Welcome to Excilys Bank"); 

After run ,it searching the text "Welcome to Excilys Bank" in complete page 
if text is getting then it pass this test and script is continue else  getting error on the script.
Performance Testing With Jmeter