2010-06-24 77 views
0

我一直在嘗試開發一個使用Selenium RC和Python的自動化測試用例解決方案,經過漫長的測試後,我在路上遇到了一個相當困難的問題,可以這麼說。Selenium RC利用Python生成多個瀏覽器

我有三個文件:unit.py,case1.py和case1m.py

unit.py配置有瀏覽器和端口case1m.py的實例,然後通過發送case1m實例運行測試通過unittest.main()。

case1.py文件是從Selenium IDE生成的一個香草案例;當從命令行運行時,它會執行測試用例並按OK退出。我用這個文件來幫助調試其他兩個文件的失敗點。

這裏是所有三個文件的源:


unit.py:

import unittest 
from case1m import case1m 

browser = "*chrome" 
port = 4444 

a = case1m() 
a.setBrowser("*chrome",4444) 
unittest.main(a) 


case1m.py - 處理瀏覽器/端口的參數和運行硒測試案例:

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() 

我遇到的第一個問題是將瀏覽器和端口值傳遞給case1m類的實例。我嘗試使用__init__來收集它們作爲參數,但顯然子類TestCase類,然後添加一個__init__覆蓋導致問題; setUp(),runTest()和tearDown()方法不再像case1類中那樣自動觸發。

因此,我重寫並插入了一個setBrowser()方法來收集值並在類實例中創建瀏覽器和端口變量。這再次導致與之前相同的問題,所以我使用setUp(),runTest()和tearDown()方法調用。在執行時,它會運行,直到它嘗試selenium實例中的do_command()方法。

以下是錯誤:

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

我的問題是:爲什麼unit.py/case1m.py組合結果socket.gaierror當case1.py文件將運行沒有錯誤?從我所看到的情況來看,selenium類在到達self.do_command()時應該會收到完全相同的信息。唯一的區別是case1.py直接從命令行運行,而case1m.py作爲導入的模塊運行。

回答

1

並排觀察2個代碼片斷,我認爲你已經顛倒了瀏覽器和端口參數。這可能是你錯誤的根源。

case1.py(運行正常):

self.selenium = selenium("localhost", 4444, "*chrome", "http://megagate-ffcdcb.xl_net.internal/") 

case1m.py(套接字錯誤):

self.selenium = selenium("localhost", self.browser, self.port, "http://megagate-ffcdcb.xl_net.internal/") 
+1

*捂臉*捂臉*捂臉* – 2010-06-25 14:37:28

相關問題