2016-09-05 35 views
0

サンプルのBDD Python Behaveコードがあります。私はホームページtest.feature振舞う実行すると開きますが、私は次のエラーを取得する:Python Behave 'Context'オブジェクトには属性 'find_element'がありません

'Context' object has no attribute 'find_element' 

フル・エラーは、次のとおりです。

Scenario Outline: visit test and search for product -- @1.1 By product # test.feature:27 
Given we are on the test homepage          # steps\steps.py:37 
When we enter "<product>" in the search field       # steps\steps.py:43 
    Traceback (most recent call last): 
    File "C:\Python27\lib\site-packageehave\model.py", line 1456, in run 
     match.run(runner.context) 
    File "C:\Python27\lib\site-packageehave\model.py", line 1903, in run 
     self.func(context, *args, **kwargs) 
    File "steps\steps.py", line 50, in step 
     search_field = context.find_element(By.XPATH, 'id("twotabsearchtextbox")') 
    unner.py", line 214, in __getattr__eehave 
     raise AttributeError(msg) 
    AttributeError: 'Context' object has no attribute 'find_element' 

私のコードスニペットは、次のとおりです。

test.feature:

Feature: testing test 

Scenario Outline: visit test and search for product 
    Given we are on the test homepage 
    When we enter "<product>" in the search field 
    And we click the search button 
    Then the list of products are displayed 

    Examples: By product 
     | Forumla One | 
     | PS4   | 
     | Headphones | 

steps.py

from behave import given, when, then 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 

@given ('we are on the test homepage') 
def step(context): 
    context.browser.visit() 


@when ('we enter "{product}" in the search field') 
def step(context, product): 
    search_field = context.find_element(By.XPATH, 'id("twotabsearchtextbox")') 
    search_field.send_keys(product) 

@when ('we click the search button') 
def step(context): 
    pass 

@then ('the list of products are displayed') 
def step(context): 
    pass 

browser.py

from selenium import webdriver 

class Browser(object): 

    base_url = 'http://www.test.com' 
    driver = webdriver.Chrome("E:\Selenium Server\chromedriver.exe") 
    driver.implicitly_wait(10) 

    def close(self): 
     """ 
     close the webdriver instance 
     """ 
     self.driver.quit() 

    def visit(self, location=''): 
     """ 
     navigate webdriver to different pages 
     """ 
     url = self.base_url + location 
     self.driver.get(url) 

    def find_by_id(self, selector): 
     """ 
     find a page element in the DOM 
     """ 
     return self.driver.find_element_by_id(selector) 

environment.py

from browser import Browser 
from selenium import webdriver 

def before_all(context): 
    context.browser = Browser() 

def after_all(context): 
    context.browser.close() 

私が、私は、Webページ上の要素を見つけることができ、これを使用したい、find_elementを見つけていません。 Behaveでfine_elementを使用する正しい構文は何ですか?

おかげで、リアズ

答えて

2

あなたがcontext.browser代わりのcontextを使用すべきではありません:

search_field = context.browser.find_element(By.XPATH, 'id("twotabsearchtextbox")') 

find_element()方法はBrowserオブジェクト上で公開されていない場合、または、内部ドライバを取得:

search_field = context.browser.driver.find_element(By.XPATH, 'id("twotabsearchtextbox")') 

質問に記載されているfind_by_id()メソッドを使用することもできます。

search_field = context.browser.find_by_id("twotabsearchtextbox") 
+0

:すべての関数でコンテキストが渡されるのはなぜですか?セレンのために必要ですか?コンテキストには何が含まれていますか? –

関連する問題