- 论坛徽章:
- 0
|
本帖最后由 haiyang_it 于 2014-02-26 18:30 编辑
我想在自己的代码中使用Selenium2Library库中的类,例如打开浏览器:
代码如下:
from Tkinter import *
import Selenium2Library
class Linghy_Library:
def Utility_Open_Window(self,url):
browser = Selenium2Library._BrowserManagementKeywords()
browser.open_browser(url,"ie")
但是这个报错,看标红部分:
File "<string>", line 2, in open_browser
File "C:\Python27\lib\site-packages\robotframework_selenium2library-1.4.0-py2.7.egg\Selenium2Library\keywords\keywordgroup.py", line 12, in _run_on_failure_decorator
return method(*args, **kwargs)
File "C:\Python27\lib\site-packages\robotframework_selenium2library-1.4.0-py2.7.egg\Selenium2Library\keywords\_browsermanagement.py", line 109, in open_browser
self._info("Opening browser '%s' to base url '%s'" % (browser, url))
AttributeError: '_BrowserManagementKeywords' object has no attribute '_info'
_BrowserManagementKeywords所在的源码如下:
import os
import robot
from robot.errors import DataError
from selenium import webdriver
from Selenium2Library import webdrivermonkeypatches
from Selenium2Library.utils import BrowserCache
from Selenium2Library.locators import WindowManager
from keywordgroup import KeywordGroup
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
FIREFOX_PROFILE_DIR = os.path.join(ROOT_DIR, 'resources', 'firefoxprofile')
BROWSER_NAMES = {'ff': "_make_ff",
'firefox': "_make_ff",
'ie': "_make_ie",
'internetexplorer': "_make_ie",
'googlechrome': "_make_chrome",
'gc': "_make_chrome",
'chrome': "_make_chrome",
'opera' : "_make_opera",
'phantomjs' : "_make_phantomjs",
'htmlunit' : "_make_htmlunit",
'htmlunitwithjs' : "_make_htmlunitwithjs",
'android': "_make_android",
'iphone': "_make_iphone"
}
class _BrowserManagementKeywords(KeywordGroup):
def __init__(self):
self._cache = BrowserCache()
self._window_manager = WindowManager()
self._speed_in_secs = float(0)
self._timeout_in_secs = float(5)
self._implicit_wait_in_secs = float(0)
def open_browser(self, url, browser='firefox', alias=None,remote_url=False,
desired_capabilities=None,ff_profile_dir=None):
if remote_url:
self._info("Opening browser '%s' to base url '%s' through remote server at '%s'"
% (browser, url, remote_url))
else:
self._info("Opening browser '%s' to base url '%s'" % (browser, url))
browser_name = browser
browser = self._make_browser(browser_name,desired_capabilities,ff_profile_dir,remote_url)
browser.get(url)
self._debug('Opened browser with session id %s'
% browser.session_id)
return self._cache.register(browser, alias)
类KeywordGroup源码如下:
import sys
import inspect
try:
from decorator import decorator
except SyntaxError: # decorator module requires Python/Jython 2.4+
decorator = None
if sys.platform == 'cli':
decorator = None # decorator module doesn't work with IronPython 2.6
def _run_on_failure_decorator(method, *args, **kwargs):
try:
return method(*args, **kwargs)
except Exception, err:
self = args[0]
if hasattr(self, '_run_on_failure'):
self._run_on_failure()
raise
class KeywordGroupMetaClass(type):
def __new__(cls, clsname, bases, dict):
if decorator:
for name, method in dict.items():
if not name.startswith('_') and inspect.isroutine(method):
dict[name] = decorator(_run_on_failure_decorator, method)
return type.__new__(cls, clsname, bases, dict)
class KeywordGroup(object):
__metaclass__ = KeywordGroupMetaClass
看源代码中貌似是没有_info属性的。
但是为什么在RIDE中运行openbrowser的时候不会报这种错呢?
是否是KeywordGroupMetaClass类中使用__new__动态给类添加了_info属性呢?
新手求教!
|
|