Java tutorial
/** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ package org.cc.demo.test; 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.WebDriverWait; /** * ??Selenium 1.0 APISelenium 2.0ByAPI. * * @author calvin */ public class Selenium2 { private 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; setImplicitlyWait(DEFAULT_WAIT_TIME); } /** * ?baseUrl, open?. */ public Selenium2(WebDriver driver) { this(driver, null); } // Driver // /** * ?,url?, baseUrl. */ public void open(String url) { final String urlToOpen = url.indexOf("://") == -1 ? baseUrl + (!url.startsWith("/") ? "/" : "") + url : url; driver.get(urlToOpen); } /** * ??. */ public String getTitle() { return driver.getTitle(); } /** * Selenium. */ public void quit() { driver.quit(); } /** * ?Element */ private void setImplicitlyWait(int seconds) { driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS); } /** * ?WebDriver, ?. */ public WebDriver getDriver() { return driver; } //Elemenet // /** * Element. */ public WebElement findElement(By by) { return driver.findElement(by); } /** * ??Element. */ public List<WebElement> findElements(By by) { return driver.findElements(by); } /** * ??Element. */ public boolean isPresent(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 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(); } /** * ?Element. */ public String getText(By by) { return driver.findElement(by).getText(); } /** * ?Inputvalue. */ public String getValue(By by) { return driver.findElement(by).getAttribute("value"); } /** * ?Inputvalue. */ public String getValue(WebElement element) { return element.getAttribute("value"); } // WaitFor // /** * Element??, timeout??. */ public void waitForVisible(By by, int timeout) { (new WebDriverWait(driver, timeout)).until(ExpectedConditions.visibilityOfElementLocated(by)); } /** * Elementtext, timeout??. */ public void waitForTextPresent(By by, String text, int timeout) { (new WebDriverWait(driver, timeout)).until(ExpectedConditions.textToBePresentInElement(by, text)); } /** * Elementvaluevalue, timeout??. */ public void waitForValuePresent(By by, String value, int timeout) { (new WebDriverWait(driver, timeout)).until(ExpectedConditions.textToBePresentInElementValue(by, value)); } /** * Conditions, timeout??. * * @see ExpectedConditions */ @SuppressWarnings("unchecked") 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); } }