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功能构建自定义关键字来创建测试逻辑是我们大多数人创建测试用例的方式。