您的位置:首页 > 其它

Selenium常用函数封装

2016-04-15 21:33 459 查看
package com.wiley.enterprise;

import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseTest {

protected WebDriver driver;

@BeforeMethod
public void initCase(){
System.setProperty("webdriver.chrome.driver", "E:\\webdriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://112.5.161.79:91/login.html");
this.login("182xxx2116", "xxxxxx");
}

/**
* login with mobile and pwd
* @param mobile
* @param pwd
*/
public void login(String mobile, String pwd){
this.sendKeys(By.name("mobile"), mobile);
this.sendKeys(By.name("password"), pwd);
this.clickEle(By.xpath("//button[@type='submit']"));
}

@AfterMethod
public void quit(){
if(driver != null){
driver.quit();
}
}

/**
* call autoIt3 to upload the file
* @throws InterruptedException
* @throws FindFailed
*/
public void uploadFile(By loc, String path) throws InterruptedException, FindFailed{
//等待弹出框
Thread.sleep(2000);
//sikuli模式
Screen screen = new Screen();
Pattern camera = new Pattern(path);
screen.click(camera);
try {
Runtime.getRuntime().exec("E:\\test\\upload.exe");
} catch (IOException e) {
System.err.println("调用autoit3失败!");
}
Thread.sleep(2000);
WebElement sureButton = null;
try{
sureButton = this.findEle(loc);
Thread.sleep(5000);
sureButton.click();
}catch(Exception e){
System.err.println("定位不到该元素!");
}
}

/**
* select by value
* @param loc
* @param value
*/
public void selectByValue(String loc, String value){
Select type = new Select(this.findEle(By.name(loc)));
type.selectByValue(value);
}

/**
* roll down and keep the element to the center of browser
* @param loc
*/
public void scrollToElement(By loc){
WebElement ele = this.findEle(loc);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView()", ele);
}

//封装智能等待方法(设置超时时间:30s)
private boolean waitToDisplayed(final By loc) {
boolean waitDisplayed=false;
waitDisplayed = new WebDriverWait(driver, 30)
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(loc).isDisplayed();
}
});
return waitDisplayed;
}

//定位单个元素方法
public WebElement findEle(By loc){
WebElement ele = null;
if(this.waitToDisplayed(loc)){
ele = driver.findElement(loc);
}
return ele;
}

//定位一组元素方法
public List<WebElement> findEles(By loc){
List<WebElement> eles = null;
if (this.waitToDisplayed(loc)) {
eles = driver.findElements(loc);
}
return eles;
}

//判断元素是否出现
public boolean isElementPresent(By loc){
boolean isDisplayed = false;
try{
isDisplayed = this.findEle(loc).isDisplayed();
}catch(Exception e){
isDisplayed = false;
}
return isDisplayed;
}

//点击一个元素
public void clickEle(By loc){
WebElement ele = this.findEle(loc);
ele.click();
}

//输入方法
public void sendKeys(By loc,String value){
try{
this.findEle(loc).clear();
this.findEle(loc).click();
this.findEle(loc).sendKeys(value);
}catch(NoSuchElementException e){
System.err.println("元素"+loc.toString()+"找不到");
}
}

// 获取text
public void getContent(By loc){
System.out.println("弹窗提示消息:"+ this.findEle(loc).getText());
}

// alert弹窗
public void alert(String loc){
Alert temp=driver.switchTo().alert();
System.out.println(temp.getText());
if(loc == "accept"){
temp.accept();
}else{
temp.dismiss();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息