gpt4 book ai didi

Python 网络驱动程序 : reusing setUp and tearDown

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:54 25 4
gpt4 key购买 nike

你好,你能给我一个在另一个类中重用 setUp 和 tearDown 的例子吗

基础.py

from selenium import webdriver
class Login(object):
def setUp(self):
# create a new Firefox session
self.driver = webdriver.Firefox()
self.driver.get("google.com")
self.driver.implicitly_wait(30)
self.driver.maximize_window()

def tearDown(self):
self.driver.quit()

登录.py

from selenium import webdriver
import base
import unittest

class LoginPage(unittest.TestCase):
...how to call setUp() and tearDown() from base.py...
def test_logn(self):
self.driver.find_element_by_id('username')
...etc...

能否提供示例,请在 pc 上运行它,看看它是如何工作的。谢谢

最佳答案

使LoginPage成为base.Login的子类,使其继承Login.setUpLogin.tearDown >:

基础.py

import unittest
from selenium import webdriver

class Login(unittest.TestCase):

def setUp(self):
# create a new Firefox session
self.driver = webdriver.Firefox()
self.driver.get("http://google.com")
self.driver.implicitly_wait(30)
self.driver.maximize_window()

def tearDown(self):
self.driver.quit()

登录.py

from selenium import webdriver
import base
import unittest

class LoginPage(base.Login):

def test_logn(self):
elt = self.driver.find_element_by_name('q')
elt.send_keys('python')
elt.submit()
for result in self.driver.find_elements_by_class_name('g'):
print(result.text)
print('-' * 80)

if __name__ == '__main__':
unittest.main()

关于Python 网络驱动程序 : reusing setUp and tearDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34107414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com