AdvanceTopics();

Happy new year folks, let us start talking Selenium, please add interested folks to this group and participate in discussion. When we talk selenium , let us talk on Selenium2 or webdriver which is more Object Oriented than traditional Selenium RC API. We will first discuss on how to convert selinium to webdriver. Let us take the basic example:
Selenium selenium = new DefaultSelenium(
"localhost", 4444, "*firefox", "http://www.google.com/");
selenium.start();
this is now be:
WebDriver driver = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.google.com/");

Now about the magic, if you want WebDriver implementation from the Selenium instance then cast wrap driver as:
WebDriver driver = ((WrapsDriver) selenium).getWrappedDriver();
or if you want default selenium to be used in webdriver then use:
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

**Some precautions*****

WebDriver more accurately emulates user behavior, so when you are using WebDriverBackedSelenium events get triggered all along you send request. for example, in Selenium, we use
selenium.type("name", "logging in");
selenium.keyDown("name", "n");
selenium.keyPress("name", "n");
selenium.keyUp("name", "n");
will be send as logging innn in webdriver.

for making the driver to wait for an element use:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
this will ask the driver, to continue if driver finds an element, else wait for 30 seconds!

Selenium2 - Driver Tips:

How to stop firefox ‘update failed’ dialog messing with your WebDriver automation :

Start firefox with a profile and set the "app.update.silent" firefox property to true.
The update error will still happen, but at least firefox won’t try and tell your automated processes about it.


profile.setPreference("app.update.silent", true);

For more details visit http://kb.mozillazine.org/Category:Preferences


Adding Firefox profile inside code for Selenium 1 and 2 (TestNG):


for selenium 2:

        FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("network.proxy.type", 1);
    firefoxProfile.setPreference("network.proxy.http", "172.20.53.8");
    firefoxProfile.setPreference("network.proxy.http_port", 3128);
    firefoxProfile.setPreference("network.proxy.ssl", "172.20.53.8");
    firefoxProfile.setPreference("network.proxy.ssl_port", 3128);
    firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
    FirefoxDriver driver = new FirefoxDriver(firefoxProfile);
 

for selenium 1:
   
    RemoteControlConfiguration rc= new RemoteControlConfiguration();
    rc.setPort(4444);
    rc.setFirefoxProfileTemplate(new File("/home/valuelabs/bijoypro/"));
   

Starting SeleniumServer at code level:

SeleniumServer seleniumserver=new SeleniumServer(rc);
    seleniumserver.boot();
        seleniumserver.start();
        setUp("http://www.snapfish.com", "*chrome");
     
        selenium.open("/");
        selenium.windowMaximize();
        selenium.windowFocus();
     



an example using httpunitdriver without invoking the real browser!

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement
;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example {
public static void main(String[] args) {

WebDriver driver = new HtmlUnitDriver();

// visit Google
driver.get("http://www.google.com/");

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
element.sendKeys("Bijoy Meethal!");

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
}


DataDriven Testing using Selenium:

package Script;

import com.thoughtworks.selenium.*;

import org.junit.AfterClass;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;

import java.io.File;
import jxl.*;


public class dataProviderExample extends SeleneseTestCase{
   
  @BeforeClass
    public void setUp() throws Exception {
      SeleniumServer seleniumserver=new SeleniumServer();
      seleniumserver.boot();
      seleniumserver.start();
        setUp("http://www.imdb.com/", "*firefox");
        selenium.open("/");
        selenium.windowMaximize();
        selenium.windowFocus();
    }

   
    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception{
        Object[][] retObjArr=getTableArray("test\\Resources\\Data\\data1.xls",
                "DataPool", "imdbTestData1");
        return(retObjArr);
    }
   
    @Test (dataProvider = "DP1")
    public void testDataProviderExample(String movieTitle,
            String directorName, String moviePlot, String actorName) throws Exception {   
        //enter the movie title
        selenium.type("q", movieTitle);
        //they keep switching the go button to keep the bots away
        if (selenium.isElementPresent("nb15go_image"))
            selenium.click("nb15go_image");
        else
        selenium.click("xpath=/descendant::button[@type='submit']");
       
        selenium.waitForPageToLoad("60000");
        //click on the movie title in the search result page
        selenium.click("xpath=/descendant::a[text()='"+movieTitle+"']");
        selenium.waitForPageToLoad("60000");
        //verify director name is present in the movie details page
        verifyTrue(selenium.isTextPresent(directorName));
        //verify movie plot is present in the movie details page
        verifyTrue(selenium.isTextPresent(moviePlot));
        //verify movie actor name is present in the movie details page
        verifyTrue(selenium.isTextPresent(actorName));
    }
   
    @AfterClass
    public void tearDown(){
        selenium.close();
        selenium.stop();
    }
   
    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;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
       

        return(tabArray);
    }
   
   
}//end of class



Free Online JavaScript books:

    Eloquent JavaScript has an earlier version than the print book, but still very good, with an excellent section on Objects.
    Essential JavaScript Design Patterns provides a concise introduction to various usage patterns.
    JQuery Fundamentals
    Building iPhone Apps with HTML, CSS, and JavaScript from O’Reilly Open Feedback Publishing System a good general intro to HTML, CSS and JavaScript

    http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/
    http://tools.css3.info/selectors-test/test.html

Comments

Popular Posts