robotframework调用python类方法,将现有的Webdriver对象传递给用于Robot Framework的自定义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.

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

解决方案

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.

Creating a custom library that subclasses Selenium2Library

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.

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.

Create a custom selenium library

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()

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:

Using the object in a testcase

The cryptic ... 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}

Creating a custom library that references Selenium2Library

The second way to accomplish this is to use robot's method of getting an instance of a library, at which point you can access the object however you want. This is documented in the robot user guide; see Getting active library instance from Robot Framework in the Robot Framework User's Guide.

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()

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}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值