【问题标题】:Rum multiple test cases using python selenium webdriver on same web browser session在同一 Web 浏览器会话上使用 python selenium webdriver 朗姆多个测试用例
【发布时间】:2016-03-21 08:40:56
【问题描述】:

我这里有以下代码:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

class MyTest1(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        driver = cls.driver
        driver.get("https://somewebsite.com")
        print "login the website"

    def test_UI_login(self):
        driver = self.driver
        print "test some things here"

    def test_duplicate_client(self):
        driver = self.driver
        print "test some things here"


    def tearDown(cls):
        cls.driver.close()


if __name__ == '__main__':
    unittest.main()

我面临的问题是, 在第一个函数 test_UI_login 之后,firefox 实例关闭。如何使用 selenium 在 unittest 中从同一个 Firefox 实例执行多个测试用例。

【问题讨论】:

  • 如果你也用@classmethod注释tearDown也许会有所帮助?
  • 如果我们用@classmethod注解tearDown是不行的

标签: selenium python-unittest


【解决方案1】:

this SO-answer中所述

您必须在__init__ 中初始化驱动程序。

【讨论】:

  • 我按照你的建议试过了。但是,我遇到了错误,typeerror __init__() 需要 1 个位置参数,但给了 2 个 class MyTest1(unittest.TestCase): def __init__(self): self.driver = webdriver.Firefox() driver = self.driver跨度>
  • 你必须声明__init__(cls, tests) 造成误解,我已经有一段时间没有使用它了。看起来 selenium 在最新版本中添加了一个额外的参数。
  • 按照建议尝试了方法(def __init__(cls, tests)),注意到下面的错误 'MyTest1' object has no attribute '_testMethodName'
  • 哦,对不起。忘了提到如果你覆盖它,你必须隐式调用超类 init 。因此,将 `unittest.TestCase.__init__(cls, tests)` 添加为 init 中的第一行应该可以工作(至少它对我有用):-)。
【解决方案2】:

__init__() 中分配webdriver,但不要忘记调用super.__init__() 以免破坏测试机制:

    def __init__(self, *args):
        super().__init__(*args)
        self.driver = webdriver.Firefox()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2015-01-17
    • 2016-08-16
    • 2013-04-11
    • 1970-01-01
    • 2012-04-03
    • 2013-08-23
    相关资源
    最近更新 更多