2012-09-25 62 views
1

時,我有兩個文件:AttributeError的使用萵苣 '世界'

steps.py:

from lettuce import * 
from splinter.browser import Browser 

@before.harvest 
def set_browser(): 
    world.browser = Browser('webdriver.chrome') 
@step(u'Given I visit "([^"]*)"') 
def given_i_visit(step, url): 
    world.browser.visit(url) 

test.feature:

Feature: Do some basic tests 
    Scenario: Check whether the website is accessable 
    Given I visit "/" 

運行對他們的生菜返回此:

Feature: Do some basic tests      # features/test.feature:1 

    Scenario: Check whether the website is accessable # features/test.feature:2 
    Given I visit "/"        # features/steps.py:8 
    Traceback (most recent call last): 
    File "/..../site-packages/lettuce/core.py", line 125, in __call__ 
     ret = self.function(self.step, *args, **kw) 
    File "/..../test/features/steps.py", line 9, in given_i_visit 
     world.browser.visit(url) 
    AttributeError: 'thread._local' object has no attribute 'browser' 

1 feature (0 passed) 
1 scenario (0 passed) 
1 step (1 failed, 0 passed) 

任何想法可能是g錯了嗎?

回答

1

雖然沒有在文檔中。將terrain.py文件放在與步驟和特徵文件相同的目錄中。用任何值初始化世界屬性,你應該沒問題。

0

的問題是,before.harvest需要一些數據,所以正確的代碼如下所示:

from lettuce import * 
from splinter import Browser 

@before.harvest 
def set_browser(data): 
    world.browser = Browser('webdriver.chrome') 

@step(u'Given I visit "([^"]*)"') 
def given_i_visit(step, url): 
    world.browser.visit(url) 

希望它能幫助!