1

我有两个文件:

步骤.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)

测试功能:

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)

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

4

2 回答 2

1

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

于 2013-01-25T19:00:31.083 回答
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)

希望能帮助到你!

于 2015-02-23T12:58:18.740 回答