将现有的 Webdriver 对象传递给 Robot Framework 的自定义 Python 库 [英] Pass existing Webdriver object to custom Python library for Robot Framework

查看:35
本文介绍了将现有的 Webdriver 对象传递给 Robot Framework 的自定义 Python 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Robot Framework 创建一个自定义 Python 库,但我是 Python 和 Robot 的新手,我不确定如何完成我想要做的事情.我想将 Robot 使用 Selenium2Library 创建的 Webdriver 对象传递给我的自定义 Python 库,以便我可以使用 Webdriver 的方法,例如 find_element_by_id.我在这里此处,但它们用于 Java 库 - 我不能查找任何 Python 指令.

I am trying to create a custom Python library for Robot Framework, but I'm new to Python and Robot and I'm not sure how to accomplish what I'm trying to do. I want to pass the Webdriver object that Robot creates using Selenium2Library to my custom Python library so that I could use the Webdriver's methods, such as find_element_by_id. I've seen some suggestions about how to do it here and here, but they're for Java libraries - I can't find any Python instructions.

我将如何在 Python 中执行此操作?或者我想以不同的方式执行此操作,而不传递 Webdriver 对象?

How would I go about doing this in Python? Or would I want to do this differently, without passing the Webdriver object?

推荐答案

库中没有任何内容可以让您做自己想做的事情本身.但是,您可以创建自己的可以访问 selenium 功能的库.有两种方法可以实现这一点,都需要在 python 中创建自己的库.这些方法是对 Selenium2Library 进行子类化,或获取对 Selenium2Library 实例的引用.

There's nothing built into the library to let you do what you want per se. However, you can create your own library that can access selenium features. There are two ways to accomplish this, both which require creating your own library in python. These methods are to to subclass Selenium2Library, or to get a reference to the Selenium2Library instance.

访问 Selenium2Library 内部的一种方法是编写一个继承自 Selenium2Library 的库类.当您这样做时,您可以访问原始库中的所有内容.然后,您可以返回对 WebDriver 对象的引用,或者您可以在 python 中编写自己的关键字.

One way to access the internals of Selenium2Library is to write a library class that inherits from Selenium2Library. When you do that, you have access to everything in the original library. You can then return a reference to the WebDriver object, or you can just write your own keywords in python.

举个例子,这里有一个自定义的 selenium 库,它有一个 new 关键字,可以返回当前的 WebDriver 实例.它通过调用私有(对原始 Selenium2Library)方法 _current_browser 来实现.由于这是一个私有方法,不能保证它会经得起时间的考验,但在我写这篇文章的时候它就存在了.

As an example, here's a custom selenium library that has a new keyword that will return the current WebDriver instance. It does this by calling the private (to the original Selenium2Library) method _current_browser. Since that's a private method, there's no guarantee it will stand the test of time, but at the time that I write this it exists.

首先,创建一个名为 CustomSeleniumLibrary.py 的新 python 文件.把它放在机器人可以找到的地方——最简单的方法就是把它和要使用它的测试套件放在同一个文件夹中.将以下内容放入该文件:

First, create a new python file named CustomSeleniumLibrary.py. Put it where robot can find it -- the easiest thing is just put it in the same folder as a test suite that is going to use it. Put the following into that file:

from Selenium2Library import Selenium2Library

# create new class that inherits from Selenium2Library
class CustomSeleniumLibrary(Selenium2Library):
    # create a new keyword called "get webdriver instance"
    def get_webdriver_instance(self):
        return self._current_browser()

创建一个使用库的测试用例

接下来,编写一个使用它而不是 Selenium2Library 的测试用例.例如:

Create a testcase that uses the library

Next, write a test case that uses this instead of Selenium2Library. For example:

*** Settings ***
| Library | CustomSeleniumLibrary.py
| Suite Teardown | close all browsers

*** Test Cases ***
| Example using custom selenium library
| | Open browser | http://www.example.com | browser=chrome
| | ${webdriver}= | Get webdriver instance
| | log | webdriver: ${webdriver}

运行测试

像运行任何其他测试一样运行测试.完成后,您应该会在日志中看到如下内容:

Run the test

Run the test as you would any other test. When it completes you should see something like this in the log:

16:00:46.887 INFO webdriver: <selenium.webdriver.chrome.webdriver.WebDriver object at 0x10b849410>

在测试用例中使用对象

神秘的 ...<selenium....WebDriver object...> 消息证明该变量实际上持有对 python WebDriver 对象的引用.使用机器人的扩展变量语法,您可以调用方法和如果需要,可以访问该对象的属性.我不建议这样做,但我认为机器人支持它真的很有趣:

Using the object in a testcase

The cryptic ...<selenium....WebDriver object...> message proves that the variable actually holds a reference to the python WebDriver object. Using the extended variable syntax of robot you could then call methods and access attributes on that object if you want. I do not recommend doing it in this way, but I think it's really interesting that robot supports it:

| | log | The page title is ${webdriver.title}

创建一个引用 Selenium2Library 的自定义库

实现此目的的第二种方法是使用机器人获取库实例的方法,此时您可以随意访问该对象.这在机器人用户指南中有记录;参见 从 Robot Framework 获取活动库实例Robot Framework 用户指南.

例如,上例中的 get_library_instance 关键字如下所示:

For example, the get_library_instance keyword from the above example would look like this:

from robot.libraries.BuiltIn import BuiltIn

def get_webdriver_instance():
    se2lib = BuiltIn().get_library_instance('Selenium2Library')
    return se2lib._current_browser()

请注意,在这种情况下,您必须同时包含 Selenium2Library 您的自定义库:

Note that in this case you must include both the Selenium2Library and your custom library:

*** Settings ***
| Library | Selenium2Library
| Library | CustomSeleniumKeywords.py
| Suite Teardown | close all browsers

*** Test Cases ***
| Example using custom selenium keyword
| | Open browser | http://www.example.com | browser=chrome
| | ${webdriver}= | Get webdriver instance
| | log | webdriver: ${webdriver}

这篇关于将现有的 Webdriver 对象传递给 Robot Framework 的自定义 Python 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