Parameter Annotation In Junit
Parameter annotation work same as we do in DataProvider annotation
Here we use 2 annotations
   1à Parameter annotation
   2à Run With Annotation

@Parameter annotation
Method under the parameter annotation is a static method
This return a list
Value returned by Parameter annotation need to be assigned to variable in constructor


@RunWith annotation
@RunWith(Parameterized.Class)
Need to pass it before class to show that class will run with parameters


package org.abc;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.chrome.ChromeDriver;



// Paramter annotation in junit
@RunWith(value=Parameterized.class)
public class Case2 {
      String uname, pass;
      public Case2(String uname, String pass)
      {
            this.uname = uname;
            this.pass = pass;
      }
     
      @Test
      public void TestCase()
      {
             System.setProperty("webdriver.chrome.driver","C:\\Documents and Settings\\Administrator\\My Documents\\Downloads\\chromedriver_win32_2.1\\chromedriver.exe");
             ChromeDriver f = new ChromeDriver();
             f.get("http://mail.in.com");
             f.findElementById("f_id").sendKeys(uname);
             f.findElementById("f_pwd").sendKeys(pass);
             f.findElementByClassName("signin");
      }
     
      @Parameters
      public static List Para()
          {
            Object arr[][]={{"uname","pass"},
                                {"uname1","pass1"},
                                {"unam22","pass2"}};
            return Arrays.asList(arr);
            }
      }




Parameterisation in JUNIT


         CONVERT SELENIUM SERVER SCRIPT TO WEBDRIVER FORMAT


 à Use Selenium Server. Jar which is version2 or more, which provide support of web driver.

à Add this jar to build path of the project

à Import

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.x86.*;

à In @Before annotation

Selenium selenium = new defaultSelenium() // in selenium-1

Replace this with
FirefoxDriver f = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, url)

à Remove Start method, Remove SeleniumTestCase Which is extended with Class

à Run, It will run successfully


package org.abc.xyz;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.x86.*;




public class Tcase1  {

      @Test
      public void testIN_100() throws Exception {
            DefaultSelenium selenium;
            FirefoxDriver f = new FirefoxDriver();
            selenium = new WebDriverBackedSelenium(f, "http://mail.in.com");
            selenium.open("/");
            selenium.type("id=f_id", "tnitin");
            selenium.type("id=f_pwd", "nitin150784");
            selenium.click("//input[@value='' and @value='' and @type='submit']");
            selenium.waitForPageToLoad("30000");
            selenium.click("link=Sign out");
            selenium.waitForPageToLoad("30000");
      }

}


                         




Backward compatibility in Webdriver

                     What is WEB DRIVER

à New Enhancement to Selenium 2.0.
à We need not to start selenium Server
à Interact with Browser Directly using browser native support(Not using Java Core etc)
à Support HTML Dom as well, so we can work on multiple elements together.
à Support Iphone application automation as well
à Support android application automation.
à More Object oriented API than RC
à HtmlUnitDriver:- is much faster because it run test case in memory not in physical.

à We have Native Drivers for each Browser sp we need to make implements separately for each driver.

WEB DRIVER

Implementation of Wait in Selenium

1à Thread.sleep() :  This is the way by which we can hold the execution of script for the predefined time
è It is forcefully wait for the given time



2à Implicitely Wait
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

The ImplicitWait will tell the webdriver to wait  certain duration when trying to find the element, this will be useful when certain elements on the webpage will not be available immediately and needs some time to load.
This will work only with find element commands

3à pageLoadTimeout()
By this we can wait for a certain period of time to wait for a page to load.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);



4à Wait for Element Present: By this we can wait until a element present or application state to be changes

WebDriverWait wait = new WebDriverWait(firef, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id=':gp']/div/div")));



Implementation of Wait in Selenium



Running Selenium Test Suite for Regression

For this we need small software called ANT, it must be installed and configured to my system(Till the time we have ant with eclipse, now need to set ANT with my system)


Apache Ant ( Build Automation Tool)

