2016-04-05 15 views
1

私は、セレニウムとPythonを使用して角ウェブアプリケーションをテストしています。私のテストでは、API呼び出しを使っていくつかのデータを設定しています。今私はテストに進む前にデータがフロントエンドに現れるまで待っています。私たちは現在、この問題を克服するために60秒待っています。しかし、私は賢く待機を期待し、次のコードを書いた:私のテストスクリプトでWebDriverWait.untilのカスタムサブルーチンがエラーをスローする

def wait_for_plan_to_appear(self,driver,plan_locator): 

    plan_name_element = UNDEF 

    try: 
     self.navigateToPlanPage() 
     plan_name_element = driver.find_element_by_xpath(plan_locator) 
    except NoSuchElementException: 
     pass 

    return plan_name_element 

def find_plan_name_element(self,plan_id): 

    plan_locator = '//*[@data-hraf-id="'+plan_id+'-plan-name"]' 
    plan_name_element = UNDEF 
    try: 
     plan_name_element = WebDriverWait(self.driver,60,2).until(self.wait_for_plan_to_appear(self.driver,plan_locator)) 
    except TimeoutException: 
     self.logger.debug("Could not find the plan with plan_id = "+plan_id) 
    return plan_name_element 

を、私が呼び出しています:

self.find_plan_name_element('e7fa25a5-0b39-4a97-b99f-44c48439ce99') # the long string is the plan-id 

をしかし、私はこのコードを実行すると - 私は次のエラーを取得します:

error: 'int' object is not callable" 

私はそれがboolean型を返すようにwait_for_plan_to_appearを変更した場合、それはエラーがスローされます。

error: 'bool' object is not callable" 

誰かがこれを自分の仕事で見た/解決しましたか?ありがとう

答えて

1

私は"...".format()を使用して自動的にplan_idを文字列に変換します。 さらに、予想される条件を使用してウェイターを簡略化することができます。

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException 

class element_loaded_and_displayed(object): 
    """ An expectation for checking that an element is present on the DOM of a 
    page and visible. Refreshes the page if the element is not present. 
    returns the WebElement once it is located and visible. 
    """ 
    def __init__(self, locator): 
     self.locator = locator 

    def __call__(self, driver): 
     try: 
      element = driver.find_element(*self.locator) 
      return element if element.is_displayed() else False 
     except StaleElementReferenceException: 
      return False 
     except NoSuchElementException as ex: 
      driver.refresh() 
      raise ex 

def find_plan_name_element(self, plan_id): 
    plan_locator = (By.CSS_SELECTOR, "[data-hraf-id='{0}-plan-name']".format(plan_id)) 
    err_message = "Could not find the plan with plan_id = {0}".format(plan_id) 

    wait = WebDriverWait(self.driver, timeout=60, poll_frequency=2) 
    return wait.until(element_loaded_and_displayed(plan_locator), err_message) 
+0

ありがとうフロラン。しかし、(.format()の使用について)提案した変更を行いましたが、私はまだ同じエラーが発生しています。 EC.visibility_of_element_locatedを使用することはできません。なぜなら、要素がリストに表示されるかどうかを確認する前に、毎回ページを更新する必要があるからです。 ECを使用して要素の可視性をチェックする前に、ページの更新を強制する方法はありますか? ECを使用すると、このエラーは消えます。しかし、それは私の問題を解決しません。なぜなら、ページが最初にナビゲートされ、いくつかのページリフレッシュが必要なときに、要素が初めて利用できないことがあるからです。 –

関連する問題