Django与Lettuce和Splinter搭配,打开浏览器但不显示任何内容

1 投票
2 回答
1246 浏览
提问于 2025-04-17 02:16

我正在尝试用lettuce和splinter进行测试。我有一个配置好的django应用,没什么问题。当我运行lettuce,执行一个“应该”在浏览器中访问某个网址的步骤时,它没有返回任何错误,但页面就是不显示出来。

这是我的.feature文件:

Feature: A test 
Scenario: User enters email, ajax will check if it exists
    When I go to the "/home/login/" URL 
    Then I fill in "#id_email" with "abc@def.gih"

这是我的steps.py:

from lettuce import *
from lettuce.django import django_url
from lxml import html
from django.test.client import Client
from nose.tools import assert_equals
from splinter.browser import Browser
from django.test.utils import setup_test_environment, teardown_test_environment
from django.core.management import call_command
from django.db import connection
from django.conf import settings

@before.all
def set_browser():
    setup_test_environment()
    world.browser = Browser('firefox')

@step(u'I go to the "(.*)" URL')
def i_go_to_the_url(step, url):
    world.response = world.browser.visit(django_url(url))

@step(u'I fill in "(.*)" with "(.*)"')
def i_fill_in(step, field, value):
    world.browser.fill(field, value)

它打开了浏览器,但浏览器什么都不显示,测试却成功了,而第二个测试用例失败了,错误信息如下:

Feature: A test        # candidate/feature/index.feature:1

  Scenario: User enters email, ajax will check if it exists # candidate/feature/index.feature:3
    When I go to the "/home/login/" URL             # home/feature/index-steps.py:29
    Then I fill in "email" with "dsfdf@safsdfsd"       # home/feature/index-steps.py:39
    Traceback (most recent call last):
      File "/usr/local/lib/python2.6/dist-packages/lettuce/core.py", line 117, in __call__
        ret = self.function(self.step, *args, **kw)
      File "********/candidate/feature/index-steps.py", line 40, in i_fill_in
        world.browser.fill(field, value)
      File "/usr/local/lib/python2.6/dist-packages/splinter/driver/webdriver/__init__.py", line 240, in fill
        field.value = value
      File "/usr/local/lib/python2.6/dist-packages/splinter/driver/webdriver/__init__.py", line 306, in _set_value
        self._element.clear()
    AttributeError: 'NoneType' object has no attribute 'clear'

1 feature (0 passed)
1 scenario (0 passed)
2 steps (1 failed, 1 passed)
(finished within 15 seconds)

请问有人能帮我解决这个问题吗?

2 个回答

0

我也遇到过同样的问题。你可以写:

When I go to the "http://0.0.0.0:8000/home/login/" URL

而不是

When I go to the "/home/login/" URL

这样一来,一切就会正常了!

3

你没有解释很多,但可以试试下面的方法

    world.browser.find_by_id(field).fill(value)

撰写回答