【问题标题】:How to get get page source in codeception yii 2如何在codeception yii 2中获取页面源
【发布时间】:2019-02-08 11:16:53
【问题描述】:

我需要保存失败测试的源代码来修复它。在yii 2中使用codeception测试时如何获取源html?

尽管有这些确切的功能,但我无法让 $I->grabPageSource()$I->_getResponseContent() 工作。

public function checkCall(FunctionalTester $I)
{
    $I->amOnRoute('mx/ed',['model' => 'State']);
    $I->seeResponseCodeIs(200);
    $I->seeResponseCodeIsSuccessful();
    $html = $I->grabPageSource();
}

【问题讨论】:

    标签: unit-testing yii2 yii2-advanced-app functional-testing codeception


    【解决方案1】:

    Codeception 将所有失败测试的最后请求的页面源自动保存在tests/_output 目录中,您无需执行任何操作。

    断言失败会引发异常,因此$I->seeResponseCodeIsSuccessful 之后的代码不会被执行。

    如果您想在特定测试中实现一些自定义错误处理,您可以将断言包装在 try-catch 块中,并将 grabPageSource 包装在 catch 中。

    public function checkCall(FunctionalTester $I)
    {
        $I->amOnRoute('mx/ed',['model' => 'State']);
        try{
            $I->seeResponseCodeIs(200);
            $I->seeResponseCodeIsSuccessful();
        } catch (Exception $e) {
            $html = $I->grabPageSource();
            //do your error handling here
            throw $e; //rethrow exception to make test fail
        }
    }
    

    如果您想为所有测试实现自定义错误处理,请将_failed method 添加到tests/_support/Helper 目录中的Helper\Functional 类。

    public function _failed(\Codeception\TestInterface $test, $fail)
    {
        $testName = $test->getMetadata()->getName();
        $pageSource = $this->getModule('Yii2')->getPageSource();
        //do your error handling here
    }
    

    【讨论】:

    • 这很好,但我仍然需要我单位中的 html。有什么办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多