Saturday 16 June 2012

This covers a basic GWT Selenium WebDriver setup


Download Source

Basic Selenium Setup

0. install the Chrome web driver
    - Chrome WebDriver can be found here: http://code.google.com/p/chromium/downloads/list
1. Install maven m2e eclipse plugin
2. Create plain old java project
3. Convert project to maven project. 
    > right click on project > goto menu configure > Convert to maven project
4. open the pom.xml
5. goto lower right tab in pom.xml to the tab pom.xml, which will show xml source
6. add this to pom.xml:
 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0
  SLP_Testing
  SLP_Testing
  0.0.1-SNAPSHOT
 
       
       
           
                org.seleniumhq.selenium
                selenium-java
                2.14.0
           
       
       
7. wait while the dependencies download. 
8. create a source folder and package and add this class:

package com.gonevertical.client;

import java.io.IOException;
import java.util.Arrays;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * {@link http://c.gwt-examples.com}
 */

@RunWith(BlockJUnit4ClassRunner.class)
public class TestGwtAppLoading extends TestCase {

  private static WebDriver driver;

  @BeforeClass
  public static void initWebDriver() throws IOException {
   
    // set the path to the chrome driver
    // http://code.google.com/p/chromium/downloads/list
    System.setProperty("webdriver.chrome.driver", "/Users/branflake2267/bin/chromedriver");
   
    // chrome driver will load with no extras, so lets tell it to load with gwtdev plugin
    // get plugin here in chrome url type > "chrome://plugins/" > hit top right details + > find gwt dev mode pluging location:
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    String gwtDevPluginPath = "--load-plugin=/Users/branflake2267/Library/Application Support/Google/Chrome/Default/Extensions/jpjpnpmbddbjkfaccnmhnkdgjideieim/1.0.9738_0/Darwin-gcc3/gwtDev.plugin";
    capabilities.setCapability("chrome.switches", Arrays.asList(gwtDevPluginPath));
   
    // init driver with GWT Dev Plugin
    driver = new ChromeDriver(capabilities);
  }

  @AfterClass
  public static void theEnd() {
    driver.quit();
  }

  @Before
  public void before() {
  }

  @After
  public void after() {
  }

  @Test
  public void testElement() {
   
   
    // navigate to gwt app - change this to your app url
    String pathToGwtApp = "http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997";
    driver.get(pathToGwtApp);


    // Don't forget to add the inherits Debug && set textAreaWidget.ensureDebugId("myTextAreaId"); in your gwt app code
    WebElement elementRta = driver.findElement(By.id("gwt-debug-myTextAreaId"));
    // do something with the element
   
   
    // set a break point on this, b/c there isn't any pausing
    System.out.println("finished");
   
  }
 
}

Finding Elements in Selenium with ensureDebugId

Selenium will need to reference the widgets by there id using widget.ensureDebugId("seleniumElementReferencetId"); in your code. 

1. add   to your project.gwt.xml file
2. If in your code your textAreaWidget.ensureDebugId("myTextAreaId"); is like then
3. Reference the element in selenium driver.findElement(By.id("gwt-debug-myTextAreaId"));
4. If your unsure it is working use the developer tools and search for the elementId.


ClickAndWait for a page to load

This snippet is loading my application and then clicking on the Google login link then selecting isAdmin and then clicking on login and back to the application as a logged in (dev) user. Also showing below is the method of waiting for completion of page load before moving on. 
  @Test
  public void testSignIn() {
   
    // navigate to gwt app
    String pathToGwtApp = "http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997";
    driver.get(pathToGwtApp);

    // wait till the app is ready for use
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver d) {
        WebElement element = driver.findElement(By.id("gwt-debug-Login_SignIn"));
        return element.isEnabled();
      }
    });
   
    WebElement element = driver.findElement(By.id("gwt-debug-Login_SignIn"));
    element.click();
   
    // wait till where on the dev login page
    (new WebDriverWait(driver, 2)).until(new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver d) {
        WebElement isAdmin = driver.findElement(By.id("isAdmin"));
        return isAdmin.isEnabled();
      }
    });
   
    // click on isAdmin
    WebElement isAdmin = driver.findElement(By.id("isAdmin"));
    isAdmin.click();
   
    // click on login
    WebElement form = driver.findElement(By.xpath("/html/body/form/div/p[3]/input[1]"));
    form.click();
   
    // wait till we get back to the user logged in with there tabs
    (new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver d) {
        WebElement element = driver.findElement(By.id("gwt-debug-tabsSchoolUser"));
        return element.isEnabled();
      }
    });
   
    // set a break point on this, b/c there isn't any pausing
    System.out.println("finished");
  }


reference
http://c.gwt-examples.com/home/testing/selenium-testing