【问题标题】:Selenium, with Python, how to simplify scripts so that I can run them from other python scripts?Selenium,使用 Python,如何简化脚本以便我可以从其他 python 脚本运行它们?
【发布时间】:2023-03-22 03:59:01
【问题描述】:

我在弄清楚如何取出硒条中不需要的东西并将其打包成可以从另一个脚本中调用它的方式时遇到了一些麻烦。我无法理解发生了什么这个,因为我不知道单元测试部分的来源......理想情况下,如果我可以将它分成一个我可以称之为的函数,这将是一个想法,感谢您的任何建议。

(而且,是的,我确实需要硒,我请您不要建议替代品,因为我将在很多事情上使用硒,所以我需要弄清楚这一点)

这只是一个基本的演示脚本:

from selenium import selenium

import unittest



class TestGoogle(unittest.TestCase):

    def setUp(self):

        self.selenium = selenium("localhost", \

            4444, "*firefox", "http://www.bing.com")

        self.selenium.start()



    def test_google(self):

        sel = self.selenium

        sel.open("http://www.google.com/webhp")

        sel.type("q", "hello world")

        sel.click("btnG")

        sel.wait_for_page_to_load(5000)

        self.assertEqual("hello world - Google Search", sel.get_title())



    def tearDown(self):

        self.selenium.stop()



if __name__ == "__main__":

    unittest.main()

【问题讨论】:

  • 哦,我刚刚找到了这个:coreygoldberg.blogspot.com/2009/09/… 这似乎是我正在寻找的东西,我会留下这个,因为关于这个主题的内容并不多,我可以找到

标签: python unit-testing selenium


【解决方案1】:

我建议在您的其他脚本中创建函数,这些函数具有对测试用例的引用作为参数。这样,如果出现问题,您的函数可能会使测试用例失败。像这样(在谷歌搜索一个字符串并检查标题):

def search_s(utest, in_str):
  s = utest.selenium
  s.type('q', in_str)
  s.click('btnG')
  s.wait_for_page_to_load('30000')
  utest.assertEqual("%s - Google Search" % (in_str,), s.get_title())

然后,在您的测试用例中,这样称呼它:

def test_google(self):
  s.open('/')
  search_s(self, "hello world")

然后您可以制作这些类型的方法的库,允许您混合搭配您的测试。

【讨论】:

  • 谢谢..是的,这基本上是我在找到其他信息后开始做的......感谢发布此
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 2019-12-18
  • 2011-09-22
  • 2022-01-08
  • 2021-04-03
  • 1970-01-01
相关资源
最近更新 更多