1

目前,我的测试代码每次通过特定页面的测试时都会调用 sign_in() 函数,我想登录一次并完成所有页面的测试,以便完成测试所需的时间更少。附件是来自基类的代码和测试页面之一。//这是基础测试类 import unittest2 from selenium import webdriver

类 BaseTestClass(unittest2.TestCase):

@classmethod
def setUpClass(cls):
    """Setups the connection to selenium and defines the broswer in use"""
    #cls.driver = webdriver.Firefox()
    cls.driver = webdriver.Remote("http://bs-sel01.lonres.lan:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX)
    cls.test_instance = cls.my_test_class(cls.driver)
    cls.url = cls.driver.current_url
    # Window for main test. At this point the first window is always the main window
    cls.main_window = cls.driver.window_handles[0]


def setUp(self):
    self.verificationErrors = []
    self.driver.get(self.url)

@classmethod
def tearDownClass(cls):
    cls.driver.quit()

def tearDown(self):
    self.assertEqual([], self.verificationErrors)
    # After a test fixture we close all browsers except the main window
    for handle in self.driver.window_handles:
        if handle != self.main_window:
            self.driver.switch_to_window(handle)
            self.driver.close()
    self.driver.switch_to_window(self.main_window)

//这是测试页面之一

从 selenium 导入 webdriver 从 base_test_class 导入 BaseTestClass 从页面导入 PAGE_TITLES,ParkingSpacesPage 从 public_site_pages 导入 PublicSiteHomePage

#

类 TestParkingSpacesPage(BaseTestClass): my_test_class = PublicSiteHomePage

@classmethod
def setUpClass(cls):
    super(TestParkingSpacesPage, cls).setUpClass()
    cls.test_instance = cls.test_instance.sign_in(ParkingSpacesPage)
    cls.url = cls.driver.current_url

#---------------------------------------------------------------------------------------------------------
def test_property_address_link(self):
    """property address link on Parking space page"""
    post_code = self.test_instance.property_address_link()

    self.driver.implicitly_wait(800)
    self.assertIn("%s - Google Maps" % post_code.replace(u'\xa0', u' '), self.driver.title)

#---------------------------------------------------------------------------------------------------------
def test_postcode_link(self):
    """postcode link on Parking space page"""
    post_code = self.test_instance.postcode_link()
    self.assertIn("%s - Google Maps" % post_code.replace(u'\xa0', u' '), self.driver.title)

#----------------------------------------------------------------------------------------------------------
def test_agent_name_link(self):
    """agent name link on Parking space page"""
    element = self.test_instance.agent_name_link()
    #confirm if name of property appears in new window title e.g "Lonres.com: Flat 9, 110 frampton Street, NW10"
    self.driver.implicitly_wait(800)
    assert ("Lonres.com: %s" % element) in self.driver.title

#--------------------------------------------------------------------------------------------------------------------
#the property needs to have at least one photo
def test_view_pic_link(self):
    """view picture link on Parking space page"""
    self.test_instance.view_pic_link()

    self.driver.implicitly_wait(800)
    self.assertIn(PAGE_TITLES["LonresPhotosPage"], self.driver.title)

#------------------------------------------------------------------------------------------
def test_send_to_mobile(self):
    """Send to mobile link on Parking space page"""
    self.test_instance.send_to_mobile_link()

    self.driver.implicitly_wait(800)
    self.assertIn(PAGE_TITLES["LonresSendMobilePhonePage"], self.driver.title)

#-------------------------------------------------------------------------------------
def test_thumbnail_image_link(self):
    """photo link on Parking space page"""
    self.test_instance.image_thumbnail_link()

    self.driver.implicitly_wait(800)
    self.assertIn(PAGE_TITLES["LonresPhotosPage"], self.driver.title)

#----------------------------------------------------------------------------------------------------------------
def test_send_valid_email(self):
    """This test sends a mail with a valid email address"""
    #check if we are on the test environment
    environment = 'https://test.lonres.lan'
    if environment not in self.driver.current_url:
        self.skipTest("This test is built to run on only https://test.lonres.lan")

    to_address = 'igba@lonres.com'
    index = 1
    email_page = self.test_instance.send_email(index)
    cc_address = email_page.get_cc_address()
    email_page.set_to_address(to_address)
    email_subject = email_page.get_email_subject()
    message_payload = email_page.send_email()
    self.assertIn('Lonres.com: Email Sent', self.driver.title)
    #inspect message payload
    self.assertEquals(to_address, message_payload['to'])
    self.assertEquals('Selenium Tester <igbaujege.lonrescomlimited@agentparticulars.com>', message_payload['from'])
    self.assertEquals(cc_address, message_payload['cc'])
    self.assertEquals(email_subject, message_payload['subject'])
4

2 回答 2

2

你确定要这么做吗?自包含的原子测试更容易调试和理解故障。

依赖其他测试使系统处于正确状态的测试是脆弱且不可靠的。

如果测试运行时是一个问题,也许您应该考虑使用 Selenium Grid 并行运行您的测试?

于 2013-08-28T12:24:13.093 回答
1

您可以创建一个全局布尔值hasSignedIn = false

然后在每次测试中,仅在必要时登录。

if not (hasSignedIn):
    sign_in()

然后,您可以尽可能避免登录,而无需依赖其他测试。检查布尔值只需要几毫秒,这样可以节省时间。

于 2013-08-28T16:49:12.833 回答