赞
踩
一、基本思路
每次使用open browser浏览器时都将当前的会话session_id和 excutor_url 按存在本地,在每一次加载senlium2brary库时候就使用ReuseBrowser去保存最新加载id和url。
二 、selenium2library库文件新增如下:
2.1 在selenium2library目录下增加getb.py文件
getb.py文件内容如下:
- #-*- coding:utf-8 -*-
- from selenium.webdriver import Remote
- from selenium.webdriver.chrome import options
-
- class ReuseBrowser(Remote):
-
- def __init__(self, command_executor, session_id):
- self.r_session_id = session_id
- if command_executor and session_id:
- Remote.__init__(self, command_executor=command_executor, desired_capabilities={})
-
- def start_session(self, desired_capabilities, browser_profile=None):
- capabilities = {'desiredCapabilities': {}, 'requiredCapabilities': {}}
- for k, v in desired_capabilities.items():
- if k not in ('desiredCapabilities', 'requiredCapabilities'):
- capabilities['desiredCapabilities'][k] = v
- else:
- capabilities[k].update(v)
- if browser_profile:
- capabilities['desiredCapabilities']['firefox_profile'] = browser_profile.encoded
-
- self.w3c = "specificationLevel" in self.capabilities
- self.capabilities = options.Options().to_capabilities()
- self.session_id = self.r_session_id
- self.w3c = False
-
- class FileHandle():
- def __init__(self):
- self.file_path="C:\\ktmp\\browserdriverinfo.txt"
- def write_file(self,data):
- if not os.path.exists(self.file_path):
- with open(self.file_path,"w") as fp:
- fp.write(data)
- fp.close()
- else:
- datas=self.read_file()
- if datas and data:
- if str(datas[-1].split("|")[0])!=str(data.split("|")[0]):#如果text中数据存的日期和新现有日期不同就直接覆盖原有数据(即只存当天数据)
- with open(self.file_path, "w") as fp:
- fp.write(data)
- fp.close()
- else:
- with open(self.file_path, "a") as fp:#如果相同就就追加数据
- fp.write("\n"+data)
- fp.close()
-
- elif not datas and data:
- with open(self.file_path, "w") as fp:
- fp.write(data)
- fp.close()
-
- def read_file(self):
- if not os.path.exists(self.file_path):
- raise Exception,u"未找处在自动化会话状态的浏览器和驱动,请检查!"
- else:
- with open(self.file_path, "r") as fp:
- return fp.readlines()
2.2在selenium2library\keywords\_browsermanagement.py文件三处修改:
第一处增加代码如下:
第二处增加:
print "executor_url:",browser.command_executor._url
print "session_id:",browser.session_id
date=time.strftime('%Y%m%d',time.localtime(time.time()))
getb.FileHandle().write_file(date+"|"+browser.command_executor._url+"|"+browser.session_id)
第三处修改如下 :
代码:
def get_browser(self, executor_url=None, session_id=None, alias=None,remote_url=False,
desired_capabilities=None,ff_profile_dir=None):
executor_url=executor_url
session_id=session_id
if executor_url==None or session_id==None:
data=getb.FileHandle().read_file()
if data:
data=data[-1].split("|")
executor_url=data[1].strip("\n")
session_id=data[2].strip("\n")
else:
print "No browsers and drivers in the automated session state are found, please check!"
try:
browser =getb.ReuseBrowser(command_executor=executor_url, session_id=session_id)
return self._cache.register(browser, alias)
except Exception:
print Exception
2.3修改selenium2library中__init__().py文件如下:
和unitest等自动化用例管理框架结合就更加简单了
问题1:如果报错是:WebDriverException: Message: chrome not reachable----表示浏览器驱动存在,但是对于的浏览器窗口关闭。
问题2:如果报错是:URLError: <urlopen error [Errno 10061] >----表示浏览器驱动不存在
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。