【转】Selenium的封装与重用

上一篇 / 下一篇  2013-11-26 10:52:10 / 个人分类:自动化测试

Selenium封装重用

上一篇 / 下一篇  2012-12-28 11:10:22 / 个人分类:Selenium

    用过一段webdriver之后就会知道经常用到的只有几个常用的selenium API方法,大家自己可以对selenium的API进行二次封装之后每次可以直接调用封装后自己写的方法来间接调用selenium的API。
   webdriver接口下的一些方法的封装
    查看Selenium的官方API可知WebDriver接口在org.openqa.selenium这个包下面
    而WebDriver接口下常用的方法有get()、close()、findElement()、findElements()、getText()、getAttribute、switchTo()。
   1)对get()、close()方法的封装:
   WebDriver driver = new FirefoxDriver();
        // 通过url打开相应的页面
public void openBrowser(String url)
{
driver.get(url);
}

// 关闭相应的页面
public void closeBrowser()
{
driver.close();
    
   2)对findElement()、getText()、switchTo()封装

        // 查找到某一元素并且给予相应的值
        // 一般用在对文本输入框元素的定位和输入相应值
      public void sendKey(By by,String value)
{
driver.findElement(by).sendKeys(value);
}
        

        // 查找到某元素并点击
        public void elementClick(By by) 
        {
       driver.findElement(by).click();
        }

         /*
         ***  先定位到相应的选择框
         ***  再定位到详细的待选项
         ***  一般用在定位下拉选择框中的某一个元素
         */
        public void selectElement(By by,String text)
{
Select select = new             Select(driver.findElement(by));
select.selectByVisibleText(text);
}
        
        // 获取链接元素的url值
        public String getLinkUrl(By by)
        {
       return driver.findElement(by).getAttribute("href");
        }

        // 判断某个元素是否存在
public boolean isElementExist(By by)
{
try
{
Boolean bool = driver.findElement(by).isDisplayed();
return bool;
}
catch(NoSuchElementException e)
{
return false;
}
}
       
        // 获得某元素的文本描述信息
        public String getWebText(By by)
{
try {
return driver.findElement(by).getText();
}
catch (NoSuchElementException e){
return "Text does not exist!";
}
}
        
   3)findElements()相关方法的封装
        /*
        *** 在一个元素集合中通过遍历文本内容定位到相应的元素
        */

        public void clickElementContainingText(By by,String text)
        {
       List<WebElement> elementList = driver.findElements(by);
   
       for(WebElement e:elementList)
       {
    if(e.getText().contains(text))
                {
       e.click();
               break;
   }
       }
         }
    
        /*
        *** 在一个元素集合中通过遍历文本内容获取相应的url
        */
        public String getLinkUrlContainingText(By by, String text)
        {
       List<WebElement> subscribeButton = driver.findElements(by);
       String url = null;
   
       for (WebElement e:subscribeButton) {
   if (e.getText().contains(text)) 
                {
   url = e.getAttribute("href");
   break;
   }
   }
   return url;
       }
   4)frame相关的方法封装
       /*
        *** 某些元素由于属于某个iframe的需要先定义到frame然后再在此frame里边查找该元素
        *** 此方法适合定位文本链接型元素
        */
     public void frameElementClick(By by,String text)
{
  WebElement element = driver.switchTo().frame(text).findElement(by);
  element.click();
}

       /*
        *** 某些元素由于属于某个iframe的需要先定义到frame然后再在此frame里边查找该元素
        *** 此方法适合定位文本输入框型元素
        */
public void frameElementSendKey(By by,String text)
{
  WebElement element = driver.switchTo().frame(text).findElement(by);
  element.sendKeys(text);
}  
        另外在不同frame之间切换时不要忘记使用以下方法:
       driver.switchTo().defaultContent(); 

注:大家可以把以上方法放到一个jar包里边 直接调用就可以了 

TAG: 重用 封装

 

评分:0

我来说两句

Open Toolbar