当前位置:   article > 正文

第三篇:web自动化动态调试方法和selenium2library库结合_seleniumlibrary动态

seleniumlibrary动态

一、基本思路

      每次使用open browser浏览器时都将当前的会话session_id和 excutor_url 按存在本地,在每一次加载senlium2brary库时候就使用ReuseBrowser去保存最新加载id和url。

二 、selenium2library库文件新增如下:

2.1 在selenium2library目录下增加getb.py文件

getb.py文件内容如下:

  1. #-*- coding:utf-8 -*-
  2. from selenium.webdriver import Remote
  3. from selenium.webdriver.chrome import options
  4. class ReuseBrowser(Remote):
  5. def __init__(self, command_executor, session_id):
  6. self.r_session_id = session_id
  7. if command_executor and session_id:
  8. Remote.__init__(self, command_executor=command_executor, desired_capabilities={})
  9. def start_session(self, desired_capabilities, browser_profile=None):
  10. capabilities = {'desiredCapabilities': {}, 'requiredCapabilities': {}}
  11. for k, v in desired_capabilities.items():
  12. if k not in ('desiredCapabilities', 'requiredCapabilities'):
  13. capabilities['desiredCapabilities'][k] = v
  14. else:
  15. capabilities[k].update(v)
  16. if browser_profile:
  17. capabilities['desiredCapabilities']['firefox_profile'] = browser_profile.encoded
  18. self.w3c = "specificationLevel" in self.capabilities
  19. self.capabilities = options.Options().to_capabilities()
  20. self.session_id = self.r_session_id
  21. self.w3c = False
  22. class FileHandle():
  23. def __init__(self):
  24. self.file_path="C:\\ktmp\\browserdriverinfo.txt"
  25. def write_file(self,data):
  26. if not os.path.exists(self.file_path):
  27. with open(self.file_path,"w") as fp:
  28. fp.write(data)
  29. fp.close()
  30. else:
  31. datas=self.read_file()
  32. if datas and data:
  33. if str(datas[-1].split("|")[0])!=str(data.split("|")[0]):#如果text中数据存的日期和新现有日期不同就直接覆盖原有数据(即只存当天数据)
  34. with open(self.file_path, "w") as fp:
  35. fp.write(data)
  36. fp.close()
  37. else:
  38. with open(self.file_path, "a") as fp:#如果相同就就追加数据
  39. fp.write("\n"+data)
  40. fp.close()
  41. elif not datas and data:
  42. with open(self.file_path, "w") as fp:
  43. fp.write(data)
  44. fp.close()
  45. def read_file(self):
  46. if not os.path.exists(self.file_path):
  47. raise Exception,u"未找处在自动化会话状态的浏览器和驱动,请检查!"
  48. else:
  49. with open(self.file_path, "r") as fp:
  50. 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] >----表示浏览器驱动不存在

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/608264
推荐阅读
相关标签
  

闽ICP备14008679号