首页 文章

在RobotFramework中扩展selenium2library WebElement

提问于
浏览
0

我是RobotFramework的新手(实际上是为一个新的测试自动化项目进行评估) .

在过去,我总是使用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
   //.........................
   //.........................

机器人框架到目前为止看起来很好,我也喜欢python ...但是我不知道我是否能够扩展像selenium2library这样的库来拥有我自己的自定义方法,就像我在上面的例子中用java做的那样 .

2 回答

  • 3

    你可以,但我不确定机器人框架提供什么 Value . 有关如何子类化库和/或访问实际webdriver对象的示例,请参阅此问题的答案:Pass existing Webdriver object to custom Python library for Robot Framework

    但是,使用机器人的全部意义在于您不直接调用webdriver函数 . 您当然可以覆盖这些方法(在python中称为“monkeypatching”),关键字将使用您的修补函数,但这不是必需的 . 使用robot时,不应直接与Web元素进行交互 .

    使用机器人框架,您应该编写更抽象的测试 . 例如:

    *** Test Cases ***
    | Login with valid credentials
    | | Go to page | LoginPage
    | | Enter the username | valid_user@example.com
    | | 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实现页面对象模式 . 有关该库的更多信息,请参阅此博文:Robot Framework Page objects

  • 0

    可以在PythonJava中编写扩展名 . 但是,我认为使用已经_2558800并通过使用existig功能制作自定义关键字来创建测试逻辑是我们大多数人创建测试用例的方式 .

相关问题