2011-08-29 18 views
1

Selenium IDE Python Remote Controlプラグインフォーマッタでエクスポートされた複数のPythonテストスクリプトを実行することにはいくつか問題があります。Selenium IDE Pythonスクリプトをテストスイートとして実行しようとしています

1)pythonスクリプトが完了すると、ブラウザウィンドウが自動的に閉じます。私は私の例として、Firefoxでテストを実行しています。

2)SeleniumはPythonでテストスイートをエクスポートできません。 Pythonでテストスイートの機能をどのように複製できますか?

Pythonでテストスクリプトを実行するのは、私たちのテストケースソリューション(Testuff)ソフトウェアが、Seleniumテストケース自動化を実行した隣接テストケースをAPI呼び出しで更新できるためです。

ここでは、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() 

...残念ながら、ブラウザウィンドウはまだ閉じています。その他の提案はありますか?

ありがとうございました。


部分的に動作するようにしました。

削除の1:

if __name__ == "__main__": 
    unittest.main() 

    def tearDown(self): 
     self.selenium.stop() 
     self.assertEqual([], self.verificationErrors) 

...と削除:

 self.selenium.stop() 

残りの "if __name__" の文と、Pythonのログに加えて、ブラウザ・ウィンドウからは開いたまま。それは正しい方向へのステップですが、スクリプトが実行された後にログウィンドウを閉じる必要があります。

次のステップを推測すると、別の停止クラスを作成し、それをselenium.pyファイルで少し遊んで、ブラウザを閉じるコマンドを削除できるかどうかを確認することです。

誰かが他の提案をいただければ、大いに感謝します。

答えて

0

インポートステートメントの2番目のセット、2番目のクラスステートメント、2番目のsetUp関数を削除し、test_python_script関数の名前を_1と_2に変更すると機能しますか?

0

毎回firefoxが再起動されるのは、各ユニットテスト関数が呼び出される前にsetUpが呼び出されているからです(同様に、tearDown、after)。したがって、単体テストは各テストに対して新しいセレンブラウザインスタンスを作成するだけです。しかし、必ずしも悪いことではありませんが、同じブラウザセッションを再利用する方が早いかもしれません。

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

setUpClassとtearDownClassだけのpython 2.7で導入されたことに注意してください。そうのようなあなたは、代わりにsetUpClass/tearDownClassクラスのメソッドを使用することができ、この上で取得するには

! Pythonの古いバージョンを使用している場合でも、それを使用できますが、unittest2というライブラリをインストールする必要があります。あなたはそれをインストールした後、あなたは、単に

import unittest2 as unittest 
+0

のようなものねえヨアフにスクリプトの先頭にインポート行を変更することができ、私はあなたの提案を試してみましたが、Firefoxのウィンドウが何らかの理由で開けません。 –

+0

正確に言うのは難しいですが、2.7より古いPythonを実行しているか、unittest2をインストールしていない/正しくインポートできませんでした。ロード時にsetUpClassが実行されるようにしてください。例えば各関数にいくつかのprintステートメントを追加し、それらが実行されるかどうかを確認します。 – gingerlime

+0

こんにちはYoav、私はPython 2.7を使用しています。ありがとう。 –

関連する問題