Monday 12 September 2016

How Selenium grid works

How Selenium grid works
·         HUB : Hub is a master machine which distributes your tests to registered slave machines (nodes).
·         NODE : Node is a slave machine which takes and executes the commands from the master machine (hub).


                      
Grid consists of Hub and nodes. Single hub and one or more nodes registered to the hub.

The hub receives a test to be executed along with information on which browser (Chrome, IE, Firefox safari etc )and ‘platform’ (i.e. WINDOWS, LINUX, etc) where the test should be run. It has the list of nodes registered under it, it selects an available node that has the requested browser-platform combination and initiates, Selenium commands (testcase) and passes them to the node selected. The node runs the browser, and executes the testcase within that browser against the application under test.

Sunday 11 September 2016

Why do we need Selenium Grid

1.      To test Browser and OS compatibility for UI tests in parallel. For example, you want to test your application for multiple combinations of OS and browser like chrome on Windows 7, 8 and 10. IE 9, 10 and 11 etc.  
2.       To save execution time by distributing the tests on
a.       Multiple machines
b.       Running on multiple browser instances on same machine.

Which will run in parallel.

Grid Installation/Set up

Prerequisite

1.       Java installed on the machine and environment variable set for Java (JAVA_HOME).
2.       Eclipse installed.
3.      TestNG is installed in eclipse.

Steps to install grid

1.       Download selenium server jar from Selenium Downloads Page , you will find it under section “Selenium-Server (formerly Selenium-RC)”.

2.       Extract it in your system say C drive . For Demonstration purposes , I will be assuming the path of selenium server jar is “C:\”.

Next

Selenium Grid Implementation

Java Program to reverse a string

package My;

import java.util.*;

class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to reverse");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      System.out.println("Reverse of entered string is: "+reverse);
   }
}

Java program to reverse a number

package My;

import java.util.Scanner;

class ReverseNumber
{
   public static void main(String args[])
   {
      int n, reverse = 0;

      System.out.println("Enter the number to reverse");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();

      while( n != 0 )
      {
          reverse = reverse * 10;
          reverse = reverse + n%10;
          n = n/10;
      }

      System.out.println("Reverse of entered number is "+reverse);
   }
}

Java Code for Bubble sort

package My;

import java.util.Scanner;

class BubbleSort {
  public static void main(String []args) {
    int n, c, d, swap;
    Scanner in = new Scanner(System.in);

    System.out.println("Input number of integers to sort");
    n = in.nextInt();

    int array[] = new int[n];

    System.out.println("Enter " + n + " integers");

    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    for (c = 0; c < ( n - 1 ); c++) {
      for (d = 0; d < n - c - 1; d++) {
        if (array[d] > array[d+1]) /* For descending order use < */
        {
          swap       = array[d];
          array[d]   = array[d+1];
          array[d+1] = swap;
        }
      }
    }

    System.out.println("Sorted list of numbers");

    for (c = 0; c < n; c++)
      System.out.println(array[c]);
  }
}

Java program to print stars pattern

package My;

class stars {
  public static void main(String[] args) {
    int row, numberOfStars;

    for (row = 1; row <= 10; row++) {
      for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
        System.out.print("*");
      }
      System.out.println(); // Go to next line
    }
  }
}

output :

*
**
***
****
*****

Java Program for Prime number

package My;

import java.util.*;

class PrimeNumbers
{

/*
*
* We have used sqrt method of Math package which find square root
* of a number. To check if an integer(say n) is prime you can check
*  if it is divisible by any integer from 2 to (n-1) or check from 2
*  to sqrt(n), first one is less efficient and will take more time.
*/
   public static void main(String args[])
   {
      int n, status = 1, num = 3;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of prime numbers you want");
      n = in.nextInt();

      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are :-");
         System.out.println(2);
      }

      for ( int count = 2 ; count <=n ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }        
   }
}

Java Program to find factorial

package My;

import java.util.Scanner;

class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;

      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);

      n = in.nextInt();

      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;

         System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}

Java program for binary search

package My;

import java.util.Scanner;

class BinarySearch
{
  public static void main(String args[])
  {
    int c, first, last, middle, n, search, array[];

    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt();
    array = new int[n];

    System.out.println("Enter " + n + " integers");


    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    System.out.println("Enter value to find");
    search = in.nextInt();

    first  = 0;
    last   = n - 1;
    middle = (first + last)/2;

    while( first <= last )
    {
      if ( array[middle] < search )
        first = middle + 1;  
      else if ( array[middle] == search )
      {
        System.out.println(search + " found at location " + (middle + 1) + ".");
        break;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if ( first > last )
      System.out.println(search + " is not present in the list.\n");
  }
}

Java Program to Add two Matrix

package My;

import java.util.Scanner;

class AddTwoMatrix
{
   public static void main(String args[])
   {
      int m, n, c, d;
      Scanner in = new Scanner(System.in);

      System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n  = in.nextInt();

      int first[][] = new int[m][n];
      int second[][] = new int[m][n];
      int sum[][] = new int[m][n];

      System.out.println("Enter the elements of first matrix");

      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the elements of second matrix");

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            second[c][d] = in.nextInt();

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = first[c][d] + second[c][d];  //replace '+' with '-' to subtract matrices

      System.out.println("Sum of entered matrices:-");

      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
            System.out.print(sum[c][d]+"\t");

         System.out.println();
      }
   }
}

Selenium Java Interview Questions

Parameterization in TestNG


Assertions in TestNG

Most Commonly used Assertions in TestNg for automation testing are :

