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

问题描述 投票:0回答:2

我有个问题。

我有10个文件中的10个类和一个带有用户日志的主类和类。我在主类中运行所有测试。我想一次跑10。目前,每个类别的浏览器最多可打开10次。我想在1个浏览器会话中进行10次测试。有可能的?因为我不能这样做。拜托,帮帮我,谢谢!我登录类:

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()
python selenium login webdriver
2个回答
0
投票

我想在1个浏览器会话中进行10次测试。

此代码将打开一个浏览器,然后将其用于多个测试。 I'm not sure that this is the best way to do selenium tests你可能想要清除那里接受的答案中的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

安慰:

$ 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 =====================================================

0
投票

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()
© www.soinside.com 2019 - 2024. All rights reserved.