使用Selenium执行所有用例的时候浏览器只打开一次,所有用例执行完以后关闭浏览器


from selenium import webdriver
import unittest
import time


# 定义driver的局部作用域,不在setUp内部,这种方式保证了浏览器只打开一次,所有用例执行完以后执行tearDownModule方法关闭浏览器

class Setting:
    driver = None
    url = None


def setUpModule():
    Setting.driver = webdriver.Chrome()
    Setting.driver.maximize_window()
    Setting.driver.implicitly_wait(10)
    Setting.url = "https://XXXXXXXXX.yyyyyyy.cn/"


def tearDownModule():
    Setting.driver.quit()


class MyTest(unittest.TestCase):

    def login(self):
        Setting.driver.get(Setting.url)
        Setting.driver.find_element_by_id('telphone').send_keys('15936558246')
        Setting.driver.find_element_by_id('password').send_keys('123456')
        Setting.driver.find_element_by_id('submitBtn').click()
        time.sleep(16)

    def test01_login(self):
        '''登录工作台'''
        self.login()
        title = Setting.driver.title
        print(title)
        self.assertEqual(title, "名字来一个")
        time.sleep(10)

    def test02_developer(self):
        '''进入开发者模式'''

        Setting.driver.find_element_by_xpath('//*[@id="root"]/div/header/div/ul/li[4]').click()
        time.sleep(5)
        print('第1个', Setting.driver.current_url)
        self.assertIn(Setting.driver.current_url,
                      'https://XXXXXXXXX.yyyyyyy.cn/?tenantId=e6f54b6f-7547-46a5-b900-986a77986503&resourceGroupId=286c67f1-28c5-4000-ba31-97fbda2eb6ae&mode=developer')

    def test03_user(self):
        '''进入用户模式'''

        Setting.driver.find_element_by_xpath('//*[@id="root"]/div/header/div/ul/li[4]').click()
        time.sleep(1)
        Setting.driver.find_element_by_xpath('//*[@id="root"]/div/header/div/ul/li[2]').click()
        time.sleep(5)
        print('第2个', Setting.driver.current_url)
        self.assertIn(Setting.driver.current_url,
                      'https://XXXXXXXXX.yyyyyyy.cn/?tenantId=e6f54b6f-7547-46a5-b900-986a77986503&resourceGroupId=286c67f1-28c5-4000-ba31-97fbda2eb6ae&mode=user')


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


posted @ 2020-12-01 16:42  大海一个人听  阅读(1318)  评论(0编辑  收藏  举报