Selenium之常用的方法的封装
作者:
网络转载 发布时间:
[ 2016/1/12 13:46:17 ] 推荐标签:
软件测试工具 功能测试
1 /**
2 * 判断对象是否存在
3 * @param key
4 * @param replace
5 * @return
6 */
7 public boolean isExist(String key,String[] replace){
8 By by=this.getBy(key, replace);
9 return driver.findElements(by).size()>0?true:false;
10 }
1 public void sleep(int sec){
2 try {
3 Thread.sleep(sec*1000);
4 } catch (InterruptedException e) {
5 e.printStackTrace();
6 }
7 }
1 public void click(){
2 log.info("click the element:"+key);
3 element.click();
4 }
5
6 public void sendKeys(String value){
7 log.info("type the value:"+value+" to element:"+key);
8 element.sendKeys(value);
9 }
10
11 public void selectKeys(String text){
12 log.info("select the text:"+text+" to element:"+key);
13 Select select=new Select(element);
14 select.selectByVisibleText(text);
15 }
1 public void moveToElement(){
2 log.info("move to element: "+key);
3 Actions action = new Actions(driver);
4 action.moveToElement(element).perform();
5 }
6
7 public void executeJS(String js){
8 log.info("execute the js: "+js+" at element: "+key);
9 ((JavascriptExecutor)driver).executeScript(js, element);
10 }
11
12 public void waitClickToLoad(){
13 log.info("click the element:"+key);
14 WebDriverWait waitLoad = new WebDriverWait(driver,Config.waitTime);
15 waitLoad.until(new ExpectedCondition<WebElement>(){
16 @Override
17 public WebElement apply(WebDriver d) {
18 return element;
19 }}).click();
20 }
21
22 public void switchToFrame(int index){
23 if(index==-1){
24 log.info("返回默认窗口");
25 driver.switchTo().defaultContent();
26 log.info("返回默认窗口成功");
27 }else{
28 log.info("切换到第一个frame");
29 driver.switchTo().frame(index);
30 }
31 }