Robotframework 使用Selenium2库在python中创建自定义关键字时出现问题

Robotframework 使用Selenium2库在python中创建自定义关键字时出现问题,robotframework,Robotframework,我的一位团队成员用python创建了一个自定义关键字。该关键字使用Selenium2库的关键字。下面是放置在“C:\Python27\Lib\site packages\Selenium2Library\keywords\u browsermanagement.py”中的代码 现在,只要这个关键字select\u popup位于\u browsermanagement.py中,一切都可以正常工作。我想在一个单独的文件中移动这个关键字,因为这是在修改属于Selenium2Library的文件,这不

我的一位团队成员用python创建了一个自定义关键字。该关键字使用Selenium2库的关键字。下面是放置在“C:\Python27\Lib\site packages\Selenium2Library\keywords\u browsermanagement.py”中的代码

现在,只要这个关键字select\u popup位于\u browsermanagement.py中,一切都可以正常工作。我想在一个单独的文件中移动这个关键字,因为这是在修改属于Selenium2Library的文件,这不是一个好的做法。现在,当我把它放在MyLib.py中时,当我在RIDE中开始测试时,它会给我错误。这是错误消息

[ 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()
[ERROR]文件“D:\Automation\My\u Resource.robot”中的错误:导入测试库“D:\Automation\MyResources\My.py”失败:NameError:未定义全局名称“self”
回溯(最近一次呼叫最后一次):
文件“D:\Automation\MyResources\my.py”,第15行,在
选择_popup();
文件“D:\Automation\MyResources\my.py”,第8行,在选择弹出窗口中
handles=self.\u current\u browser().get\u window\u handles()

我认为它没有找到对selenium2library对象的引用。有人能帮我将自定义python关键字隔离到不同的文件中吗。

您应该创建自己的库并继承Selenium2Library。大概是这样的:

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

*** Test Cases ***
StackOverflow
    Open Browser    http://www.google.com/    Chrome
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])
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])
MySelenium2Library可以与robot脚本位于同一文件夹中,如下所示:

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

*** Test Cases ***
StackOverflow
    Open Browser    http://www.google.com/    Chrome
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])
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])
更新日期:8月31日至18日

SeleniumLibrary的新版本似乎需要@keyword decorator:

此库的新版本如下所示:

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

*** Test Cases ***
StackOverflow
    Open Browser    http://www.google.com/    Chrome
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])
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创建的相同对象来打开浏览器。我使用TestNg+Selenium,在这种情况下,我使用该方法传递WebDriver对象。既然用户没有创建任何对象,它在这里将如何工作。因为MySelenium2Library继承了Selenium2Library,它应该与Selenium2Library一样工作,只是它有您的新关键字。我按照您的建议实现了它,以覆盖按键,但它仍然获取旧关键字。它也不会从Selenium2库中获取新关键字导入Selenium2库类MySeleniumLibrary(Selenium2库):def press\u key(self,locator,key):if key.startswith('\\\\\')和len(key)>1:key=getattr(key,key[2:])elif key.startswith('\\\\\')和len key>1:key=self映射ascii\u key\u-key代码到(int(key[1:])element=self.find_element(locator)element.send_keys(key)def anotherKw(self,locator,key):打印“hello”如果您使用的是最新版本的SeleniumLibrary,则需要修饰关键字。看看我最新的答案。