尝试将 Selenium IDE Python 脚本作为测试套件运行
我在运行 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 文件中稍微使用它,看看是否可以删除关闭浏览器的命令。
如果有人有任何其他建议,将不胜感激。
I have a couple of problems with running more than one Python test script exported by Selenium IDE Python Remote Control plugin formatter.
1) After a python script is completed the browser window automatically closes. I am running tests in Firefox, for my example.
2) Selenium can't export it's test suites in python. How can I replicate the test suite functionality in python?
The reason why I am putting in the time to run the test script in Python is because our Test case solution (Testuff) software allows API calls to update the adjacent test case that have ran through Selenium test case automation.
Here is an example of the code with the API calls.
Thanks.
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()
Thanks for the quick response. I tried jcfollower's recommendation with this code:
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()
...and unfortunately, the browser window still closed. Any other suggestions?
Thanks.
Got it to partially work.
Removed one of the:
if __name__ == "__main__":
unittest.main()
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
...and removed the:
self.selenium.stop()
from the remaining "if __name__
" statement and the python log plus the browser window remain open. Thats a step in the right direction but I need the log window to close after the script is done running.
Im guessing the next step is to create another stop class and play around with it in the selenium.py file a little and see if I can remove the command to close the browser.
If anyone has any other suggestions that would be greatly appreciative.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Firefox 每次都会重新启动的原因是因为 setUp 在每个单元测试函数被调用之前被调用(同样,tearDown 之后)。因此,单元测试只是为每个测试创建一个新的 selenium 浏览器实例。但这不一定是坏事,但重复使用相同的浏览器会话可能会更快。
要解决这个问题,您可以使用 setUpClass /tearDownClass 类方法,如下所示:
请注意,setUpClass 和tearDownClass 仅在 python 2.7 中引入!如果您使用的是旧版本的 python,您仍然可以使用它 - 但您必须安装一个名为
The reason firefox gets restarted every time is because setUp is getting called before each unit test function is called (and similarly, tearDown, after). So the unit test simply creates a new selenium browser instance for each test. It's not necessarily a bad thing though, but it might be faster to re-use the same browser session.
To get over this you can use the setUpClass / tearDownClass class methods instead, like so:
Please note that setUpClass and tearDownClass were introduced only in python 2.7 ! If you're using an older version of python, you can still use it - but you'd have to install a library called unittest2. After you install it, you can simply change the import line on top of the script to something like
如果删除第二组 import 语句、第二个 Class 语句和第二个 setUp 函数,然后将 test_python_script 函数重命名为在其末尾添加 _1 和 _2 ,它会起作用吗?
Will it work if you delete the second set of import statements, the second Class statement and the second setUp function, and then rename the test_python_script functions to have _1 and _2 on the ends of them.