【问题标题】:Behat pressButton not working in laravel 5Behat pressButton 在 laravel 5 中不起作用
【发布时间】:2015-01-24 06:09:01
【问题描述】:

https://laracasts.com/lessons/laravel-5-and-behat-driving-authentication

这是我正在测试的 Jeoffery Way 的教程

这是我要测试的场景

Scenario: Adding a new school
When I add a new school with "Narendra School" "This is a cool school"
Then I should see "Add School"
And a school is created

这是我的功能上下文文件,

/**
* @When I add a new school with :arg1 :arg2
*/
public function iAddANewSchoolWith($name , $description )
{
    $this->visit('school/create');
    $this->fillField( 'name', $name );
    $this->fillField( 'description', $description );
    $this->pressButton('register');
}

/**
* @Then a school is created
*/
public function aSchoolIsCreated()
{
   $school = School::all();
   PHPUnit::assertEquals($school->isEmpty(), false );
}

这是我的html代码

<form method="post">
<h2>Add School</h2>

    <input type="hidden" name="_token" value="{!! Session::getToken() !!}"/>

    <input type="text" name="name" placeholder="Name">
    <textarea name="description" placeholder="Description"></textarea>
    <button name="register">Register</button>
</form>

这是我用来创建对象的控制器方法

function create( ) {

   if ( \Request::isMethod( 'post' ) ) {

     $this->school->add( \Input::all() );// I am using a repository and dependency injection so this code is fine, it will add a school
   }

   return view('school::school.add');
}

当我点击 url、填写表格并在浏览器中点击注册时,我保存了一所新学校,但是当我运行测试时,我似乎无法创建学校。 测试正在运行我可以在执行printLastResponse()时看到html

Feature: Testing
    In order to teach Behat
    As a teacher
    I want to add a new school

  Scenario: Adding a new school                                            # features\example.feature:6
    When I add a new school with "Narendra School" "This is a cool school" # FeatureContext::iAddANewSchoolWith()
    Then I should see "Add School"                                         # FeatureContext::assertPageContainsText()
    And a school is created                                                # FeatureContext::aSchoolIsCreated()
      Failed asserting that false matches expected true.

--- Failed scenarios:

    features\example.feature:6

1 scenario (1 failed)
3 steps (2 passed, 1 failed)
0m3.10s (26.24Mb)

这是我的终端日志 我做错了什么?

【问题讨论】:

    标签: php testing behat laravel-5


    【解决方案1】:

    你可以试试替换:

    $this->pressButton('register');
    

    类似的东西:

    $page = $this->getSession()->getPage();
    $buttonElement = $page->find('css',".//button[@name='register']");
    $buttonElement->click();
    

    当您使用 Selenium Driver 时,有时您可以在 Selenium Server 日志中找到线索,或者至少在出现问题时看到异常。

    这会更好;-)

    PHPUnit::assertNotEmpty($school->isEmpty());
    

    【讨论】:

    • 试过了,但没有帮助,我开始认为这可能是我正在使用的一个 laravel 模块的问题,因为我尝试了全新安装 laravel,它就像一个魅力,我的项目中有很多组件,例如存储库模式、HMVC。我开始认为这可能是问题所在。
    • 我已将问题缩小到我将表单提交到相同的 url 并检查该方法是否是 post 并且它是一个 post 方法,我保存数据,这不起作用.当我使用不同的 url 来存储数据时,它正在工作。
    • 我不确定它是否有帮助,但我确实注意到,当我提交表单时,点击链接,它有助于检查页面上的某些内容(文本、元素)。如果没有检查,有时测试会随机失败,因为 Behat 正在执行步骤并且页面尚未完全加载。
    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2016-05-17
    • 2015-05-07
    • 2016-01-30
    • 1970-01-01
    • 2015-11-07
    相关资源
    最近更新 更多