Ant is an open source tool for creating builds, it is used to automation repetitive task which we do while creating build, like compiling source code, creating jars javadoc etc.

Ant uses a xml file for configuration.

This xml file contain Target,
 Target means that action/steps need to be performed, it can have  multiple dependent/independent targets for example if a Target A is dependent to Target be then Ant will execute Target B before A.

We can also specify main Target. Main target means target that will be execute per default this main target is dependent to other target that Ant will  execute other targets then this main target.



                        Configure ANT to my system

1à First Check Ant Exist in your system or Not ?

  Go to command prompt and write    “ant –version”

è If it display any version then OK otherwise we need to configure ant to my system



Here it shows version, means I have ant in my system

2à If no version display means, you don’t have ant in your system

è Go to Eclipse Folder, inside it, we have plugins folder, inside that we have apache ant folder



Go to inside bin folder and copy this path

C:\Documents and Settings\Administrator\Desktop\eclipse-java-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\bin


3à We need to set this path in PATH environment variable of my machine

Right Click on My Computerà PropertiesàAdvanced à Click on Environment Variable button à In System Variable à go to PATH variable and Click on Edit


Go to Start if this variable value textbox and paste the bin folder path we copies in step 2 and place semicolon(;)




Now press all OK buttons

4à Again Check Ant Exist in your system or Not ?

  Go to command prompt and write    “ant –version”

Here it will display some version, so ANT is configured now




                            Execute Test Suite without Opening Eclipse



1à Copy the location where my project is placed(location to the folder where build.xml exists)

C:\Documents and Settings\Administrator\workspace1\JavaTest1


2à Start Command prompt and move to that location


3à Now just write ant, it will start execution



                                       Create bat file for execution

Place all these command in a bat file

è Open notepad
è Write in first line : cd C:\Documents and Settings\Administrator\workspace1\JavaTest1
è In second line write : ant


Save it with any name with .bat extension



Now whenever we want to execute our test suite, just click on this bat file

Running Selenium Test Suite

Difference between CaptureScreenshot and CaptureEntirePageScreenshot

àCaptureScreenshot
     à Supported in all browsers
     à Take screenshot of display screen with OS controls like start button etc
     à Take only 1 argument(file name with path)
   

à CaptureEntirePageScreen
     à Supported on Mozilla for IE need addon.
     à Take screenshot of complte AUT page, irrespective to scroll bar etc.
     à Take 2 arguments(file path , background colour)


Difference between CaptureScreenshot and CaptureEntirePageScreenshot



                                    Working with Selenium Server API.


While working with selenium server first we need to setup configuration of selenium server, like on which port the server will run, Avoid proxy setting, start logs etc.

For working with Selenium server configuration, we need to perform following tasks.

1à Add selenium server jar file in build path of current project.



2à Import org.openqa.selenium.sever.RemoteControlConfiguration;

Here we have imported selenium sever Rcmote Control Configuration Class, we can need to create object of that class and then we can use methods of the class to ser remote control server configuration.


3à Create RemoteControlConfiguration object

RemoteControlConfiguration rc = new RemoteControlConfiguration();


4à Set Port

 rc.setPort(4444);

5à Set Proxy

 rc.setAvoidProxy(true)

6à Set Browser side logs

rc.setBrowserSideLogsEnabled(true)

By this way we can set as much settings as we want in RemoteControlConfiguration


RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setPort(4444);
rc.setAvoidProxy(true);
rc.setBrowserSideLogEnabled(true);

////////////////////////////// CODE /////////////////////////////////

package org.selenium.server;
import org.openqa.selenium.server.RemoteControlConfiguration;

public class SeleniumServer{

public void startServer()
 {
      try
      {
       RemoteControlConfiguration rc = new RemoteControlConfiguration();
       rc.setPort(4444);
       rc.setAvoidProxy(true);
       rc.setBrowserSideLogEnabled(true);
       
      }
      catch(Exception ex){}
 }

}

Run Selenium Server using Java code