开发者

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.

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.


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:

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 ! 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 unittest2 as unittest


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