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(); } } }
相关文章推荐
- Mootools 1.2教程 函数
- autoit3最新汉化版下载
- AutoIt3和按键精灵的功能对比第1/2页
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记