运行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文件中稍微使用它,看看我是否可以删除关闭浏览器的命令。
如果有人有任何其他建议会非常感激。
答案 0 :(得分:0)
如果删除第二组import语句,第二个Class语句和第二个setUp函数,它是否有效,然后将test_python_script函数重命名为在它们的末尾有_1和_2。
答案 1 :(得分:0)
每次重启firefox的原因是因为在调用每个单元测试函数之前调用setUp(同样地,在后面的tearDown中)。因此,单元测试只是为每个测试创建一个新的selenium浏览器实例。但这并不一定是坏事,但重新使用相同的浏览器会话可能会更快。
要克服这个问题,你可以使用setUpClass / tearDownClass类方法,如下所示:
class python_script(unittest.TestCase):
@classmethod
def setUpClass(cls)
cls.selenium = selenium("localhost", 4444, "*chrome", "http://test website url/")
cls.selenium.start()
def setUp(self):
self.verificationErrors = []
def test_python_script_1(self):
...
def test_python_script_2(self):
...
def tearDown(self):
self.assertEqual([], self.verificationErrors)
@classmethod
def tearDownClass(cls):
cls.selenium.stop()
请注意,setUpClass和tearDownClass仅在python 2.7中引入!如果你使用的是旧版本的python,你仍然可以使用它 - 但是你必须安装一个名为unittest2的库。安装后,您只需将脚本顶部的导入行更改为
import unittest2 as unittest