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

查看:527
本文介绍了将现有的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?

推荐答案

库中没有内置任何内容可以让您本身进行操作.但是,您可以创建自己的可以访问硒功能的库.有两种方法可以做到这一点,这两种方法都需要在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.

作为示例,这是一个自定义的硒库,它具有一个新关键字,该关键字将返回当前的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获取活动库实例《机器人框架用户指南》 中.

例如,上例中的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天全站免登陆