使用生菜'世界'时的AttributeError

时间:2012-09-25 06:04:05

标签: lettuce

我有两个文件:

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)

关于可能出现什么问题的任何想法?

2 个答案:

答案 0 :(得分:1)

虽然不在文档中。将terrain.py文件放在与步骤和功能文件相同的目录中。用任何值初始化了world属性,你应该没问题。

答案 1 :(得分: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)
希望它有所帮助!