我在弄清楚如何取出 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/