码迷,mamicode.com
首页 > 其他好文 > 详细

Selenium方法封装

时间:2017-06-12 00:36:13      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:current   tool   span   turn   scribe   null   send   targe   copyto   

 Selenium 封装

 Selenium 封装

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

 

[java] view plain copy
 
  1. WebElement element =driver.findElement(By.name("q"));  
  2. element.sendKeys("Cheese!");  

 

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

 

[java] view plain copy
 
  1. protected void sendKeys(By by, String value){  
  2.     driver.findElement(by).sendKeys(value);  
  3.  }  

 

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

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

类似的封装还有:

 

[java] view plain copy
 
  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.   
  9. public class DriverAction {  
  10.    protected RemoteWebDriver driver;  
  11.    protected WebDriverWait driverWait;  
  12.    private int WAIT_ELEMENT_TO_LOAD=10;  
  13.   
  14.     protected boolean isWebElementExist(By selector) {   
  15.        try {   
  16.            driver.findElement(selector);  
  17.            return true;   
  18.        } catch(NoSuchElementException e) {   
  19.            return false;   
  20.        }   
  21.     }  
  22.       
  23.     protected String getWebText(By by) {   
  24.        try {   
  25.        return driver.findElement(by).getText();  
  26.        } catch (NoSuchElementException e) {   
  27.            return "Text not existed!";   
  28.        }   
  29.     }  
  30.       
  31.     protected void clickElementContainingText(By by, String text){  
  32.        List<WebElement>elementList = driver.findElements(by);  
  33.        for(WebElement e:elementList){  
  34.            if(e.getText().contains(text)){  
  35.                e.click();  
  36.                break;  
  37.            }  
  38.        }       
  39.     }  
  40.       
  41.     protected String getLinkUrlContainingText(By by, String text){  
  42.        List<WebElement>subscribeButton = driver.findElements(by);  
  43.        String url = null;  
  44.        for(WebElement e:subscribeButton){  
  45.            if(e.getText().contains(text)){  
  46.                url =e.getAttribute("href");  
  47.                break;  
  48.            }  
  49.        }  
  50.        return url;  
  51.     }  
  52.   
  53.     protected void click(By by){  
  54.        driver.findElement(by).click();  
  55.        driver.manage().timeouts().implicitlyWait(WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);  
  56.     }  
  57.    
  58.     protected String getLinkUrl(By by){  
  59.        return driver.findElement(by).getAttribute("href");  
  60.     }  
  61.   
  62.     protected void sendKeys(By by, String value){  
  63.        driver.findElement(by).sendKeys(value);  
  64.     }  
  65. }  

Selenium方法封装

标签:current   tool   span   turn   scribe   null   send   targe   copyto   

原文地址:http://www.cnblogs.com/111testing/p/6986918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!