package com.yk.userlive.base;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
/**
* @author Seiitsu 2016年9月5日
*/
public class YKWebTestAPI {
private String value = “”;
private boolean flag = true;
private static WebDriver driver = null;
/*
* public void setWebDriver(WebDriver driver) { this.driver = new
* InternetExplorerDriver(); }
*
* public WebDriver getWebDriver() { return this.driver = new
* InternetExplorerDriver(); }
*/
public static WebDriver myDriver(String browser) {
String proxyIpAndPort= “localhost:12104”;
DesiredCapabilities cap = new DesiredCapabilities();
Proxy proxy=new Proxy();
proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
System.setProperty(“http.nonProxyHosts”, “localhost”);
cap.setCapability(CapabilityType.PROXY, proxy);
if (“firefox”.equals(browser.toLowerCase())) {
// 啟動本機firefox
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile(“default”);
// 啟動浏覽器
driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
} else if (“ie”.equals(browser.toLowerCase())) {
// 關閉保護模式
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capability.setCapability(“ignoreProtectedModeSettings”, true);
// 指定驅動位置,並加載驅動
System.setProperty(“webdriver.ie.driver”, “tools/IEDriverServer64.exe”);
driver = new InternetExplorerDriver(capability);
driver.manage().window().maximize();
} else if (“chrome”.equals(browser.toLowerCase())) {
// 指定驅動位置,並加載驅動
System.setProperty(“webdriver.chrome.driver”, “tools/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
} else {
System.out.println(“==============”);
}
return driver;
}
/**
* 根據name定位元素並輸入內容
*
* @param name
* @param value
*/
public boolean inputByName(String name, String value) {
if (flag) {
try {
driver.findElement(By.name(name)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據classname定位元素並點擊
*
* @param name
*/
public boolean clickByClassName(String name) {
if (flag) {
try {
driver.findElement(By.className(name)).click();;
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據linkText定位元素並點擊
*
* @param name
*/
public boolean clickByLinkText(String name) {
if (flag) {
try {
driver.findElement(By.linkText(name)).click();;
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 執行js方法
*
* @param js
*/
public boolean excuteJS(String js) {
if (flag) {
try {
((JavascriptExecutor) driver).executeScript(js);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據id定位元素並輸入內容
*
* @param id
* @param value
*/
public boolean inputById(String id, String value) {
if (flag) {
try {
driver.findElement(By.id(id)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並輸入內容
*
* @param xpath
* @param value
*/
public boolean inputByXpath(String xpath, String value) {
if (flag) {
try {
driver.findElement(By.xpath(xpath)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並輸入內容
*
* @param css
* @param value
*/
public boolean inputByCss(String css, String value) {
if (flag) {
try {
driver.findElement(By.cssSelector(css)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
public boolean clickBindCard(String cardNo) {
flag = false;
String value = “”;
int i = 0;
if (!driver.findElement(By.id(“r_x”)).isDisplayed()) {
for (i = 0; i < 5; i++) {
value = driver.findElement(By.id(“t_b_” + i)).getText();
if (value.contains(cardNo)) {
flag = true;
System.out.println(“t_b_” + i);
driver.findElement(By.id(“sel_img_” + i)).click();
}
}
} else {
while (driver.findElement(By.id(“r_x”)).isDisplayed()) {
System.out.println(“i>>” + i);
if (i > 0 && i % 5 == 0) {
driver.findElement(By.id(“r_x”)).click();
}
value = driver.findElement(By.id(“t_b_” + i)).getText();
System.out.println(value + “>>” + value.contains(cardNo));
if (value.contains(cardNo)) {
flag = true;
System.out.println(“t_b_” + i);
driver.findElement(By.id(“sel_img_” + i)).click();
break;
}
i++;
}
}
return flag;
}
/**
* 根據id定位元素並點擊
*
* @param id
*/
public boolean clickById(String id) {
if (flag) {
try {
if (id.startsWith(“sel_img_”) && !id.contains(“sel_img_ct”)) {
int i = Integer.parseInt(id.substring(8, id.length()));
int j = 4;
while (i > 3) {
driver.findElement(By.id(“sel_img_” + j)).click();
i -= 4;
j += 4;
}
}
driver.findElement(By.id(id)).click();
System.out.println(“click element by id>>” + id);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並點擊
*
* @param xpath
*/
public boolean clickByXpath(String xpath) {
if (flag) {
try {
driver.findElement(By.xpath(xpath)).click();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並點擊
*
* @param css
*/
public boolean clickByCss(String css) {
if (flag) {
try {
driver.findElement(By.cssSelector(css)).click();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據id定位元素並連續點擊
*
* @param id
*/
public boolean clickById(String id, int count) {
if (flag) {
try {
if (id.startsWith(“sel_img_”) && !id.contains(“sel_img_ct”)) {
int i = Integer.parseInt(id.substring(8, id.length()));
int j = 4;
while (i > 3) {
driver.findElement(By.id(“sel_img_” + j)).click();
i -= 4;
j += 4;
}
}
for (int i = 0; i < count; i++) {
driver.findElement(By.id(id)).click();
}
System.out.println(“click element by id>>” + id);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並連續點擊
*
* @param xpath
*/
public boolean clickByXpath(String xpath, int count) {
if (flag) {
try {
for (int i = 0; i < count; i++) {
driver.findElement(By.xpath(xpath)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並連續點擊
*
* @param css
*/
public boolean clickByCss(String css, int count) {
if (flag) {
try {
for (int i = 0; i < count; i++) {
driver.findElement(By.cssSelector(css)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素獲取值
*
* @param xpath
* @return
*/
public String getValueByXpath(String xpath) {
if (flag) {
value = driver.findElement(By.xpath(xpath)).getText();
System.out.println();
return value;
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}
/**
* 根據id定位元素獲取值
*
* @param id
* @return
*/
public String getValueById(String id) {
if (flag) {
value = driver.findElement(By.id(id)).getText();
System.out.println(value);
return value;
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}
/**
* 根據css定位元素獲取值
*
* @param css
* @return
*/
public String getValueByCss(String css) {
if (flag) {
value = driver.findElement(By.cssSelector(css)).getText();
System.out.println(value);
return value;
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}
/**
* 根據id定位元素並清空值
*
* @param id
*/
public boolean clearInputValueById(String id) {
if (flag) {
try {
driver.findElement(By.id(id)).clear();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並清空值
*
* @param xpath
*/
public boolean clearInputValueByXpath(String xpath) {
if (flag) {
try {
driver.findElement(By.xpath(xpath)).clear();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並清空值
*
* @param css
*/
public boolean clearInputValueByCss(String css) {
if (flag) {
try {
driver.findElement(By.cssSelector(css)).clear();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 獲取網頁的title值
*
* @return
*/
public String getTitle() {
if (flag) {
return driver.getTitle();
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}
/**
* 切換到frame框
*
* @param frameName
*/
public boolean switchToFrame(String frameName) {
if (flag) {
try {
driver.switchTo().frame(frameName);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據id定位元素並獲取元素的顯示狀態
*
* @param id
* @return boolean
*/
public boolean getDisplayStatById(String id) {
if (flag) {
return driver.findElement(By.id(id)).isDisplayed();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並獲取元素的顯示狀態
*
* @param xpath
* @return
*/
public boolean getDisplayStatByXpath(String xpath) {
if (flag) {
return driver.findElement(By.xpath(xpath)).isDisplayed();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並獲取元素的顯示狀態
*
* @param css
* @return
*/
public boolean getDisplayStatByCss(String css) {
if (flag) {
return driver.findElement(By.cssSelector(css)).isDisplayed();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據id定位元素並獲取元素的可寫狀態
*
* @param id
* @return
*/
public boolean getEnableStatById(String id) {
if (flag) {
return driver.findElement(By.id(id)).isEnabled();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並獲取元素的可寫狀態
*
* @param xpath
* @return
*/
public boolean getEnableStatByXpath(String xpath) {
if (flag) {
return driver.findElement(By.xpath(xpath)).isEnabled();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並獲取元素的可寫狀態
*
* @param css
* @return
*/
public boolean getEnableStatByCss(String css) {
if (flag) {
return driver.findElement(By.cssSelector(css)).isEnabled();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據id定位元素並獲取元素的選中狀態
*
* @param id
* @return
*/
public boolean getSelectStatById(String id) {
if (flag) {
return driver.findElement(By.id(id)).isSelected();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據xpath定位元素並獲取元素的選中狀態
*
* @param xpath
* @return
*/
public boolean getSelectStatByXpath(String xpath) {
if (flag) {
return driver.findElement(By.xpath(xpath)).isSelected();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 根據css定位元素並獲取元素的選中狀態
*
* @param css
* @return
*/
public boolean getSelectStatByCss(String css) {
if (flag) {
return driver.findElement(By.cssSelector(css)).isSelected();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}
/**
* 獲取當前焦點所在頁面元素的屬性值(name,value,id,src等等)
*
* @param attribute
* @return
*/
public String getFocusAttributeValue(String attribute) {
try {
Thread.sleep(333);
} catch (Exception e) {
e.printStackTrace();
}
value = driver.switchTo().activeElement().getAttribute(attribute);
System.out.println(“The focus Element’s ” + attribute + “attribute value is>>” + value);
return value;
}
/**
* 打開網頁鏈接
*
* @param pageUrl
*/
public boolean openPage(String pageUrl) {
try {
driver.get(pageUrl);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 進入測試,打開浏覽器,輸入網址,打開網頁
*
* @param remoteUrl
* 遠程服務器地址
* @param pageUrl
* 測試頁面地址
*/
public boolean startTest(String remoteUrl, String pageUrl) {
try {
try {
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.get(pageUrl);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
/**
* 進入測試,打開浏覽器,輸入網址,打開網頁
*
* @param explore
* 調用的浏覽器,需要啟動不同的server,如:firefox,需要運行selenium-server-standalone-
* 2.33.0.jar。IE,則需運行IEDriverServer.exe
*
* @param remoteUrl
* 遠程服務器地址
* @param pageUrl
* 測試頁面地址
*/
public boolean startTest(String explore, String remoteUrl, String pageUrl) {
try {
try {
if (“f”.equals(explore)) {
System.out.println(“firefox”);
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
} else if (“ie”.equals(explore)) {
System.out.println(“internet explorer”);
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver(new URL(remoteUrl), cap);
} else {
System.out.println(“firefox”);
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
}
} catch (Exception e) {
e.printStackTrace();
}
driver.get(pageUrl);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 設置定位頁面元素的超時時間
*
* @param second
* @return
*/
public boolean setTimeOut(int second) {
try {
driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 結束測試,關閉浏覽器
*/
public boolean endTest() {
try {
driver.quit();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 休息間隔,單位毫秒
*
* @param i
* @return
*/
public boolean sleep(int i) {
try {
Thread.sleep(i);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 根據id等待對應的頁面元素出現
*
* @param id
* @return
*/
public boolean waitForElementById(String id) {
try {
driver.findElement(By.id(id));
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 根據css等待對應的頁面元素出現
*
* @param css
* @return
*/
public boolean waitForElementByCss(String css) {
try {
driver.findElement(By.cssSelector(css));
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* 根據xpath等待對應的頁面元素出現
*
* @param xpath
* @return
*/
public boolean waitForElementByXpath(String xpath) {
try {
driver.findElement(By.xpath(xpath));
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
}
轉載請注明:《封裝常用的selenium方法--http://www.testinn.pub/testinn/55》