本文介绍了使用 Selenium2library 在 python 中创建自定义关键字的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一名团队成员在 python 中创建了一个自定义关键字.该关键字使用 Selenium2Library 的关键字.这是位于C:\Python27\Lib\site-packages\Selenium2Library\keywords_browsermanagement.py"中的代码

One of my team member created a custom keyword in python. that keyword uses the Selenium2Library's keyword. Here is the code that is places in "C:\Python27\Lib\site-packages\Selenium2Library\keywords_browsermanagement.py"

# Public, open and close
def select_popup(self):
    BuiltIn().sleep(3)
    handles = self._current_browser().get_window_handles()
    self._info("Window Names: %s " % handles)
    self._info("Pop Up Window being selected: %s " % handles[-1])
    print "in function"
    if len(handles) >= 1:
        self._current_browser().switch_to_window(handles[-1])

现在,只要这个关键字 select_popup 在 _browsermanagement.py 中,一切都正常.我想将此关键字移动到一个单独的文件中,因为我正在修改属于 Selenium2Library 的文件,这不是一个好习惯.现在,当我把它放在 MyLib.py 中时,当我在 RIDE 中开始测试时它会给我错误.这是错误消息.

Now, everything works fine as long as this keyword select_popup is in _browsermanagement.py. I want to move this keyword in a separate file as I this is modifying the file which belong to Selenium2Library which is not good practice. Now when I put this in MyLib.py it gives me error when I start the test in RIDE. Here is the error message.

[ ERROR ] Error in file 'D:\Automation\My_Resource.robot': Importing test library 'D:\Automation\MyResources\my.py' failed: NameError: global name 'self' is not defined
Traceback (most recent call last):
  File "D:\Automation\MyResources\my.py", line 15, in <module>
    select_popup();
  File "D:\Automation\MyResources\my.py", line 8, in select_popup
    handles = self._current_browser().get_window_handles()

我认为它没有找到对 selenium2library 对象的引用.有人可以在这里帮助我将自定义 python 关键字隔离到不同的文件.

I think it is not finding reference to selenium2library's object. Can someone help me here isolating the custom python keyword to different file.

推荐答案

您应该创建自己的库并继承 Selenium2Library.像这样:

You should create your own library and inherit Selenium2Library. Something like this:

*** Settings ***
Library           MySelenium2Library
Suite Teardown    Close All Browsers

*** Test Cases ***
StackOverflow
    Open Browser    http://www.google.com/    Chrome

MySelenium2Library 可以与您的机器人脚本位于同一文件夹中,它看起来像这样:

MySelenium2Library can be in same folder as your robot script and it would look like this:

from Selenium2Library import Selenium2Library

class MySelenium2Library(Selenium2Library):
    def select_popup(self):
        BuiltIn().sleep(3)
        handles = self._current_browser().get_window_handles()
        self._info("Window Names: %s " % handles)
        self._info("Pop Up Window being selected: %s " % handles[-1])
        print "in function"
        if len(handles) >= 1:
            self._current_browser().switch_to_window(handles[-1])

8 月 31-18 日更新

Update Aug-31-18

SeleniumLibrary 的新版本似乎需要@keyword 装饰器:GitHub 中的示例

New versions of SeleniumLibrary seem to require @keyword decorator:Example in GitHub

这个库的新版本看起来像这样:

New version of this library would look like this:

from SeleniumLibrary import SeleniumLibrary
from SeleniumLibrary.base import keyword

class MySeleniumLibrary(SeleniumLibrary):
    @keyword
    def select_popup(self):
        BuiltIn().sleep(3)
        handles = self._current_browser().get_window_handles()
        self._info("Window Names: %s " % handles)
        self._info("Pop Up Window being selected: %s " % handles[-1])
        print "in function"
        if len(handles) >= 1:
            self._current_browser().switch_to_window(handles[-1])

这篇关于使用 Selenium2library 在 python 中创建自定义关键字的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 10:05