【问题标题】:How to check a Wordpress plugin is activated in a Codeception acceptance test如何在 Codeception 验收测试中检查 Wordpress 插件是否已激活
【发布时间】:2019-09-19 20:51:21
【问题描述】:

我正在学习如何在 Wordpress 中编写插件,我想测试插件在激活时的行为。我使用Codeception/Wp-Browser 作为 TDD 框架。我打算使用带有 Chrome 的 WpWebDriver 模块编写验收测试

问题是我编写的测试相互干扰。出于这个原因,我需要在每次测试之前重置插件的状态,但我不知道该怎么做。

在每次测试之前我需要:

  • 测试插件是否处于活动状态
  • 如果是,则将其停用
  • 如果不是,则什么也不做

我知道我可以在 Cest 类中使用 _before 方法。这是我的代码:

<?php
class activationCest
{
    public function _before(AcceptanceTester $I)
    {        
        // what to do here???
    }

    public function _after(AcceptanceTester $I)
    {
    }

    // tests
    public function activationCaseFailPHP(AcceptanceTester $I)
    {
        $I->wantTo('see an error message if the PHP version 
                    is not compatible with the plugin');

        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        $I->activatePlugin('testwidget');

        $I->see('Your PHP version is outdated. 
                 Testwidget requires a PHP version equal or superior
                 to 7.0. Contact your hosting provider about 
                 how to update PHP');
    }

    public function activationCaseFailWP(AcceptanceTester $I)
    {
        $I->wantTo('see an error message if the Wordpress version
                    is not compatible with the plugin');

        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        $I->activatePlugin('testwidget');

        $I->see('Your Wordpress version is outdated. 
                 Testwidget requires a Wordpress version equal 
                 or superior to 5.0. Contact your hosting provider
                 about how to update Wordpress');
    }

    public function activationCaseSuccess(AcceptanceTester $I)
    {
        $I->wantTo('see a success message if the PHP and Wordpress versions are 
                    compatible with the plugin');

        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        $I->activatePlugin('testwidget');

        $I->see('Selected plugins activated', '#message');
    }
}

天真,我试过这个

public function _before(AcceptanceTester $I)
    {        
       if (is_plugin_active('testwidget')) {
          $I->deactivatePlugin('testwidget');
       }
    }

当然,它没有用。程序抛出错误:[Error] Call to undefined function is_plugin_active(),因为 Wordpress 函数 is_plugin_active 不在作用域内。

Codeception 的文档说有conditional assertions——即canSeeElementcantSeeElement 之类的方法,用于测试元素是否在页面上,如果失败则不会停止测试。 Codeception 操作似乎也有类似的东西,如您所见here

我不知道这些步骤装饰器是否可以成为解决方案,因为我不清楚它们是如何工作的以及如何设置它们。

您认为解决此问题最简单的方法是什么?如果你处于我的位置,你会如何解决它?

【问题讨论】:

    标签: php wordpress codeception


    【解决方案1】:

    验收测试无法执行任何服务端代码,因此无法使用wordpress功能,只能使用WpWebDriver模块提供的辅助方法与网站交互。

    由于see 方法在看不到预期内容时会抛出异常,因此您可以捕获该异常并仅在不满足断言时执行另一个代码。

    public function _before(AcceptanceTester $I)
    {
        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        try {
            //it throws PHPUnit Assertion Failed exception if plugin is active.
            $I->seePluginDeactivated('my-plugin');
        } catch (\Exception $e) {
            $I->deactivatePlugin('hello-dolly');
        }
    }
    

    如果您想在许多测试文件中重用此代码,请将其设为帮助方法,如 https://codeception.com/docs/06-ModulesAndHelpers#Helpers 中所述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2016-11-30
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      相关资源
      最近更新 更多