2016-11-13 8 views
2

내 셀렌 웹 테스트 제품군에서 python gflags 모듈을 사용하는 데 문제가 있습니다. 나는 기본적으로 gflags github repo에서 어떻게 예제를 따르는가? DEFINE_string을 통해 정의한 새 플래그가 작동합니다. 그러나 기본 옵션 --help가 작동하지 않습니다.python gflags 모듈 도움말 플래그가 작동하지 않습니다.

다음
#!/usr/bin/env python 
import datetime 
import gflags 
import sys 
import time 
import os 
import glob 
import unittest 
from selenium import webdriver 
from pageobject.contact_us_page import ContactUsPage 
from pageobject.utility import SendEmailNotification 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 

FLAGS = gflags.FLAGS 
gflags.DEFINE_string('sender_addr', '', 'The Sender of email notification') 
gflags.DEFINE_list('receivers_addr', '', 'The list of receivers') 
gflags.DEFINE_string('sender_password', '', 'The password of sender email box') 


class TestContactUsPage(unittest.TestCase): 

    @classmethod 
    def setUpClass(cls): 
     cls.driver = webdriver.Chrome('../chromedriver') 
     cls.cu_page = ContactUsPage(cls.driver) 

    @classmethod 
    def tearDownClass(cls): 
     cls.driver.close() 

    def tally(self): 
     return len(self._resultForDoCleanups.errors) + len(self._resultForDoCleanups.failures) 

    def setUp(self): 
     self.errors_and_failures = self.tally() 

    def tearDown(self): 

     # if sys.exc_info()[0]: 
     #  message = 'Test fails with expection: %s' % sys.exc_info()[1] 
     #  subject = 'Test fails' 
     #  utilityLib.SendEmailResult('[email protected]', '[email protected]',) 
     if self.tally() > self.errors_and_failures: 
      now = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S') 
      self.cu_page.TakeScreenShot('error_' + now) 

    def testStep01(self): 
     self.cu_page.Open() 
     time.sleep(3) 
     title = self.driver.title 
     self.assertEqual(title, 'Parkside Lending LLC') 

    def testStep02(self): 
     self.cu_page.GotoPage() 
     title_element = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((
      By.XPATH, '//h1[contains(.,"Contact Us")]'))) 
     self.cu_page.TakeScreenShot('cupage') 
     self.assertEqual(title_element.text, 'Contact Us') 

    def testStep03(self): 
     self.cu_page.FillContactForm() 
     submit_button = self.driver.find_element_by_name('submit') 
     submit_button.click() 
     self.assertTrue(self.cu_page.SubmitVerification(), 'Contact Form submission Fails, Please check out the ' 
                  'output and screen shot.') 

if __name__ == '__main__': 
    FLAGS(sys.argv) 
    suite = unittest.TestLoader().loadTestsFromTestCase(TestContactUsPage) 
    testResult = unittest.TextTestRunner(verbosity=2).run(suite) 
    now = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S') 
    error_file = max((glob.glob('error_*.png')), key=os.path.getmtime) 
    sender = FLAGS.sender_addr 
    receipts = FLAGS.receivers_addr 
    password = FLAGS.sender_password 
    with open('test.out', 'w') as f: 

     if testResult.wasSuccessful(): 
      f.write('%s --- All %s test steps are passed, %s failures and %s errors' % (now, testResult.testsRun, 
        testResult.failures, testResult.errors)) 
     else: 
      f.write('%s --- Test steps are failed.\n' % now) 
      if len(testResult.failures): 
       for failure in testResult.failures: 
        f.write('Test is failing at: %s\n' % failure[0]) 
        f.write(failure[1]) 
      if len(testResult.errors): 
       for error in testResult.errors: 
        f.write('Test has error at: %s\n' % error[0]) 
        f.write(error[1]) 
    with open('test.out', 'r') as f: 
     message = f.read() 
     if testResult.wasSuccessful(): 
      subject = 'All Contact Us Page test steps are passed' 
     else: 
      subject = 'Contact Us Page Test steps are failed.' 
      SendEmailNotification().SendEmailResult(
      sender, receipts, message, subject, 
      ['cupage.png', error_file] 
      , password) 

내가 도움말 텍스트를 얻기 위해 시도하는 파이썬 contact_us_page_test.py의 --help를 사용하는 경우의 출력입니다 :

여기 내 코드입니다.

python contact_us_page_test.py --help 

Traceback (most recent call last): 
    File "contact_us_page_test.py", line 70, in <module> 
    FLAGS(sys.argv) 
    File "/usr/local/lib/python2.7/dist-packages/gflags/flagvalues.py", line 708, in __call__ 
    name, value, suggestions=suggestions) 
gflags.exceptions.UnrecognizedFlagError: Unknown command line flag 'help' 

답변

2

파이썬 GFLAGS는 --help 플래그를 제공하지 않습니다. 그러나 gflags.FLAGS 개체는 전체 도움말로 문자열을 바꿉니다. 이처럼 사용

try: 
    argv = FLAGS(sys.argv) 
except gflags.FlagsError as e: 
    print "%s\nUsage: %s ARGS\n%s" % (e, sys.argv[0], FLAGS) 
    sys.exit(1) 
0

python-gflagsabseil-py에 병합되었다.

abslabsl/app.py입니다. defines은 도움말 플래그입니다. 다음과 같이 프로그램을 구성 할 수 있습니다.

from absl import app 
from absl import flags 

FLAGS = flags.FLAGS 

def main(argv): 
    # Your main program starts here. 
    # FLAGS is already parsed. 
    pass 

if __name__ == '__main__': 
    # app.run defines help functions and parses flags, 
    # then calls `main` with remaining args. 
    app.run(main)