【问题标题】:Use the same browser session for multiple tests with Selenium Pytest使用 Selenium Pytest 对多个测试使用相同的浏览器会话
【发布时间】:2016-07-22 12:01:14
【问题描述】:

我希望能够在同一个测试文件中使用同一个浏览器会话进行多个测试。

我为登录设置了一个类:

class Loginpage ():
 url="http://appsv01:8084/#/"

 def __init__(self, workbook):
    self.workbook=workbook

 def login(self,value_Name,worksheet):
    #Open a new mymobile suite window in Chrome and maximize
    driver = webdriver.Chrome('C:/temp/chromedriver.exe')
    driver.get("http://appsv01:8084")
    driver.maximize_window()

我一直在关闭浏览器会话,然后在每次测试时打开一个新会话,但我尝试更改它以使结构看起来像(在名为 test_mytests.py 的文件中):

   #open the browser and log in
   mylogin=Loginpage('C:\Automation\Common_Objects.xlsx')
   driver=mylogin.login("AutoSMS", "Users")

   #perform the first test
   def test_one():
    task1
    task2

   #perform the second test
    def test_two():
    task3
    task4

这失败并出现错误:

E   ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

如果我将#open the browser 的代码分别放在每个测试下,那么一切正常。是否可以只打开一次浏览器并让文件中的所有测试在同一个浏览器会话上运行?

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您可以将驱动程序对象传递给函数调用:

     #open the browser
     driver = webdriver.Chrome('C:/temp/chromedriver.exe')
     driver.get("http://appsv01:8084")
     driver.maximize_window()
    
     #perform the first test
     def test_one(driver):
      #do something with driver here, e.g.
      driver.find_element_by_id('test').click()
    
    
     #perform the second test
     def test_two(driver):
       task3
       task4
    
     #function calls
     test_one(driver)
     test_two(driver)
    
     #close driver
     driver.close()
    

    【讨论】:

    • 我已经在使用驱动了(对不起,我应该在我的原始帖子中包含它): class Loginpage (): url="appsv01:8084/#" def __init__(self, workbook): self .workbook=workbook def login(self,value_Name,worksheet): #在Chrome中打开一个新窗口并最大化 driver = webdriver.Chrome('C:/temp/chromedriver.exe') driver.get("appsv01:8084") driver .maximize_window() def test_one: mylogin = Loginpage('C:\Automation\Common_Objects.xlsx') driver = mylogin.login("AutoSMS", "Users")
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2012-12-26
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多