python - Selenium,使用 Python,如何简化脚本以便我可以从其他 python 脚本运行它们?

标签 python unit-testing selenium

我在弄清楚如何取出 Selenium 条中不需要的内容并以可以从另一个脚本调用它的方式打包它时遇到了一些麻烦。我无法理解发生了什么这是因为我不知道单元 testint 部分来自哪里...理想情况下,如果我可以将其分离到一个我可以调用的函数中,那就太好了,感谢您的任何建议。

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

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

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()

最佳答案

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

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")

然后,您可以创建这些类型的方法的库,从而允许您混合和匹配测试的各个部分。

关于python - Selenium,使用 Python,如何简化脚本以便我可以从其他 python 脚本运行它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3577986/

相关文章:

python - word2vec 负采样 skip gram 模型的正确梯度

python - 帮助新手了解 TensorFlow 中的线性代数(三阶张量)

python - 编写 BIG pandas DataFrame 的最快方法

android - 如何模拟应用程序类来单元测试 ViewModel

c# - PrivateObject 找不到属性

ruby - 丰富的 sproutcore 应用程序的 QA 自动化

python - 为什么我的代码不打印产品链接?

python - 如何在具有 Decimal 类型值的 pandas TimeSeries 上使用 mean 方法?

c++ - 谷歌模拟 : why NiceMock does not ignore unexpected calls?

java - Selenium 作为 Windows 服务运行以截取错误的屏幕截图