selenium常用的方法_第1页
selenium常用的方法_第2页
selenium常用的方法_第3页
selenium常用的方法_第4页
selenium常用的方法_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、package avaya.browser.method;import java.io.IOException;import java.util.*;import org.junit.Before;import org.openqa.*;import com.thoughtworks.selenium.Wait;import erfac.Driver;/attention: 以Br.开头的方法,是因为Br是自己在本类还封装了一层,需要仔细看代码public class Br extends Baseclass private String locator;pu

2、blic static String log(String info)System.out.println(info);return info;public static void openbrowser(String url)/driver=new FirefoxDriver();/Navigation navitation=driver.navigate();/navitation.to(url);driver.get(url);Br.log(open browser and input testURL:+url);/opration the windows maxpublic stati

3、c void maxwindow()driver.manage().window().maximize();Br.log(max windows);/refresh the windowspublic static void refresh()driver.navigate().refresh();Br.log(refresh the windows); public static void login(String url,String user,String pwd) throws InterruptedException driver.get(url); Br.clear(By.id(B

4、r.logrm:username); Br.sendkeys(By.id(Br.logrm:username),user); Br.clear(By.id(Br.logrm:password); Br.sendkeys(By.id(Br.logrm:password),pwd); Br.getWebText(By.id(Br.logrm:submitText); driver.findElement(By.id(Br.logrm:submitText).click(); try Thread.sleep(5000); catch (InterruptedException e) / TODO

5、Auto-generated catch blocke.printStackTrace(); Op.refresh();Br.log(,input:+url);Br.log(登陆账号为:+user+ +登陆密码为:+pwd); /if form parameter is not null,input data and use this method;public static void closebrowser(String BrowserType)if(BrowserType!=null) if(BrowserType.equals(c) try Runtime.getRuntime().e

6、xec(taskkill /F /IM chrome.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); else if(BrowserType.equals(i) try Runtime.getRuntime().exec(taskkill /F /IM iexplorer.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); else if(BrowserType.equ

7、als(o) try Runtime.getRuntime().exec(taskkill /F /IM opera.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); else if(BrowserType.equals(s) try Runtime.getRuntime().exec(taskkill /F /IM safari.exe); catch (IOException e) / TODO Auto-generated catch block e.printStackTr

8、ace(); else try Runtime.getRuntime().exec(taskkill /F /IM firefox.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); /if form parameter is null,use this method;public static void closebrowser()try Runtime.getRuntime().exec(taskkill /F /IM firefox.exe); catch (IOExcepti

9、on e) / TODO Auto-generated catch blocke.printStackTrace(); public static void switchToIframe(By by)driver.switchTo().frame(driver.findElement(by);/driver.switchTo().frame(driver.findElement(By.xpath(/html/body/div4/div4/div/div/iframe); /find element and set the valuepublic static void sendkeys(By

10、by,String str) throws InterruptedExceptionString text;/text=driver.findElement(by).getText();driver.findElement(by).clear();Thread.sleep(1500);driver.findElement(by).sendKeys(str);/Br.log(execute select element object+text);Br.log(Set: +str);public static void click(By by) throws InterruptedExceptio

11、nString text;if(driver.findElement(by).getText()!=null & ! .equals(driver.findElement(by).getText()text=driver.findElement(by).getText();Thread.sleep(3000);System.out.println(execute click operation);System.out.println(click+ +text);elseSystem.out.println(this Element is not found text);driver.findE

12、lement(by).click();/* doubleclick * by */public static void doubleClick(By by)new Actions(driver).doubleClick(driver.findElement(by).doubleClick();public static void clear(By by)driver.findElement(by).clear();Br.log(Empty element content,by: +by);/*select element by index * webdriver is nonsupport s

13、elect function,so suggest not */ public static void selectByIndex(By by,int index)/driver.findElement(by).click();String text;Select select=new Select(driver.findElement(by);try Thread.sleep(10); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();select.selectByIndex

14、(index);Br.log(Perform a drop-down box operations,operation element:+by);text=getWebText(by);Br.log(select+index+element+element info is:+text);/*select element by value * webdriver is nonsupport select function,so suggest not */public static void selectByText(By by,String text)driver.findElement(by

15、).click();Select select=new Select(driver.findElement(by);select.selectByVisibleText(text);/select element by value/public static void selectBy(By by,String value)/driver.findElement(by).click();/Select select=new Select(driver.findElement(by);/select.selectByValue(value);/public static void select(

16、By by0,By by1)/driver.findElement(by0).click();/driver.findElement(by1).click();/Select select=new Select(driver.findElement(by);/select.selectByValue(value);/*select element by linktext * webdriver is nonsupport select function,so suggest not */public static void selectByLinkText(By by,String LinkT

17、ext)/driver.findElement(by).click();Select select=new Select(driver.findElement(by);select.selectByValue(LinkText);Br.log(execute select element ,the object:+LinkText); / 获取链接元素的url值 public static String getLinkUrl(By by) return driver.findElement(by).getAttribute(href); / 判断某个元素是否存在 public static b

18、oolean isElementExist(By by) try Boolean bool = driver.findElement(by).isDisplayed(); Br.log(execute+by+exist or not+,+result is :+bool); return bool; catch(NoSuchElementException e) return false; / 获得某元素的文本描述信息 public static String getWebText(By by) String text; text=driver.findElement(by).getText(

19、);try Br.log(get Element text +:+text);return text;/return driver.findElement(by).getText();catch (NoSuchElementException e)return not found the Element text!; /* * 在一个元素集合中通过遍历文本内容定位到相应的元素 */ public static void clickElementContainingText(By by,String text) List elementList = driver.findElements(by)

20、; for(WebElement e:elementList) if(e.getText().contains(text) e.click(); break; /* 根据id定位元素并获取元素的选中状态 * param id * return */ public static boolean getselectstat(By by) boolean flag=true; if(flag) Br.log(页面元素状态:+by+已被选中); return driver.findElement(by).isSelected(); else Br.log(页面元素状态:+by+未被选中); retur

21、n false; /* 等待时间 * param second * return */ public static boolean wait(int second) trydriver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS); /Br.log(执行了等待操作,等待了+second+秒); return true; catch(Exception e) return false; / public static void waitForpageToload(int i)/ boolean isLoaded=fals

22、e; / int count=0; / do / if(count+3) / break; / / try / selenium.waitForPageToLoad(i); / isLoaded=true; / catch(Exception ex) / continue; / / while(!isLoaded); / / / /public boolean switchToWindow(WebDriver driver,String windowTitle) / 通过弹出框的title进行切换,非xpath等方法,此方法比较方便 public static boolean switchto

23、bytitle(String windowTitle) boolean flag = false; try String currentHandle = driver.getWindowHandle(); Set handles = driver.getWindowHandles(); for (String s : handles) if (s.equals(currentHandle) /System.out.println(test here); continue; else driver.switchTo().window(s); if (driver.getTitle().contains(windowTitle) flag = true; System.out.println(切换窗口到:+ windowTitle); break; else continue; catch (NoSuchWindowException e) System.out.printf(Window: + windowTitle+ cound not found!, e.fillInStackTrace(); flag = false; return flag; /* 通过windows的id号进行切换 * i 窗口的索引从0开始 * by driver的查找元素

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论