Selenium Python运行多个测试用例,我如何一次登录并使用相同的实例浏览器 [英] Selenium Python run multiple test cases how do i log in once and use the same instance browser

查看:1594
本文介绍了Selenium Python运行多个测试用例,我如何一次登录并使用相同的实例浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Selenium Python中创建了一些测试用例,并将其放在了测试套件中.每次运行测试用例时,它都会打开浏览器并导航到URL. 然后登录,进行一些测试,然后注销,然后浏览器关闭.

I have created some Test Cases in Selenium Python and I have put it in a Test Suite. Each time a Test Case runs it opens the browser and navigates to the URL. It then logs in, does some tests and then logs out and the browser closes.

是否有一种方法可以运行测试以仅打开浏览器的一个实例,一次登录并在其余测试用例中继续使用该实例?

Is there a way to run the tests to only open 1 instance of the browser, log in once and keep using that instance for the rest of the test cases?

我不想为每个测试用例都关闭浏览器并打开新的浏览器并每次登录.

I do not want to close the browser for every single test case and open a new browser and log in each time.

例如 测试用例1运行,打开浏览器,导航到URL,登录,运行一些测试.注销并关闭浏览器.

For e.g. Test Case 1 runs, opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

测试用例2运行,打开浏览器,导航到URL,登录,运行一些测试.注销并关闭浏览器.

Test Case 2 runs, opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

测试用例3打开浏览器,导航到URL,登录,运行一些测试.注销并关闭浏览器.
等等.

Test Case 3 opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.
and so on.

我想这样做.

测试用例1运行,打开浏览器,导航到URL,登录,运行一些测试.注销并关闭浏览器.

Test Case 1 runs, opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

测试用例2使用与测试用例1相同的浏览器,您仍处于登录状态,运行一些测试.

Test Case 2 use the same browser from test case 1, you are still logged in, run some tests.

测试用例3使用与测试用例1相同的浏览器,您仍处于登录状态,运行一些测试.

Test Case 3 use the same browser from test case 1, you are still logged in, run some tests.

最后一个测试用例使用的是与测试用例1相同的浏览器,您仍然登录并运行了一些测试.注销,关闭浏览器.

The last test case use the same browser from test case 1, you are still logged in, run some tests. Log out, close the browser.

为每个测试用例打开一个新的浏览器并登录需要更长的时间才能完成测试.

Opening a new browser for every single test case and logging in takes longer for the tests to complete.

我的代码段如下:

class BaseTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
    cls.driver = webdriver.Ie(Globals.IEdriver_path)
    cls.driver.get(Globals.URL_justin_pc)
    cls.login_page = login.LoginPage(cls.driver)
    cls.driver.implicitly_wait(120)
    cls.driver.maximize_window()

@classmethod
def tearDownClass(cls):
    cls.login_page.click_logout()
    cls.driver.close()


Test Case 1

class AdministrationPage_TestCase(BaseTestCase):

    def test_add_Project(self):
        print "*** test_add_project ***"
        self.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
        menu_bar = MenuBarPage(self.driver)
        administration_page = menu_bar.select_menuBar_item("Administration")
        administration_page.click_add_project_button()
        administration_page.add_project(project_name, Globals.project_description)
        administration_page.click_save_add_project()
        # etc ...

    def test_edit_Project(self):    
    ...


Test Case 2

class DataObjectsPage_TestCase(BaseTestCase):

    def testa_add_Data_Objects_Name(self):
        print "*** test_add_Data_Objects - Name ***"
        self.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
        menu_bar = MenuBarPage(self.driver)
        data_configuration_page = menu_bar.select_menuBar_item("Data Configuration")
        project_navigator = ProjectNavigatorPage(self.driver)
        data_objects = project_navigator.select_projectNavigator_item("Data Objects")
        data_objects.click_add_button_for_data_objects()

    def testb_add_Data_Objects_Address(self):
        print "*** test_add_Data_Objects - Address ***"
        ...

    def testc_add_Data_Objects_Phone(self):
    ...

Test Case 3 and so on

我的测试套件是:

def suite():

test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(TestCases.AdministrationPage_TestCase.AdministrationPage_TestCase))
test_suite.addTest(unittest.makeSuite(TestCases.DataObjectsPage_TestCase.DataObjectsPage_TestCase))
# etc...

谢谢

Riaz

推荐答案

不必要的打开和关闭浏览器,硬编码的延迟等导致的缓慢的自动测试浪费了大量时间,并且几乎总是可以避免的经验.

Slow automated tests caused by needlessly opening and closing browsers, hardcoded delays etc. are, as you say, a huge waste of time, and almost always avoidable in my experience.

好消息是,您无需执行任何特殊操作即可避免这种情况.如果您的测试是独立的并且按顺序运行,并且还假设您只有一个浏览器/版本并且设置为Capabilities,那么您的测试运行程序所需要做的就是:

The good news is that you don't need to do anything special to avoid this. If your tests are independent and run in sequence, also assuming that you only have one browser/version and set of Capabilities, then all your test runner needs to do is:

  • 在运行开始时创建单例驱动程序(或应用程序,如果允许多次运行,则创建;或者在第一次需要时懒惰地创建)
  • 按顺序运行每个测试,没有显式的closequit调用,而是进行所有其他适当的清理(清除所有创建的cookie,注销,清除会话,本地存储等)
  • quit(非常)端的驱动程序
  • create singleton Driver at start of run (or application, if multiple runs allowed; or lazily when first required)
  • run each test in sequence, with no explicit close or quit calls, but with all other suitable cleanup (clearing any created cookies, logging out, clearing sessions, Local storage etc.)
  • quit Driver at the (very) end

以几乎相同的方式支持多种浏览器/功能并不是很多工作.

It's not very much more work to support multiple browsers / capabilities in much the same way.

这篇关于Selenium Python运行多个测试用例,我如何一次登录并使用相同的实例浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