• python+selenium多线程启动多个不同浏览器


    前言
    如果想用多个浏览器跑同一套测试代码,driver=webdriver.Firefox()这里的driver就不能写死了,可以把浏览器名称参数化。
    后续如果想实现多线程同时启动浏览器执行用例,用前面讲的tomorrow模块,设置下线程数套用下就可以了

    启动浏览器
    1.为了实现多个浏览器的灵活切换,可以把启动浏览器写一个函数,参数用浏览器名称就行了

     def startBrowser(name):
          """
          打开浏览器函数,"firefox""chrome""ie""phantomjs"
          """
          try:
              if name == "firefox" or name == "Firefox" or name == "ff":
                  print("start browser name :Firefox")
                  driver = webdriver.Firefox()
                  return driver
              elif name == "chrome" or name == "Chrome":
                  print("start browser name :Chrome")
                  driver = webdriver.Chrome()
                  return driver
              elif name == "ie" or name == "Ie":
                  print("start browser name :Ie")
                  driver = webdriver.Ie()
                  return driver
              elif name == "phantomjs" or name == "Phantomjs":
                  print("start browser name :phantomjs")
                  driver = webdriver.PhantomJS()
                  return driver
              else:
                  print("Not found this browser,You can use 'firefox', 'chrome', 'ie' or 'phantomjs'")
          except Exception as msg:
              print("启动浏览器出现异常:%s" % str(msg))

    多线程启动不同浏览器
    1.代码参考:

    # coding:utf-8
      from selenium import webdriver
      import time
      from tomorrow import threads
      
      def startBrowser(name):
          """
          打开浏览器函数,"firefox""chrome""ie""phantomjs"
          """
          try:
              if name == "firefox" or name == "Firefox" or name == "ff":
                  print("start browser name :Firefox")
                  driver = webdriver.Firefox()
                  return driver
              elif name == "chrome" or name == "Chrome":
                  print("start browser name :Chrome")
                  driver = webdriver.Chrome()
                  return driver
              elif name == "ie" or name == "Ie":
                  print("start browser name :Ie")
                  driver = webdriver.Ie()
                  return driver
              elif name == "phantomjs" or name == "Phantomjs":
                  print("start browser name :phantomjs")
                  driver = webdriver.PhantomJS()
                  return driver
              else:
                  print("Not found this browser,You can use 'firefox', 'chrome', 'ie' or 'phantomjs'")
          except Exception as msg:
              print("启动浏览器出现异常:%s" % str(msg))
      
      @threads(5)
      def run_case(name):
          driver = startBrowser(name)
          driver.get("https://www.cnblogs.com/yoyoketang/")
          time.sleep(3)
          print(driver.title)
          driver.quit()
      
      if __name__ == "__main__":
          names = ["chrome", "ff", "ie"]
          for i in names:
              run_case(i)
    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    Linux下安装mysql(yum和源码编译两种方式)
    搭建Hadoop集群(centos6.7+hadoop-2.7.3)
    xen虚拟机(Centos6.6)的创建和扩容
    Apache Ranger对HDFS的访问权限控制的原理分析(一)
    Block Token 原理分析
    Hadoop安全机制之令牌
    VS 2012 Unit Test
    java 并发
    图片相关
    cmd出现乱码
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/15071250.html
Copyright © 2020-2023  润新知