2012-11-26 4 views
0

저는 셀렌 자동화의 초보자입니다. 셀레늄 테스트 케이스 & 테스트 스위트를 만들었습니다. 테스트 스위트를 파이썬 웹 드라이버로 내보냈습니다.Selenium의 Python webdriver

이 파이썬 코드는 어떻게 실행해야합니까? 나는 노력이 :

Traceback (most recent call last): 
File "./pythondriver.py", line 52, in <module> 
unittest.main() 
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__ 
self.parseArgs(argv) 
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs 
self.createTests() 
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests 
self.module) 
File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames 
suites = [self.loadTestsFromName(name, module) for name in names] 
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName 
parent, obj = obj, getattr(obj, part) 
AttributeError: 'module' object has no attribute '<testcasename>' 

답변

6

파이썬 webdriver 같은 것은 없다 :

./pythonwebdriver <selenium test case.html>

나는이 오류가 발생합니다. Webdriver은 웹 페이지를 운전하기위한 구성 요소입니다. Selenium 2에 통합되었습니다. Java에서 기본적으로 작동하지만 Python을 비롯한 많은 언어에서 사용할 수있는 바인딩이 있습니다.

다음은 webdriver 설명서의 주석이 달린 예제입니다. unittest를 만들려면 unittest 모듈에서 제공하는 TestCase 클래스를 상속받는 테스트 클래스를 만듭니다. webdriver에 대한

#!/usr/bin/python 

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 
import unittest 

class GoogleTest(unittest.TestCase): 
    def test_basic_search(self): 
     # Create a new instance of the Firefox driver 
     driver = webdriver.Firefox() 
     driver.implicitly_wait(10) 

     # go to the google home page 
     driver.get("http://www.google.com") 

     # find the element that's name attribute is q (the google search box) 
     inputElement = driver.find_element_by_name("q") 

     # type in the search 
     inputElement.send_keys("Cheese!") 

     # submit the form (although google automatically searches 
     # now without submitting) 
     inputElement.submit() 

     # the page is ajaxy so the title is originally this: 
     original_title = driver.title 

     try: 
      # we have to wait for the page to refresh, the last thing 
      # that seems to be updated is the title 
      WebDriverWait(driver, 10).until(lambda driver : 
            driver.title != original_title) 
      self.assertIn("cheese!", driver.title.lower()) 

      # You should see "cheese! - Google Search" 
      print driver.title 
     finally: 
      driver.quit() 

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

하나 개 좋은 점은 당신이 테스트해야하는 브라우저 내용에 따라

driver = webdriver.Chrome() 
driver = webdriver.Firefox() 
driver = webdriver.Ie() 

로 드라이버 라인을 변경할 수 있다는 것입니다. ChromeDriver 외에도 FirefoxDriver 또는 InternetExplorerDriver 외에도 가장 가벼우 며 헤드리스 (브라우저와 달리 자바 스크립트가 다르게 작동 할 수도 있음) RemoteWebDriver이 원격 컴퓨터 및 병렬 테스트 및 기타 여러 가지 테스트를 실행할 수 있습니다 (iPhone, Android, 사파리, 오페라).

실행은 모든 파이썬 스크립트를 실행하는 것으로 수행 할 수 있습니다. 어느 단지와 : 위의 !#/usr/bin/python 같은 첫 번째 줄에 인터프리터 이름을 포함

python <script_name.py> 

나. 마지막 두 줄

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

이 파일을 직접 ./selenium_test.py처럼 실행될 때 스크립트가 테스트를 실행합니다. 여러 파일에서 자동으로 테스트 케이스를 수집하여 함께 실행할 수도 있습니다 (unittest 문서 참조). 일부 모듈 또는 일부 개별 테스트에서 테스트를 실행하는 또 다른 방법은 스크립트가 명령 줄 인수를 처리 unittest.main()를 호출

python -m unittest selenium_test.GoogleTest 
+0

내 보낸 파이썬 스크립트를 어떻게 실행할 수 있습니까? 내 질문에 언급 한대로 오류가 발생했습니다. – cppcoder

+0

죄송합니다. 러닝 파트를 잊어 버렸습니다. 이제 unittest 프레임 워크 설정 및 테스트 실행 방법을 포함하도록 답변을 업데이트했습니다. 오류를 제공하는 스크립트를 제공하지 않았으므로 정확히 무엇이 잘못 되었는가를 말하기는 어렵지만 희망적으로 도움이됩니다. – Edu

1

입니다 : <selenium test case.html>. unittest.main()<selenium test case.html>이 아니라 명령 모듈의 이름, 테스트 클래스 또는 테스트 메서드를 명령 줄 인수로 사용합니다.

관련 문제