[英]python selenium all test in one session browser
I have a question. 我有个问题。
I have 10 classes in 10 files and one main class and class with user log. 我在10个文件中有10个班级,一个主班级和一个带有用户日志的班级。 I run all tests in the main class.
我在主类中运行所有测试。 I would like to run 10 at once.
我想一次运行10个。 At present, the browser opens up to 10 times with each class.
目前,每个类的浏览器最多打开10次。 I would like 10 tests to take place in 1 browser session.
我希望在1个浏览器会话中进行10个测试。 It is possible?
有可能的? Because I can not do this.
因为我做不到。 Please, help me, thank you!
请帮帮我,谢谢! Me login class:
我的登录课程:
class Login(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.browser = webdriver.Chrome()
def setUp(self):
self.browser.get("www")
username = self.browser.find_element_by_id("username")
password = self.browser.find_element_by_id("password")
username.send_keys("aaaa")
password.send_keys("ssswww")
self.browser.find_element_by_id("button").click()
def tearDown(self):
self.browser.get("about:blank")
@classmethod
def tearDownClass(cls):
cls.browser.quit()
I would like 10 tests to take place in 1 browser session.
我希望在1个浏览器会话中进行10个测试。
This code will open one browser and then use it for multiple tests. 此代码将打开一个浏览器,然后将其用于多个测试。 I'm not sure that this is the best way to do selenium tests and you may want to clear cookies as shown in the accepted answer there but I'll leave that to you.
我不确定这是否是进行硒测试的最佳方法 ,您可能想要清除cookie(如此处接受的答案所示),但我将留给您。
import time
import pytest
from selenium import webdriver
# if you omit `scope='module'`, every test will open a new browser
# this way it will use the same browser for each test
@pytest.fixture(scope='module')
def driver():
d = webdriver.Chrome()
# d.get("www")
# username = d.find_element_by_id("username")
# password = d.find_element_by_id("password")
# username.send_keys("aaaa")
# password.send_keys("ssswww")
# d.find_element_by_id("button").click()
yield d
d.quit()
@pytest.mark.parametrize('url', (
'https://google.com',
'https://yahoo.com',
'https://bing.com',
))
def test_hmm(driver, url):
driver.get(url)
time.sleep(3)
assert True
Console: 安慰:
$ pytest hmm.py
======================================================= test session starts ========================================================
platform linux -- Python 3.6.4, pytest-3.4.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/lettuce/Dropbox/Python/Python_3, inifile:
plugins: hypothesis-3.46.0
collected 3 items
hmm.py ... [100%]
==================================================== 3 passed in 15.34 seconds =====================================================
capybara-py , which provides a layer on top of Selenium, uses a single browser session, taking care of the necessary cleanup for you: capybara-py在Selenium之上提供了一层,它使用一个浏览器会话,为您进行必要的清理:
import capybara
class Login(unittest.TestCase):
def setUp(self):
self.page = capybara.dsl.page
self.page.visit("http://www.example.com")
self.page.fill_in("Username", value="aaaa")
self.page.fill_in("Password", value="ssswww")
self.page.click_button("Sign in")
def tearDown(self):
capybara.reset_sessions()
You can simply initialize Chrome webdriver inside your Login
class, instead of initializing it in the setUp()
class. 您只需在
Login
类中初始化Chrome Webdriver,而不用在setUp()
类中对其进行初始化。 This way, the browser will open once for each test. 这样,浏览器将为每个测试打开一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.