简体   繁体   English

尝试将Selenium IDE Python脚本作为测试套件运行

[英]Attempting to run Selenium IDE Python scripts as test suite

I have a couple of problems with running more than one Python test script exported by Selenium IDE Python Remote Control plugin formatter. 我运行多个由Selenium IDE Python远程控制插件格式化程序导出的Python测试脚本时遇到了两个问题。

1) After a python script is completed the browser window automatically closes. 1)python脚本完成后,浏览器窗口将自动关闭。 I am running tests in Firefox, for my example. 例如,我正在Firefox中运行测试。

2) Selenium can't export it's test suites in python. 2)Selenium无法使用python导出它的测试套件。 How can I replicate the test suite functionality in python? 如何在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. 我之所以花时间在Python中运行测试脚本,是因为我们的测试用例解决方案(Testuff)软件允许API调用更新已通过Selenium测试用例自动化运行的相邻测试用例。

Here is an example of the code with the API calls. 这是带有API调用的代码示例。

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: 我用以下代码尝试了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()

...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. 从其余的“ if __name__ ”语句和python日志以及浏览器窗口保持打开状态。 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. 我想下一步是创建另一个停止类,并在selenium.py文件中稍作处理,看看是否可以删除命令以关闭浏览器。

If anyone has any other suggestions that would be greatly appreciative. 如果有人有其他建议,将不胜感激。

如果删除第二组导入语句,第二个Class语句和第二个setUp函数,然后重命名test_python_script函数以在其末尾具有_1和_2,它是否可以工作?

The reason firefox gets restarted every time is because setUp is getting called before each unit test function is called (and similarly, tearDown, after). 每次都重新启动firefox的原因是,在调用每个单元测试功能之前(也类似,在downdown之后)调用setUp。 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: 为了克服这个问题,您可以改用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()

Please note that setUpClass and tearDownClass were introduced only in python 2.7 ! 请注意,setUpClass和tearDownClass仅在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 . 如果您使用的是旧版本的python,您仍然可以使用它-但您必须安装一个名为unittest2的库。 After you install it, you can simply change the import line on top of the script to something like 安装后,您只需将脚本顶部的导入行更改为类似

import unittest2 as unittest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM