python打开一个软件并进行操作,Selenium Python运行多个测试用例,我如何一次登录并使用同一实例浏览器...

本文讨论了如何在Selenium Python测试用例中避免每次测试都打开和关闭浏览器,从而提高测试执行速度。通过创建单一的浏览器实例并在所有测试用例间复用,可以实现登录一次并保持登录状态,直到所有测试完成后才注销并关闭浏览器。这种方法减少了登录和关闭操作,提升了自动化测试的效率。
摘要由CSDN通过智能技术生成

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.

For e.g.

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

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

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

and so on.

I would like to do it this way.

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

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

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

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.

My code snippet is as follows:

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

My Test Suite is:

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...

Thanks,

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.

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:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值