Skip to main content
 首页 » 编程设计

python之关于构建 selenium/python 框架的建议

2025年04月02日23artech

    import sys 
    import os 
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) 
    from selenium import web driver 
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
    from selenium.common.exceptions import NoSuchElementException 
    from selenium.webdriver.support.ui import WebDriverWait 
 
    class WebDriver(): 
 
      def setup(self): 
         self.driver = WebDriver() 
         self.driver = webdriver.Firefox() 
         self.base_url = "www.google.com" 
         self.driver.get(self.base_url) 
         self.driver.delete_all_cookies() 
         self.driver.implicitly_wait(30) 
         self.verificationErrors = [] 
         self.accept_next_alert = True 
         self.driver.maximize_window() 
 
      def teardown(self): 
         self.driver.quit() 

我想将其用作我的自动化框架中的基础文件,但它似乎不起作用。请帮忙!

另外,注意缩进,而不是这里遇到的问题。 我希望能够导入它并让它为我添加到框架的每个脚本运行设置和拆除。

如果能帮助理解如何构建框架,我们将不胜感激!谢谢

请您参考如下方法:

您打算使用什么测试框架?这将完全改变您用于在测试(或整个测试套件)之前/之后运行此逻辑的语法。

其他注意事项:

你在做什么类型的测试?

  • “单元测试”? (配额 b/c 你不能*在 python 中为网页进行单元测试)
  • 集成测试
  • 压力/性能测试?

如果您将 Selenium 视为对您的网络应用程序的 UI 进行单元测试的一种方式,您可能需要查看一些 JavaScript 测试框架。如果您在 UI 中使用 任何 JavaScript,一定要检查 JavaScript 框架。使用 JavaScript 来操作 DOM,并尝试使用 Selenium 来操作 DOM 是 DOM 上的一个巨大竞争条件。

您打算使用 Selenium 测试什么?

  • 检查您的 UI 按钮是否正确连接?
  • 您的网络应用程序的业务规则?

我强烈建议您使用 Selenium 来验证网络应用程序中的快乐路径(即我可以单击此按钮),并且不测试您的业务规则;锤击 API 来执行这些业务规则。 API 比 UI 更不可能发生变化,并且 UI 的变化通常会导致您的 Selenium 测试出现误报(测试中断导致失败,而不是应用程序中的真实失败)。

请不要因此而气馁!您正在编写测试真是太棒了!

如果使用得当,Selenium 是一个很好的工具,它很容易重载并导致测试不一致(取决于 JS 的数量和 JS 框架)。

具体关于您的代码的指针:

使类成为您可以根据需要实例化和绑定(bind)的东西 - 使跨框架使用代码更容易,并且调试更容易 b/c 您只需打开 python 解释器并使用它。

# file named my_webdriver.py 
 
import sys 
import os 
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) 
from selenium import webdriver 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.support.ui import WebDriverWait 
 
class MyDriver(): 
 
  def __init__(self): 
     self.driver = webdriver.Firefox() 
     self.base_url = "www.google.com" 
     self.driver.implicitly_wait(30) 
     #self.verificationErrors = []  # delete this line, and deal with errors in the framework 
     self.accept_next_alert = True 
     self.driver.maximize_window() 
     self.reset() 
 
  def reset(self): 
      """So I can be lazy and reset to a know starting point before each test case""" 
      self.driver.get(self.base_url) 
      self.driver.delete_all_cookies() 
 
  def teardown(self): 
     self.driver.quit() 

使用它:

from my_webdriver import MyDriver 
 
driver = MyDriver() 
driver.get('http://my-awesome-app.org') 
element = driver.find_element_by_id('some-id') 
element.click() 

将其绑定(bind)在单元测试框架内:

import unittest 
 
from my_webdriver import MyDriver 
 
class AwesomeTest(unittest.TestCase): 
 
    @classmethod 
    def setUpClass(cls): 
        """Runs once per suite""" 
        cls.driver = MyDriver() 
 
    def setUp(self): 
        """Runs before each test case""" 
        self.driver.reset() 
 
    @classmethod 
    def tearDownClass(cls): 
        cls.driver.teardown() 
 
    def test_stuff(self): 
        """a test case!""" 
        # stuff 
        pass 

祝你好运!希望这有帮助/有用。

*我在 PyCon 上看到了一些关于使用 Python 来操纵 DOM 的东西,但我认为没有人在生产中这样做。