简体   繁体   English

python selenium全部在一个会话浏览器中进行测试

[英]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.

相关问题 Python Selenium-一个会话浏览器 - Python Selenium - One session browser Selenium Hub,启动2个浏览器仅在其中1个中运行测试(Selenium python) - Selenium Hub, Launches 2 browser only runs test in 1 of them (selenium python) 硒重用浏览器会话 - Selenium reusing browser session 如何使用 Selenium 和 Python 启动基于 Chromium 的 Vivaldi 浏览器 session - How to initiate a Chromium based Vivaldi browser session using Selenium and Python 使用 Python Selenium 在新浏览器中保持登录(保存会话) - Stay logged in(save session) in new browser with Python Selenium 如何在 python selenium 中为每个测试启动单独的浏览器 - how to launch seperate browser for each test in python selenium "如何重用 selenium 浏览器会话" - How to reuse a selenium browser session 无论如何运行 selenium 的 session 与现有的浏览器 session - Is there anyway to run a session of selenium with an exisiting session of a browser 如何使用Selenium-Python恢复浏览器会话或使用现有的浏览器窗口? - How to resume browser session or use existing browser window with Selenium-Python? 异常:无法启动新的浏览器会话:在Python中启动浏览器Selenium时出错 - Exception: Failed to start new browser session: Error while launching browser Selenium in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM