Selenium 封装

Selenium 封装

WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐:

  1. WebElement element =driver.findElement(By.name("q"));
  2. element.sendKeys("Cheese!");

我们可以考虑对这些基本的操作进行一个封装,简化操作。比如,封装代码:

  1. protected void sendKeys(By by, String value){
  2. driver.findElement(by).sendKeys(value);
  3. }

那么,在测试用例可以这样调用:

sendKeys(By.name("q"),”Cheese!”);

类似的封装还有:

  1. import java.util.List;
  2. import java.util.NoSuchElementException;
  3. import java.util.concurrent.TimeUnit;
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.remote.RemoteWebDriver;
  7. import org.openqa.selenium.support.ui.WebDriverWait;
  8. public class DriverAction {
  9. protected RemoteWebDriver driver;
  10. protected WebDriverWait driverWait;
  11. private int WAIT_ELEMENT_TO_LOAD=10;
  12. protected boolean isWebElementExist(By selector) {
  13. try {
  14. driver.findElement(selector);
  15. return true;
  16. } catch(NoSuchElementException e) {
  17. return false;
  18. }
  19. }
  20. protected String getWebText(By by) {
  21. try {
  22. return driver.findElement(by).getText();
  23. } catch (NoSuchElementException e) {
  24. return "Text not existed!";
  25. }
  26. }
  27. protected void clickElementContainingText(By by, String text){
  28. List<WebElement>elementList = driver.findElements(by);
  29. for(WebElement e:elementList){
  30. if(e.getText().contains(text)){
  31. e.click();
  32. break;
  33. }
  34. }
  35. }
  36. protected String getLinkUrlContainingText(By by, String text){
  37. List<WebElement>subscribeButton = driver.findElements(by);
  38. String url = null;
  39. for(WebElement e:subscribeButton){
  40. if(e.getText().contains(text)){
  41. url =e.getAttribute("href");
  42. break;
  43. }
  44. }
  45. return url;
  46. }
  47. protected void click(By by){
  48. driver.findElement(by).click();
  49. driver.manage().timeouts().implicitlyWait(WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
  50. }
  51. protected String getLinkUrl(By by){
  52. return driver.findElement(by).getAttribute("href");
  53. }
  54. protected void sendKeys(By by, String value){
  55. driver.findElement(by).sendKeys(value);
  56. }
  57. }

最新文章

  1. java对象与XML相互转化
  2. Android开发:第四日番外——Assets文件夹和RAW文件夹区别
  3. Android UI设计系统-android selector 开始自定义样式
  4. Windows 8 应用开发 - 异步调用
  5. 织梦不仅是链接到其他调用next
  6. C#拾遗(二、函数)
  7. 微信自定义菜单url默认80端口问题解决
  8. SQL语句执行性能
  9. UTF8 &amp; GBK之间的转换
  10. [py]python的继承体系
  11. jsapi微信扫一扫
  12. Zabbix监控介绍及安装配置
  13. Sybase数据库应用系统调优的五大领域
  14. Apache rewrite 出现 400 Bad Request 的解决方法
  15. Djano + Nginx + docker配置与管理
  16. linux下安装使用虚拟环境
  17. 【转发】Build Squid with SSL Bump and ICAP Client
  18. v4l2驱动文档之——streaming IO【转】
  19. &lt;&lt;= 什么意思?|=什么意思?
  20. ios界面笔记(二)

热门文章

  1. Java 执行linux scp 远程获取文件和上传
  2. 怎么用ChemDraw加反应条件
  3. php7 宏杂记
  4. nib文件的默认搜索规则
  5. Android音视频学习第7章:使用OpenSL ES进行音频解码
  6. 系统管理模块_用户管理1_实现用户有关的功能_测试功能、解决事务的问题、对密码进行MD5摘要
  7. ORA-00972: 标识符过长
  8. 【BZOJ3425】Poi2013 Polarization 猜结论+DP
  9. 学习ASP.NET MVC3(6)----- Filte
  10. 03.Curator深入使用