com.androidwhy.modules.test.selenium.Selenium2.java Source code

Java tutorial

Introduction

Here is the source code for com.androidwhy.modules.test.selenium.Selenium2.java

Source

/**
 * Copyright (c) 2009-2012 androidwhy.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.androidwhy.modules.test.selenium;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

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

    public static final int DEFAULT_WAIT_TIME = 20;
    private WebDriver driver;
    private String baseUrl;

    public Selenium2(WebDriver driver, String baseUrl) {
        this.driver = driver;
        this.baseUrl = baseUrl;
        setTimeout(DEFAULT_WAIT_TIME);
    }

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

    /**
     * JVMSelenium?
     */
    public void setStopAtShutdown() {
        Runtime.getRuntime().addShutdownHook(new Thread("Selenium Quit Hook") {
            @Override
            public void run() {
                quit();
            }
        });
    }

    // Driver   //
    /**
     * ?,url?, baseUrl.
     */
    public void open(String url) {
        final String urlToOpen = url.indexOf("://") == -1 ? baseUrl + (!url.startsWith("/") ? "/" : "") + url : url;
        driver.get(urlToOpen);
    }

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

    /**
     * ??
     */
    public void back() {
        driver.navigate().back();
    }

    /**
     * ?
     */
    public void refresh() {
        driver.navigate().refresh();
    }

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

    /**
     * Selenium.
     */
    public void quit() {
        try {
            driver.quit();
        } catch (Exception e) {
            System.err.println("Error happen while quit selenium :" + e.getMessage());
        }
    }

    /**
     * ?Element
     */
    public void setTimeout(int seconds) {
        driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
    }

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

    //Element //

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

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

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

    /**
     * Element???.
     */
    public boolean isVisible(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 check(By by) {
        WebElement element = driver.findElement(by);
        check(element);
    }

    /**
     * Element.
     */
    public void check(WebElement element) {
        if (!element.isSelected()) {
            element.click();
        }
    }

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

    /**
     * ?Element.
     */
    public void uncheck(WebElement element) {
        if (element.isSelected()) {
            element.click();
        }
    }

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

    /**
     * Element?.
     */
    public boolean isChecked(WebElement element) {
        return element.isSelected();
    }

    /**
     * Select Element,?????Select?.
     * eg. s.getSelect(by).selectByValue(value);
     */
    public Select getSelect(By by) {
        return new Select(driver.findElement(by));
    }

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

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

    /**
     * ?Inputvalue.
     */
    public String getValue(WebElement element) {
        return element.getAttribute("value");
    }

    // WaitFor  //
    /**
     * Element??, timeout??.
     */
    public void waitForVisible(By by, int timeout) {
        waitForCondition(ExpectedConditions.visibilityOfElementLocated(by), timeout);
    }

    /**
     * Elementtext, timeout??.
     */
    public void waitForTextPresent(By by, String text, int timeout) {
        waitForCondition(ExpectedConditions.textToBePresentInElement(by, text), timeout);
    }

    /**
     * Elementvaluevalue, timeout??.
     */
    public void waitForValuePresent(By by, String value, int timeout) {
        waitForCondition(ExpectedConditions.textToBePresentInElementValue(by, value), timeout);
    }

    /**
     * Conditions, timeout??.
     * 
     * @see #waitForTextPresent(By, String, int)
     * @see ExpectedConditions
     */
    public void waitForCondition(ExpectedCondition conditon, int timeout) {
        (new WebDriverWait(driver, timeout)).until(conditon);
    }

    // Selenium1.0  //

    /**
     * ???.
     */
    public boolean isTextPresent(String text) {
        String bodyText = driver.findElement(By.tagName("body")).getText();
        return bodyText.contains(text);
    }

    /**
     * ??, ?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);
    }

}