0

我是RobotFramework的新手(實際評估它是爲了一個新的測試自動化項目)。在RobotFramework中擴展Selenium2庫WebElement

在過去,我總是使用Java和頁面對象創建自己的小框架,但是這次我想知道我是否可以使用現有的框架。我正在評估Spock和Robot Framework。

我的要求是測試web應用程序,windows對象和移動應用程序,所以Robot絕對比Spock有優勢。另外,我更喜歡Python而不是Groovy。

我通常在我的框架中用以下代碼擴展WebElement。我想知道是否有可能在Robot Framework中做這樣的事情。

//importing webdriver and other selenium libraries 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebDriverException; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.interactions.internal.Coordinates; 
import org.openqa.selenium.internal.Locatable; 
import org.openqa.selenium.internal.WrapsElement; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.FluentWait; 
import org.openqa.selenium.support.ui.Wait; 

//interface that will be implemented by my custom web element 
interface MyElement extends WebElement, WrapsElement, Locatable { 

} 

//my custom web element class 
public class MyWebElement implements MyElement { 

    private WebElement element; 

    //private constructor for my web element class 
    private MyWebElement(WebElement element) { 
     this.element = element; 
    } 

    //public factory method for my web element class 
    public static MyWebElement getInstance(By by) { 
     WebElement element = MyWebDriver.getInstance().findElement(by); 
     MyWebElement myWebElement = new MyWebElement(element); 
     return myWebElement; 
    } 

    //overriding WebElement method click 
    @Override 
    public void click() { 
     waitUntilClickable(); 
     element.click(); 
    } 

    //adding method waitUntilClickable to my web element 
    public MyWebElement waitUntilClickable() { 
     return waitUntilClickable(MyWebDriver.getTimeoutElementInMilliseconds(), 
       MyWebDriver.getPollingElementInMilliseconds()); 
    } 

    //adding helper method to implement waitUntilClickable 
    public MyWebElement waitUntilClickable(long timeOutInMilliseconds, 
      long pollingIntervalInMilliseconds) { 
     Wait<WebDriver> wait = new FluentWait<WebDriver>(MyWebDriver.getInstance()) 
       .withTimeout(timeOutInMilliseconds, TimeUnit.MILLISECONDS) 
       .pollingEvery(pollingIntervalInMilliseconds, TimeUnit.MILLISECONDS); 
     wait.until(ExpectedConditions.elementToBeClickable(element)); 
     return this; 
    } 
    //other additional and overriding methods 
    //......................... 
    //......................... 

機器人框架看起來不錯,到目前爲止,我喜歡蟒蛇太..但是我不知道我是否能夠延長圖書館像selenium2library有自己的定製方法,就像我以前做的java在上面的例子中。

+0

您的實際目標是什麼?您的實際目標是重寫webelement方法,還是您的目標是讓您更容易地調用按您的方式行事的硒函數(例如:添加額外的日誌記錄,等待等)?換句話說,重寫click方法的目標,還是隻是達到目的的一種手段? –

回答

3

你可以,但我不確定在機器人框架方面提供了什麼價值。有關如何繼承庫和/或訪問實際webdriver對象的示例,請參閱此問題的答案:Pass existing Webdriver object to custom Python library for Robot Framework

但是,使用機器人的關鍵在於您不直接調用webdriver函數。你當然可以重寫方法(在python中稱爲「monkeypatching」),關鍵字將使用你的補丁函數,但這並不是必須的。當你使用機器人時,你不應該直接與網頁元素交互。

隨着機器人框架,你應該寫更抽象的測試。例如:

*** Test Cases *** 
| Login with valid credentials 
| | Go to page | LoginPage 
| | Enter the username | [email protected] 
| | Enter the password | letmein! 
| | Click the login button 
| | The current page should be | DashboardPage 

請注意測試如何根本不使用web元素。它使用高級關鍵字,消除了與Web元素直接交互的需求,添加等待等。測試編寫器與頁面的實際實現完全隔離,因此他們可以專注於測試本身的邏輯。

當然,機器人不提供這樣的關鍵字,因爲它是一個通用的框架。它提供了通用關鍵字(「轉到頁面」,「點擊按鈕」等),但這不是機器人框架的真正威力所在。真正的功能來自於定製頁面特定的庫。

在你的例子中,你顯示覆蓋了click方法,這樣你可以添加一個明確的等待。與機器人無關,只要有意義,就可以在關鍵字本身中建立等待。

例如,的「點擊登錄按鈕」可能看起來像這樣實現:

def click_login_button(self): 
    with self._wait_for_page_refresh(): 
     WebDriverWait(self.browser, 10).until(
      lambda *args: button_element.element_to_be_clickable((By.ID, locator.login_button)) 
     ) 
     self.browser.find_element_by_id(locator.login_button).click() 

在這個例子中,我們已經採取了等待被點擊的元素照顧,我們點擊在它上面,然後我們等待頁面在返回之前被刷新。請注意,在關鍵字本身中,即使在測試用例級別沒有任何可見的東西,您仍然可以使用python和selenium的全部功能。


注意:這些示例大致基於我編寫的實現頁面對象模式的庫。有關該庫的更多信息,請參閱此博客文章:Robot Framework Page objects

+0

非常感謝Bryan的詳細解釋。從您的示例代碼中,我們將等待登錄按鈕可點擊,然後點擊該按鈕。我想爲任何Web元素做同樣的事情,當我想點擊它時。此外,我想在WebElement級別添加更多功能,以便這些功能可用於所有Web元素。所以,你提供的鏈接提供了一些見解。稍後我會詳細介紹一下。很高興知道你用機器人編寫頁面對象模式庫,因爲我想在我的測試中實現分層頁面對象。我會嘗試一下。 –

0

PythonJava中寫入擴展名是可能的。不過,我認爲使用已經存在的available keywords並通過使用existig功能構建自定義關鍵字來創建測試邏輯是我們大多數人創建測試用例的方式。