51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1513|回复: 1
打印 上一主题 下一主题

【转帖】WebDriver拾级而上(10) – 封装与重用

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-7-14 11:31:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐:
CODE:
  1. <font size="4">// Find the text inputelement by its name
  2. WebElement element = driver.findElement(By.name("q"));

  3. // Enter something to search for
  4. element.sendKeys("Cheese!");</font>
复制代码

我们可以考虑对这些基本的操作进行一个封装,简化操作。比如,封装代码:
CODE:
  1. <font size="4">protected void sendKeys(By by, String value){
  2.    driver.findElement(by).sendKeys(value);
  3. }</font>
复制代码

那么,在测试用例可以这样简化调用:
CODE:
  1. <font size="4">sendKeys(By.name("q"),"Cheese!");</font>
复制代码

看,这就简洁多了。
类似的封装还有:
CODE:
  1. <font size="4">package com.drutt.mm.end2end.actions;
  2.   
  3. import java.util.List;
  4. import java.util.NoSuchElementException;
  5. import java.util.concurrent.TimeUnit;
  6.   
  7. import org.openqa.selenium.By;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.remote.RemoteWebDriver;
  10. import org.openqa.selenium.support.ui.WebDriverWait;
  11. import com.drutt.mm.end2end.data.TestConstant;
  12.   
  13. public class WebDriverAction {
  14.     //protected WebDriverdriver;
  15.     protected RemoteWebDriverdriver;
  16.     protected WebDriverWaitdriverWait;


  17.     protected boolean isWebElementExist(By selector) {
  18.         try {
  19.             driver.findElement(selector);
  20.             return true;     
  21.         } catch(NoSuchElementException e) {
  22.             return false;
  23.         }
  24.     }
  25.      
  26.     protected String getWebText(By by) {
  27.         try {
  28.             return driver.findElement(by).getText();
  29.         } catch (NoSuchElementException e) {
  30.             return "Textnot existed!";
  31.         }
  32.     }
  33.      
  34.     protected void clickElementContainingText(By by, String text){
  35.         List<WebElement>elementList = driver.findElements(by);
  36.         for(WebElement e:elementList){
  37.             if(e.getText().contains(text)){
  38.                 e.click();
  39.                 break;
  40.             }
  41.         }     
  42.     }
  43.      
  44.     protected String getLinkUrlContainingText(By by, String text){
  45.         List<WebElement>subscribeButton = driver.findElements(by);
  46.         String url = null;
  47.         for(WebElement e:subscribeButton){
  48.             if(e.getText().contains(text)){
  49.                 url =e.getAttribute("href");
  50.                 break;
  51.             }
  52.         }
  53.         return url;
  54.     }
  55.      
  56.     protected void click(By by){
  57.        driver.findElement(by).click();
  58.        driver.manage().timeouts().implicitlyWait(TestConstant.WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
  59.     }
  60.   
  61.     protected String getLinkUrl(By by){
  62.         return driver.findElement(by).getAttribute("href");
  63.     }
  64.      
  65.     protected void sendKeys(By by, String value){
  66.        driver.findElement(by).sendKeys(value);
  67.     }
  68. }</font>
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

该用户从未签到

2#
发表于 2017-7-14 15:10:24 | 只看该作者
决定日后跟随楼主了
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-5 02:13 , Processed in 0.061878 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表