I've been trying to develop an automated test case solution using Selenium RC and Python and after lengthy testing I've hit a pretty hard block in the road, so to speak.
I have three files: unit.py, case1.py, and case1m.py
unit.py configures instances of case1m.py with a browser and a port, then runs the test by sending the case1m instance through unittest.main().
The case1.py file is a vanilla case that is generated from Selenium IDE; when run from the command line, it executes the test case and exits with OK. I used this file to help debug the failing points of the other two files.
Here is the source for all three files:
unit.py:import unittest
from case1m import case1m
browser = "*chrome"
port = 4444
a = case1m()
a.setBrowser("*chrome",4444)
unittest.main(a)
case1m.py - handles browser/port arguments and runs selenium test cases:
from selenium import selenium
import unittest, time, re
class case1m(unittest.TestCase):
def setBrowser(self,b,p):
print "entered setBrowser"
self.browser = b
self.port = p
print "leaving setBrowser"
self.setUp()
def setUp(self):
print self.browser,", ",self.port
self.verificationErrors = []
self.selenium = selenium("localhost", self.browser, self.port, "http://megagate-ffcdcb.xl_net.internal/")
self.selenium.start()
print "end setUp"
self.runTest()
def runTest(self):
print "entered runTest"
sel = self.selenium
sel.open("/seltest/")
try: self.failUnless(sel.is_text_present("BODY"))
except AssertionError, e: self.verificationErrors.append(str(e))
print "leaving runTest"
self.tearDown()
def tearDown(self):
print "entered tearDown"
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
print "leaving tearDown"
case1.py:
from selenium import selenium
import unittest, time, re
class case1(unittest.TestCase):
def setUp(self):
print "entered setUp"
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://megagate-ffcdcb.xl_net.internal/")
self.selenium.start()
def runTest(self):
sel = self.selenium
sel.open("/seltest/")
try: self.failUnless(sel.is_text_present("BODY"))
except AssertionError, e: self.verificationErrors.append(str(e))
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == '__main__':
unittest.main()
The first problem I ran into was passing the browser and port values to an instance of the case1m class. I tried using __init__
to collect them as arguments, but apparently sub-classing the TestCase class and then adding an __init__
override causes problems; the setUp(), runTest() and tearDown() methods no longer triggered automatically as they do in the case1 class.
So instead, I overrode and inserted a setBrowser() method to collect the values and create the browser and port variables within the class instance. This again causes the same issue as before, so I resorted to inserting method calls into setUp(), runTest() and tearDown(). When executed, it runs until it tries the do_command() method in the selenium instance.
Here is the error:
Traceback (most recent call last):
File "C:\sel-test\unit.py", line 13, in a.setBrowser("*chrome",4444) File "C:\sel-test\case1m.py", line 10, in setBrowser 开发者_如何学编程 self.setUp() File "C:\sel-test\case1m.py", line 16, in setUp self.selenium.start() File "C:\Python26\lib\selenium.py", line 190, in start result = self.get_string("getNewBrowserSession", [self.browserStartCommand, self.browserURL, self.extensionJs]) File "C:\Python26\lib\selenium.py", line 225, in get_string result = self.do_command(verb, args) File "C:\Python26\lib\selenium.py", line 213, in do_command conn.request("POST", "/selenium-server/driver/", body, headers) File "C:\Python26\lib\httplib.py", line 910, in request self._send_request(method, url, body, headers) File "C:\Python26\lib\httplib.py", line 947, in _send_request self.endheaders() File "C:\Python26\lib\httplib.py", line 904, in endheaders self._send_output() File "C:\Python26\lib\httplib.py", line 776, in _send_output self.send(msg) File "C:\Python26\lib\httplib.py", line 735, in send self.connect() File "C:\Python26\lib\httplib.py", line 716, in connect self.timeout) File "C:\Python26\lib\socket.py", line 500, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): socket.gaierror: [Errno 10109] getaddrinfo failed
My questions is: why does the unit.py/case1m.py combination result in socket.gaierror when the case1.py file will run without error? From what I can see, the selenium class should be receiving the exact same information by the time it reaches self.do_command(). The only difference is that case1.py is being run directly from the commandline, while case1m.py is being run as an imported module.
Looking at the 2 code snippets side by side, I think you have inverted the browser and port arguments. This is probably the source of your error.
case1.py (runs fine):
self.selenium = selenium("localhost", 4444, "*chrome", "http://megagate-ffcdcb.xl_net.internal/")
case1m.py (socket error):
self.selenium = selenium("localhost", self.browser, self.port, "http://megagate-ffcdcb.xl_net.internal/")
精彩评论