Tuesday, October 18, 2011

Separate Firefox profile with SeleniumRC


package com.sample;


import com.thoughtworks.selenium.*;


import java.io.File;
import java.util.regex.Pattern;


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


public class Test67 extends SeleneseTestCase {
public Selenium selenium;
public Selenium settingup()throws Exception {
File template = new File("C:\\Documents and Settings\\sasidhar\\Application Data\\Mozilla\\Firefox\\Profiles\\Selenium");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setFirefoxProfileTemplate(template);
SeleniumServer seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://google.com/");
seleniumServer.start();
selenium.start();
selenium.open("https://google.com");
selenium.windowFocus();
selenium.windowMaximize();
return selenium;
}

public void testUntitled() throws Exception {
selenium.open("about:home");
selenium.type("id=searchText", "sasidhar");
selenium.click("id=searchSubmit");
selenium.waitForPageToLoad("30000");
}
}

Friday, October 14, 2011

Webdriver - Drag And Drop

package script;
import org.openqa.selenium.By;
import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.SeleneseTestCase;

public class DragAndDrop extends SeleneseTestCase{
    public static void main (String[] arg)
    {
        WebDriver driver = new FirefoxDriver();
        WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, "http://www.webreference.com/programming/javascript/mk/column2/");
        driver.get("http://www.webreference.com/programming/javascript/mk/column2/");
        selenium.windowFocus();
        selenium.windowMaximize();
        RenderedWebElement from = (RenderedWebElement) driver.findElement(By.xpath("html/body/div[5]/div[2]/fieldset[1]/div/div[1]/div[1]"));
        RenderedWebElement to = (RenderedWebElement) driver.findElement(By.xpath("html/body/div[5]/div[2]/fieldset[1]/div/div[1]/div[2]"));
        from.dragAndDropOn(to);
        }
}

Web Driver - Handling Multiple Windows

package script;
import java.util.Set;

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


public class HandlingWindows  {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, "http://www.way2sms.com/");
        driver.get("http://www.way2sms.com/");
        selenium.windowFocus();
        selenium.windowMaximize();
        driver.findElement(By.xpath(".//*[@id='header']/div/div/div[2]/a")).click();
        selenium.waitForPageToLoad("30000");
        System.out.println("Page title is: " + driver.getTitle());
        driver.findElement(By.id("username")).click();
        Set allWindowHandles = driver.getWindowHandles();
        String window0 = (String) allWindowHandles.toArray()[0];
        System.out.println("Window0 String: "+ window0);
        driver.switchTo().window(window0);
        String win0Title = selenium.getTitle();
        System.out.println("Window0 Title: "+ win0Title);
        String window1 = (String) allWindowHandles.toArray()[1];
        System.out.println("Window1 String: "+ window1);
        driver.switchTo().window(window1);
        String win1Title = selenium.getTitle();
        System.out.println("Window1 Title: "+ win1Title);     
     }
  }

Thursday, October 13, 2011

DataProvider in Selenium

f

Data driven testing with Selenium

Data Driven Testing with Selenium:


Testing multiple data with same script.

the main intension of this testing is to find the behaviour 
of the application for different input values.

When i am executing my Test Cases which are same action up to some milestone. so I am are doing the same steps with different data... then I thought why should i do like this and i thought the data driven, how to do..first i thought for reusing the code.. then i created a class file then i am using the same class for all test cases in the class i am taking the values from xl file..

then i tried with java...then its working fine now..


package com.org.selected.datadriven;

import com.thoughtworks.selenium.*;
import java.io.File;
import java.sql.Time;
import java.util.regex.Pattern;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Blank;

import org.openqa.selenium.internal.seleniumemulation.Click;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class Datadriventest_firefox36 extends SeleneseTestCase {


Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="30000";
private SeleniumServer seleniumServer;



@BeforeTest
public void setUp() throws Exception {

File template = new File("C:\\Documents and Settings\\username\\Application Data\\Mozilla\\Firefox\\Profiles\\Selenium");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setFirefoxProfileTemplate(template);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "URL");
seleniumServer.start();
selenium.start();
selenium.open("URL");
selenium.windowFocus();
selenium.windowMaximize();
}


