Saturday, 31 May 2014

How to create Object repository in Selenium and How to use in Selenium Webdriver

One of the interview questions asked 

Step 1- Open note pad

Step 2- Put key and value pairs for example

facebook.login.username.xpath=.//*[@id='email']
facebook.login.password.xpath=.//*[@id='pass']
facebook.login.Signup.xpath=.//*[@id='loginbutton']

Step 3- Save file as .properties file (I saved in project home diretory and inside Object_Repo folder and name of object repository is facebook.properties

Step 4- Write Selenium script and use the same in script

Lets implement the same

package ObjectRepoExample;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TestFacebook {

@Test
public void TestOR() throws IOException{


File src=new File("./Object_Repo/facebook.properties");

FileInputStream fis=new FileInputStream(src);

Properties pro=new Properties();

pro.load(fis);

System.out.println("Property class loaded");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

driver.findElement(By.xpath(pro.getProperty("facebook.login.username.xpath"))).sendKeys("Selenium@gmail.com");

driver.findElement(By.xpath(pro.getProperty("facebook.login.password.xpath"))).sendKeys("adsadasdas");

driver.findElement(By.xpath(pro.getProperty("facebook.login.Signup.xpath"))).click();


}



}

How to highlight elements in Selenium Webdriver

One of the interview questions asked is how to highlight the web elements in the page

Below is the answer 
In Selenium we can use JavascriptExecutor (interface) to execute Javascript code into webdriver

In this post we will execute Javascript which will highlight the element.

Lets implement the same this will highlight the user name field

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



public class Highlight {


public static void main(String []args){

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

               // Create the  JavascriptExecutor object
JavascriptExecutor js=(JavascriptExecutor)driver;

WebElement username= driver.findElement(By.id("email"));

               // call the executeScript method
js.executeScript("arguments[0].setAttribute('style,'border: solid 2px red'')", username); // this code creates a block of solid red color around the email id text box
}

}


if we want to just blink and go off as we have an option in QTP we can add one more line of code 

js.executeScript("arguments[0].setAttribute('style,'border: solid 2px red'')", username); // this code creates a block of solid red color around the email id text box   
js.executeScript("arguments[0].setAttribute("style,'border: "),username) // this code removes the block , so this above 2 lines of code will resemble the highlighting operation 

Any comments please paste in Comment section

Database Testing (MS access) using Selenium

Selenium does not support Database Testing but partially we can do.


Approach- We can connect to Database using ODBC and verify whether data (DB table)is updated or not.

Let see how to connect with Database using ODBC Driver

These are the simple steps to follow

Step 1- Load the driver

Step 2-Create a connection

Step 3- Create a statement

Step 4- Execute your query

Step 5- Store the data in Result set

Step 6- Verify whether data (table) is updated or not

Lets implement the same using Java and DB as Oracle

Precondition- Set the DSN (Data Source Name)

Step to setup DSN-

1- Control Panel > Administrative tool > Click on ODBC

2- Go to user DSN and Click on Add button

3- Select the respective database which you want to connect in this case I selected Oracle 11G

4- Give the name to DSN and Save

Here we go


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.testng.annotations.Test;

public class TestDatabase {



@Test
public void TestVerifyDB(){

try {
                       
                         // This will load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");



    // Specify the path of the database

String dblocation= "C:\\Users\\Desktop\\DB\\FacebookDB1.accdb";    
    // This will connect with Database , getConnection taking three argument
    //  first argument Test_Oracle is the dsn which you can change as per your system,
Connection con=DriverManager.getConnection("jdbc:odbc:AscentAccess;DBQ="+dblocation);            

System.out.println("Connection created");

   // This will create statement
       Statement smt=con.createStatement();
         
       System.out.println("Statement created");

   // Now execute the query, here facebook is the table which I have created in DB
   
ResultSet rs=  smt.executeQuery("Select * from Facebook");

System.out.println("Query Executed");

  // Iterate the resultset now

      while(rs.next()){
 
String uname=    rs.getString("username");
String pass=        rs.getString("password");
String email=      rs.getString("email");

System.out.println("Uname is "+uname+" password is "+pass+" email id is "+email);
 
 }
}
catch (Exception e) {
System.out.println(e.getMessage());
}


}

}

Tuesday, 27 May 2014

MindTree



Selenium:

1. how to handle dynamic object
2. how to work with button which is in div tag and and u have to click without using xpath
3. JVM is dependent or independent platform
4.how many Test script you write in day
5. describe your framework
6. how to parameterized your junit
7.how to handle ssl security
8. how to handle window pops
9. diffnct between implicit and explicit
10.what are the types of assertion and what are assertion in junit

Java

1.JVM is dependent or independent platform
2.diffn bw hashmap and hash set, set and linkedlist, arraylist and vector list , linkedhash set and hashset
3.abstract and interface
4.throw and throws
5.how to split
6.checked and unchecked exception
7.how to work with azax aplication
8.why sring is immutable
9.wat is the retru ntype of getwindowhandles();
10.what are the types of assertion and what are assertion in java

Manhattan Associates 2

Face to Face Interview Questions 

1.what is the meaning of this line
        WebDriver driver=new FirefoxDriver();
        can we write firefoxDriver d=new firefoxDriver();

2.login on facebook and description of each method
3.can we use other function behalf of get() to go on specific url.
4.how do you log the defact in manual testing.
5.Defect life cycle.
6.there is 2 textbox id password.something is already written in text field like
"Emailid",how can you copy from emailid field and paste in password field.
7.about yourself.
8.project description.
9.About my current company environment.
10.puzzle:-3 bulb and 3 switches problem.
11.How to handle the drop downlist.
12.overloading and overridding
13.it's a overloading or overridding will it compile or not.
        public int m1(int a,int b)
        public float m1(int c,int d)

14.we have a excel sheet some thing is written in excel sheet then compare those value
with web page values.
15.how many bugs have you found yet.tell me 2 complex bug.
16.string reverse .
17. find highest value in list.
18.find the duplicate value in list.
19.how will upload a file if the text is not editable.
20.write inner query .

Manhattan Associates.

1st round:written test

10 question from testing
10 question including analytical and logical and puzzle
10 questions from database to predict output
10 question from java to predict output


2nd round with technical panel:

1.xpath for dynamic objects(they gave 1 table structure)

2.webdriver code to read the data from excel sheet and login into application

3.code for database connectivity

4.how to handle exception

5.Automation test cycle

6.fuctions to find objects in UI

7.code to click on a link which will open another window and perform an operation on 2nd window to open a frame and then perform an operation on a frame

8.types of wait in webdriver and difference

9.code to work with IE and settings required for this

10.which framework u used in your company and why

11.what is dataprovider why we use dataprovider

12.what is pagefactory and why we use page factory

13.what is SVN

14.what is selenium grid and why we use this

15.which test cases can't be automated

16.Interface and why we use it

17.diffrence between interface and abstract class,and when we use interface,when we use abstract class

18.explain oop's concept

19.bug life cycle

20.regression,compatibility,adhoc,smoke,useability.load,stability,security testing and why we do these testing

21.functional,integration,system testing,and what is module,what is component

22.test case design techniques,give an example of boundary value analysis

23.what is severity and priority

24.when we set bug status as deferred

25.which bug tracking tool u used in ur company

26.explain test scenario and test case

27.throws for runtime or compile time? and try catch for runtime or compile time?



3rd round:(technical and managerial with director)

1.what is class and why we use class

2.what is inheritence and why we use it,write a code

3.WebDriver code to print username in the welcome page

4.code to find a particular word from a string and explain line by line

5.code to write a java program (any program)

6.explain public static void main

7.access specifiers and their use

8.difference between primary key and unique key

10.write test case template which u follow in ur company

11.difference between array and linklist

12.7-8 managerial questions

Basic Sorting

Sorting


1. Write a Program for Bubble Sort.
import java.io.*;
public class BubbleSort{
 public static void main(String args[])throws Exception
 {
   BufferedReader br1=new BufferedReader(new  InputStreamReader(System.in));
   System.out.print("Enter the Size of the Array: ");
   int size=Integer.parseInt(br1.readLine());
   int arr[]=new int[size];
   for(int a=0;a
   {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.print("Enter The "+(a+1)+" Element=");                                             
     arr[a]=Integer.parseInt(br.readLine());
   }
   for(int i=0;i<arr.length;i++)
   {
     for(int k=1;k<arr.length;k++)
     {
       if(arr[k-1]>arr[k])
       {
         int j=arr[k-1];
         arr[k-1]=arr[k];
         arr[k]=j;
       }
     }
   }
   System.out.print("Sorted Elements are: ");
   for(int b=0;b
   {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.print(" "+arr[b]);
   }
 }
}

Bubble sort Example: 6, 5, 1, 7, 2, 4, 9
Array size=7
5,6,1,7,2,4,9  
5,1,6,7,2,4,9
5,1,6,7,2,4,9
5,1,6,2,7,4,9
5,1,6,2,4,7,9
5,1,6,2,4,7,9
=============
1,5,6,2,4,7,9
1,5,2,6,4,7,9
1,5,2,4,6,7,9
=============
1,2,5,4,6,7,9
1,2,4,5,6,7,9
=============

2. Write a Program for Selection Sort.
 public void SelectionSort()
{
   for (int b=arr.length-1;b>0;b--)
   {
      int maxPosition=0;
      for(int c=1;c<=b;c++)
      {
         if(arr[c]>arr[maxPosition])
         maxPosition=c;
      }
      int l=arr[maxPosition];
      arr[maxPosition]=arr[b];
      arr[b]=l;
      }
}

Selection sort Example:
4, 3, 5, 6, 1
Maxposition=2
4, 3, 5, 6, 1
Maxposition=3
4, 3, 5, 6, 1
=============
4, 3, 5, 1, 6
4, 3, 5, 1, 6
=============
4, 3, 1, 5, 6
=============
1, 3, 4, 5, 6

3. Write a Program for Insertion Sort.

public void InsertionSort()
{
  long timeNs=System.nanoTime();
  for(int i=1;i<arr.length;i++)
  {
    int current=arr[i];
    int j=i;
    while(j>0 && arr[j-1]>current)
    {
      arr[j]=arr[j-1];
      j--;
    }
    arr[j]=current;
  }
  nanoTime=System.nanoTime()-timeNs;
}