在运行由 Selenium IDE Python 远程控制插件格式化程序导出的多个 Python 测试脚本时,我遇到了一些问题。
1)python脚本完成后,浏览器窗口会自动关闭。例如,我在 Firefox 中运行测试。
2) Selenium 无法在 python 中导出它的测试套件。如何在 python 中复制测试套件功能?
我之所以花时间在 Python 中运行测试脚本是因为我们的测试用例解决方案 (Testuff) 软件允许 API 调用来更新通过 Selenium 测试用例自动化运行的相邻测试用例。
这是一个带有 API 调用的代码示例。
谢谢。
from selenium import selenium
import unittest, time, re
class python_script(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://test website url/")
self.selenium.start()
def test_python_script(self):
sel = self.selenium
from selenium import selenium
import unittest, time, re, urllib
class python_script(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://test website url/")
self.selenium.start()
def test_python_script(self):
sel = self.selenium
sel.open("http://192.168.48.23/labmatrix")
for i in range(60):
try:
if sel.is_element_present("//*[@name='username']"):
break
except: pass
#time.sleep(1)
else:
fields = {"test_id" : "testuff test_id number","status" : "failed"}
result = urllib.urlopen("testuff api url", urllib.urlencode(fields))
print result.read()
self.fail("time out")
sel.type("//*[@name='username']", "username")
for i in range(60):
try:
if sel.is_element_present("//*[@name='password']"): break
except: pass
#time.sleep(1)
else:
fields = {"test_id" : "testuff test_id number","status" : "failed"}
result = urllib.urlopen("testuff api url", urllib.urlencode(fields))
print result.read()
#self.fail("time out")
sel.type("//*[@name='password']", "password")
for i in range(60):
try:
if sel.is_element_present("//*[@id='submitButton']"): break
except: pass
#time.sleep(1)
else:
fields = {"test_id" : "testuff test_id number","status" : "failed"}
result = urllib.urlopen("testuff api url", urllib.urlencode(fields))
print result.read()
self.fail("time out")
sel.click("//*[@id='submitButton']")
#time.sleep(0.1)
for i in range(60):
try:
if sel.is_element_present("//*[@id='loadingDeck'][@selectedIndex='1']"):
fields = {"test_id" : "testuff test_id number","status" : "passed"}
result = urllib.urlopen("testuff api url", urllib.urlencode(fields))
print result.read()
break
except: pass
#time.sleep(1)
else:
self.fail("time out")
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
感谢您及时的回复。我用这段代码尝试了 jcfollower 的建议:
from selenium import selenium
import unittest, time, re
class python_script(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "Testing Website URL")
self.selenium.start()
def test_python_script_1(self):
sel = self.selenium
def test_python_script_2(self):
sel = self.selenium
sel.open("Testing website URL")
for i in range(60):
try:
if sel.is_element_present("//*[@name='username']"):
break
except: pass
#time.sleep(1)
else:
fields = {"test_id" : "Testuff API Test_id","status" : "failed"}
result = urllib.urlopen("API URL", urllib.urlencode(fields))
print result.read()
self.fail("time out")
sel.type("//*[@name='username']", "username")
for i in range(60):
try:
if sel.is_element_present("//*[@name='password']"): break
except: pass
#time.sleep(1)
else:
fields = {"test_id" : "testuff API test_id","status" : "failed"}
result = urllib.urlopen("testuff API url", urllib.urlencode(fields))
print result.read()
#self.fail("time out")
sel.type("//*[@name='password']", "password")
for i in range(60):
try:
if sel.is_element_present("//*[@id='submitButton']"): break
except: pass
#time.sleep(1)
else:
fields = {"test_id" : "testuff API test_id","status" : "failed"}
result = urllib.urlopen("API URL", urllib.urlencode(fields))
print result.read()
self.fail("time out")
sel.click("//*[@id='submitButton']")
#time.sleep(0.1)
for i in range(60):
try:
if sel.is_element_present("//*[@id='loadingDeck'][@selectedIndex='1']"):
fields = {"test_id" : "testuff API test_id","status" : "passed"}
result = urllib.urlopen("API URL", urllib.urlencode(fields))
print result.read()
break
except: pass
#time.sleep(1)
else:
self.fail("time out")
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
...不幸的是,浏览器窗口仍然关闭。还有其他建议吗?
谢谢。
让它部分工作。
删除了其中一项:
if __name__ == "__main__":
unittest.main()
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
...并删除了:
self.selenium.stop()
从剩余的“ if __name__
”语句和 python 日志加上浏览器窗口保持打开状态。这是朝着正确方向迈出的一步,但我需要在脚本运行完成后关闭日志窗口。
我猜下一步是创建另一个停止类并在 selenium.py 文件中稍微玩一下,看看我是否可以删除关闭浏览器的命令。
如果有人有任何其他建议,将不胜感激。