我在这里有以下代码:
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实例执行多个测试用例。
答案 0 :(得分:0)
您必须在__init__
初始化驱动程序。
答案 1 :(得分:0)
在__init__()
中分配网络驱动程序,但不要忘记调用super.__init__()
以免破坏测试机制:
def __init__(self, *args):
super().__init__(*args)
self.driver = webdriver.Firefox()