       assertEquals(boolean actual, boolean expected)
     Asserts that two booleans are equal. If not, assertions fails.
       assertEquals(boolean actual, boolean expected, String message)
                 Asserts that two booleans are equal. If not, assertions fails, & message is displayed.
       assertEquals(String actual, String expected)
               Asserts that two Strings are equal. If not, assertions fails.
       assertEquals(String actual, String expected)
               Asserts that two Strings are equal. If not, assertions fails.
       assertEquals(String actual, String expected, String message)
               Asserts that two Strings are equal. If not, assertions fails, & message is displayed.
       assertFalse(boolean condition)
              Asserts that a condition is false. If not, assertions fails.
       assertFalse(boolean condition, String message)
              Asserts that a condition is false. If not, assertions fails, & message is displayed.

       assertNotNull(Object object)
        Asserts that an object isn't null. If not, assertion fails
       assertNull(Object object)
        Asserts that an object is null.
       assertNull(Object object, String message)
        Asserts that an object is null. . If not, assertion fails, & message is displayed.
       assertTrue(boolean condition)
        Asserts that a condition is true. If not, assertion fails.
       assertTrue(boolean condition, String message)

        Asserts that a condition is true. If not, assertion fails, & message is displayed

Next

Skipping a test case in TestNG

package testng.examples;
  import org.testng.Assert;
 import org.testng.annotations.Test;

  public class Ignore {
       @Test(enabled = false)
         public void test() {
              Assert.assertEquals(true, true);
      }
}

Creating Suites in TestNG

In TestNG suites are created using xml file. 

Below is an example.


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  
 <!-- @BeforeSuite -->
<suite name="TestAll">
   <!-- @BeforeTest -->
              <test name="case1">
                     <classes>
                                <class name="com.testng.examples.configuration.Test1" />
                                <class name="com.testng.examples.configuration.Test2" />
                                <class name="com.testng.examples.configuration.Test3" />
                       </classes>
               </test>
<!-- @AfterTest -->
  <!-- @BeforeTest -->
           <test name="case2">
                  <classes>
                         <class name="com.testng.examples..Test1" />
                         <class name="com.testng.examples.Test2" /> 
                   </classes>
            </test>
<!-- @AfterTest -->
 </suite>

<!-- @AfterSuite -->

Next

Parameters passed to @Test annotation

       dataProvider : The name of the data provider for this test method.

       dependsOnMethods : The list of methods this method depends on.

       enabled : Whether methods on this method are enabled.

       groups :  The list of groups this class/method belongs to.

       expectedExceptions : The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.


       timeOut : The maximum number of milliseconds this test should take.

Next

Why do we need TestNG in Selenium?

  1. TestNG can generate reports based on our Selenium test results.
  2. TestNG simplifies the way the tests are coded.
  3. Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.

Next

TestNG Annotations

@Test: The annotated method is a part of a test case

@BeforeMethod: The annotated method will be run before each test method.

@AfterMethod: The annotated method will be run after each test method.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeSuite: The annotated method will be run before all tests in this suite have run.

@AfterSuite: The annotated method will be run after all tests in this suite have run.


@DataProvider : Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation. 

Next

How to Install TestNG ?

In eclipse :

1.       Launch the Eclipse IDE and from Help menu, click “Install New Software”.
2.       On the dialog window, click “Add” button.
3.       Type name as “TestNG” and type http://beust.com/eclipse/” as location. Click OK.
4.       You will now see TestNG option selected, if not selected select the check box and click on Next button.
5.       Click “I accept the terms of the license agreement Finish.
6.       Eclipse will prompt to restart . Click on OK.

Else 
  1. Go to Help -> Eclipse Market Place.
  2.  Search for testng.
  3.  Select testNG option and click on install.
  4. Click “I accept the terms of the license agreement Finish.
  5.  Eclipse will prompt to restart . Click on OK.


Maven dependency :
<dependency>

  <groupId>org.testng</groupId>

  <artifactId>testng</artifactId>

  <version>6.8</version>

  <scope>test</scope>


</dependency>

Next