zoukankan      html  css  js  c++  java
  • Selenium 方法封装 一

     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. }  
  • 相关阅读:
    敏捷 咨询师 火星人陈勇博客牛人
    fpa 功能点分析法
    cto职责
    lexus
    c
    zz
    百度效率云
    zz
    产品研发体系中的需求承接与输出
    ESAPI学习笔记
  • 原文地址:https://www.cnblogs.com/111testing/p/6986918.html
Copyright © 2011-2022 走看看