cn.hxh.springside.test.functional.Selenium2.java Source code

Java tutorial

Introduction

Here is the source code for cn.hxh.springside.test.functional.Selenium2.java

Source

/**
 * Copyright (c) 2005-2011 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Selenium2.java 1631 2011-06-28 22:20:35Z calvinxiu $
 */
package cn.hxh.springside.test.functional;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import cn.hxh.springside.utils.ThreadUtils;

import com.thoughtworks.selenium.Selenium;

/**
 * ??Selenium 1.0 APISelenium 2.0ByAPI.
 *
 * @author calvin
 */
public class Selenium2 {

    public static final int DEFAULT_TIMEOUT = 5000;
    public static final int DEFAULT_PAUSE_TIME = 250;

    private WebDriver driver;
    private Selenium selenium;
    private int defaultTimeout = DEFAULT_TIMEOUT;

    public Selenium2(WebDriver driver, String baseUrl) {
        this.driver = driver;
        this.selenium = new WebDriverBackedSelenium(driver, baseUrl);
    }

    /**
     * ?baseUrl, open?. 
     */
    public Selenium2(WebDriver driver) {
        this(driver, "");
    }

    /**
     * ?,url?, baseUrl.
     * @param url
     */
    public void open(String url) {
        selenium.open(url);
        waitForPageToLoad();
    }

    /**
     * ??.
     */
    public String getTitle() {
        return driver.getTitle();
    }

    /**
     * ???.
     */
    public String getLocation() {
        return driver.getCurrentUrl();
    }

    /**
     * Element.
     */
    public WebElement findElement(By by) {
        return driver.findElement(by);
    }

    /**
     * ??Element.
     */
    public List<WebElement> findElements(By by) {
        return driver.findElements(by);
    }

    /**
     * ??.
     */
    public boolean isTextPresent(String text) {
        return selenium.isTextPresent(text);
    }

    /**
     * ??Element.
     */
    public boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    /**
     * Element???.
     */
    public boolean isDisplayed(By by) {
        return driver.findElement(by).isDisplayed();
    }

    /**
     * Element.
     */
    public void type(By by, String text) {
        WebElement element = driver.findElement(by);
        element.clear();
        element.sendKeys(text);
    }

    /**
     * Element.
     */
    public void click(By by) {
        driver.findElement(by).click();
    }

    /**
     * Element, ?.
     */
    public void clickTo(By by) {
        driver.findElement(by).click();
        waitForPageToLoad();
    }

    /**
     * Element.
     */
    public void check(By by) {
        WebElement element = driver.findElement(by);
        element.click();
    }

    /**
     * ?Element.
     */
    public void uncheck(By by) {
        WebElement element = driver.findElement(by);
        if (element.isSelected()) {
            element.click();
        }
    }

    /**
     * Element?.
     */
    public boolean isChecked(By by) {
        WebElement element = driver.findElement(by);
        return element.isSelected();
    }

    /**
     * ?Element.
     */
    public String getText(By by) {
        return driver.findElement(by).getText();
    }

    /**
     * ??, ?0, Selnium1.0.
     */
    public String getTable(WebElement table, int rowIndex, int columnIndex) {
        return table.findElement(By.xpath("//tr[" + (rowIndex + 1) + "]//td[" + (columnIndex + 1) + "]")).getText();
    }

    /**
     * ??, ?0, Selnium1.0.
     */
    public String getTable(By by, int rowIndex, int columnIndex) {
        return getTable(driver.findElement(by), rowIndex, columnIndex);
    }

    /**
     * ??, timeoutdefaultTimeout.
     */
    public void waitForPageToLoad() {
        waitForPageToLoad(defaultTimeout);
    }

    /**
     * ??, timeout??.
     */
    public void waitForPageToLoad(int timeout) {
        selenium.waitForPageToLoad(String.valueOf(timeout));
    }

    /**
     * Element, timeout??.
     */
    public void waitForVisible(By by, int timeout) {
        long timeoutTime = System.currentTimeMillis() + timeout;
        while (System.currentTimeMillis() < timeoutTime) {
            if (isDisplayed(by)) {
                return;
            }
            ThreadUtils.sleep(DEFAULT_PAUSE_TIME);
        }
        throw new RuntimeException("waitForVisible timeout");
    }

    /**
     * Selenium.
     */
    public void quit() {
        driver.close();
        driver.quit();
    }

    /**
     * ?WebDriver, ?.
     */
    public WebDriver getDriver() {
        return driver;
    }

    /**
     * ?Selenium,?.
     */
    public Selenium getSelenium() {
        return selenium;
    }

    /**
     * ?.
     */
    public void setDefaultTimeout(int defaultTimeout) {
        this.defaultTimeout = defaultTimeout;
    }
}