@DataProvider(name = "Test")
// Test is hte xl file name, sheet1 is the sheet name and test1 is the table name.
public Object[][] createPayId() throws Exception{
    Object[][] retObjArr=getTableArray("D:\\workspace\\DemoProject\\datafiles\\Test.xls", "sheet1", "test1"); // what is these two ( sheet names )
    return(retObjArr);
}


@Test (dataProvider = "Test", description="Testing ")
public void Testpayments(String col1, String col2, String col3 )throws Exception {
selenium.setSpeed("500");
selenium.select("name=sn", xxx );
    pause(3000);
verifyTrue(selenium.isElementPresent("name=display"));
verifyTrue(selenium.isElementPresent("id=userid"));
verifyTrue(selenium.isElementPresent("name=rand_userid"));
selenium.select("name=aaa", col1);
selenium.select("name=livemode", col2);
selenium.type("id=amount", col3);
    selenium.click("id=Update");
pause(3000);

}

public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
    String[][] tabArray=null;
 
        Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
        Sheet sheet = workbook.getSheet(sheetName);
        int startRow,startCol, endRow, endCol,ci,cj;
        Cell tableStart=sheet.findCell(tableName);
        startRow=tableStart.getRow();
        startCol=tableStart.getColumn();


Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);              
        endRow=tableEnd.getRow();
        endCol=tableEnd.getColumn();
        System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                "startCol="+startCol+", endCol="+endCol);
        tabArray=new String[endRow-startRow-1][endCol-startCol-1];
        ci=0;








Wednesday, October 12, 2011

Firefox profile for your Selenium RC

Why create a new firefox profile for Selenium?


We create firefox profile for Selenium RC tests, so we can set light configurations to it, which later on we’ll be using to run our Selenium scripts FASTER.

How to create Firefox profile for your Selenium?


1.Make sure all your firefox instance are closed
2.Click Start>Run
3.Type “firefox.exe -ProfileManager -no-remote”
4.Select “Create Profile” (i.e. selenium)
5.Click “Next”
6.Enter new profile name
7.Select a directory folder to store your new profile
8.Click “Finish”
9.Select “Don’t ask at startup”
10.Click “Start Firefox” and configure settings based on suggestion below***
11.Set Profile back to “default” (enable you to use your previous settings on your browser)
12.Add -firefoxProfileTemplate command line option as you start the Selenium Server

java -jar selenium-server.jar -firefoxProfileTemplate “

Suggested settings for your Selenium Profile


1.From “View\Toolbars” tab, uncheck “Bookmarks Toolbar”
2.Right click from toolbar and click “Customize”
3.Remove “Google search” by dragging it to the “Customize Toolbar” window
4.From the “Customize Toolbar” window, click “Use Small Icons” check box then hit “Done”
5.Click “Tools\Options” then set the following:
a. “Main” tab
- set Home Page to “about:blank”
- uncheck “Show the Downloads..” option
b. “Tabs” tab
- Select “a new window” for new pages
- Uncheck all warning options
c. “Content” tab
- uncheck “Block pop-up” windows option
d. “Privacy” tab
- uncheck all “History” options
e. “Security” tab
- uncheck all “Security” options
- click “Settings” and uncheck all warning options
f. “Advanced” tab
- Uncheck “autoscrolling” option from “General” tab
- uncheck “warn me …” and “Ssearch Engines”option from “Update” tab
6.From “Tools\Add-ons” install the following:
- Firebug: allows you to edit, debug, and monitor CSS, HTML, and JavaScript on your application under test
- Selenium IDE: allows you to record, edit, and debug Selenium tests
- ScreenGrab: saves entire webpages as images.
7.From the address bar type “about:config” and add the following by right-click anywhere on the page and selecting “new”
- extensions.update.notifyUser (type=boolean; value=false)
- extensions.newAddons (type=boolean; value=false)