metadrop/behat-contexts

我们在Drupal网站上使用Behat 3.x测试的上下文。


README

我们在Drupal网站上使用Behat 3.x测试的上下文。

此仓库基于Nuvole drupal扩展

安装

使用Composer安装

composer require metadrop/behat-contexts

配置

每个上下文可能都有自己的配置。这里有一个示例,其中添加了所有上下文。

上下文

缓存上下文

清除缓存的步骤。

步骤

  • Given :path 页面缓存已刷新 清除特定页面缓存。

  • Given :view 视图数据缓存已刷新 清除特定视图的缓存。仅适用于Drupal 7,待在D8中实现。

内容作者上下文

允许创建由登录用户拥有的内容。

步骤

  • Given own :type 内容:创建以当前用户为作者的 内容。

定时任务上下文

执行定时任务的辅助工具。

步骤

  • Given I run elysia cron 运行elysia定时任务。仅适用于D7。

  • Given I run the elysia cron :job job 运行elysia-cron-job。仅适用于D7。

  • Given I run the cron of Search API 运行search api定时任务。仅适用于D7。

  • Given I run the cron of Search API Solr 运行search api solr定时任务。仅适用于D7。

调试上下文

简单的上下文,通过一些步骤帮助调试测试。此外,它通过挂钩在步骤后的事件来添加一个步骤,在失败的步骤上生成错误报告。

此报告包括

  • 包含HTML页面内容的文件。
  • 包含当前URL和错误异常转储的文件。
  • 如果可用,包含当前页面状态的文件。

Cookie合规性上下文

允许检查网站是否遵守cookie GDPR法规。此上下文与任何cookie横幅集成一起工作:onetrust、cookies、eu cookie compliance...

它检查在接受cookie之前浏览器中是否没有保存cookie,并且当接受cookie时,预期的cookie出现。

上下文参数包括

  • cookie_agree_selector:同意cookie的按钮的CSS选择器。
  • cookie_banner_selector:cookie合规性横幅的CSS选择器。
  • cookies:每个cookie类别的映射。键是cookie类别,值是接受cookie合规性类别后将存在的cookie列表。
  • cookies_ignored:必须在步骤'There are no cookies loaded'中忽略的cookie列表。在此处添加无法在服务器端管理的cookie。
  • cookies_third_party_domains_ignored:报告包含可能加载的cookie但可以忽略的域的列表,因为未加载任何cookie。
  • cookies_third_party_domains_included:不在上下文默认检查域列表中的域列表,需要检查这些网站是否不是通过iframe加载cookie。

示例配置

  - CookieComplianceContext:
      cookie_agree_selector: '.eu-cookie-compliance-banner button.agree-button'
      cookie_banner_selector: '.eu-cookie-compliance-banner'
      cookies:
        mandatory:
          - 'cookie-agreed'
          - 'cookie-agreed-categories'
        analytics:
          - '_ga'
      # Optional configuration:
      cookies_ignored:
        - cookieA
        - cookieB
      cookies_third_party_domains_ignored:
        - example.com
      cookies_third_party_domains_included:
        - extra-analytics-service.com

包含在此上下文中的步骤

  • Then I accept cookies:使用默认选项接受cookie横幅。

  • Then the cookies of :type type have not been loaded:断言特定类别的cookie不存在。

  • Then the cookies of :type type have been loaded:断言特定类别的cookie存在。

  • 当等待cookie横幅出现时:等待cookie横幅加载完成。

  • 此时不应加载任何cookie:检查是否完全没有加载cookie。它还会报告来自第三方iframe(例如:youtube、doubleclick等)的潜在cookie来源。

    默认情况下,只有属于CookieComplianceContext常量THIRD_PARTY_COOKIE_HOSTS域的iframe将被检测为会添加不想要的cookie的iframe。

步骤

  • 然后捕获宽度为:width的整个页面

    将当前页面的屏幕截图以给定宽度保存到文件。

  • 然后捕获宽度为:width的整个页面,以配置目录(screenshots_path)中的:filename命名

    将当前页面的屏幕截图以给定宽度保存到配置目录(screenshots_path)中的给定文件名。

  • 然后捕获宽度为:width的整个页面到:path

    将当前页面的屏幕截图以给定宽度保存到给定路径的文件。如果路径是相对的,则使用screenshots_path配置值作为根。

  • 然后捕获宽度为:width的整个页面到:path,以:filename命名

    将当前页面的屏幕截图以给定宽度保存到给定路径的文件,以给定文件名命名。如果路径是相对的,则使用screenshots_path配置值作为根。

  • 然后保存最后的响应

    将页面内容保存到文件。

  • 然后将最后的响应保存到:path

    将页面内容保存到给定路径的文件。

  • 然后将最后的响应保存到:path

    暂停测试给定秒数。在调试有计时问题的测试时很有用。在真实测试中不要使用此步骤。

配置

将DebugContext添加到您的套件中。

这是一个当引导目录位于DRUPALROOT/sites/all/tests/behat/bootstrap时的示例。

default:
  autoload:
    ...
  suites:
    default:
      ...
      contexts:
        - Metadrop\Behat\Context\DebugContext:
            parameters:
              'report_on_error': true
              'error_reporting_url': 'https://example.com/sites/default/files/behat/errors'
              'error_reporting_path': '/var/www/html/docroot/sites/default/files/behat/errors'
              'screenshots_path': '/var/www/html/docroot/sites/default/files/behat/screenshots'
              'page_contents_path': '/var/www/html/docroot/sites/default/files/behat/pages'
        - Metadrop\Behat\Context\EntityContext:
            parameters:
              'purge_entities':
                - user
                - custom_entity

参数

  • report_on_error: 如果true,在失败的步骤上生成错误报告。
  • error_reporting_path: 报告保存的路径。
  • error_reporting_url: 错误屏幕截图将显示的URL。如示例所示,URL必须指向我们保存报告的目录,并且目录必须可以通过网站访问。
  • screenshots_path: 保存屏幕截图的路径。报告屏幕截图保存在报告路径中,这里只保存来自捕获整个页面步骤的屏幕截图。
  • page_contents_path: 保存页面内容的路径。报告页面内容保存在报告路径中,这里只保存来自保存页面内容步骤的页面内容。

实体上下文

与实体相关的无差别步骤。

步骤

  • Given I go to the last entity :entity created Go to last entity created.

  • Given I go to the last entity :entity with :bundle bundle created Go to the last entity created from a specific bundle.

  • Given I go to :subpath of the last entity :entity created Go to last entity created subpath (s.e.:node/1/edit).

  • Given I go to :subpath of the last entity :entity with :bundle bundle created. Go to last entity created subpath (s.e.:node/1/edit) from a specific bundle.

文件上下文

在drupal中创建文件。

步骤

  • Given file with name :filename Create file in drupal file system. Files are extracted from files_path set in behat.

  • Given file with name :filename in the :directory directory Create file in drupal file system in a specific directory. Directory must start with file system (public:// , private://). Default is public:// .

表单上下文

与表单元素相关的步骤。

步骤

  • Then form :type element :label should be required Check a form element of a specific type (s.e.: input, select) with label is required.

  • Then form :type element :label should not be required Check a form element of a specific type (s.e.: input, select) with label isn't required.

节点访问上下文

与节点访问系统相关的步骤。仅适用于D7。

步骤

  • 给定最后创建的节点的访问权限已刷新,刷新从最后节点授予的节点权限。

  • 给定使用 :bundle 包创建的最后节点的访问权限已刷新,刷新特定内容类型的最后节点的节点权限。

段落上下文

将段落附加到内容的步骤。

步骤

  • 给定引用于最后内容 :field_paragraph 字段的 :paragraph_type 类型段落:创建一个带有字段的段落并将其附加到最后创建的节点。

URL上下文

检查URL值的步骤

步骤

  • 然后当前URL应该具有":param"参数,其值为":value",检查URL查询参数中具有特定值。

  • 然后当前URL不应具有":param"参数,其值为":value",检查URL查询参数中不具有特定值。

UI上下文

此上下文提供某些UI元素的步骤。

步骤

  • 给定我选择 :option 从 :select chosen.js 选择框

    从Chosen选择小部件中选择一个选项。仅适用于单选,在启用多选或标签样式时不起作用。

    请参阅 https://harvesthq.github.io/chosen/

elementShouldBeInPosition内部方法的使用示例

/**
* Example of implementation elementShouldBeInPosition on a custom step.
*
* @Then the card on the infinite scroll view with title :title should be in position :position.
*/
public function theCardWithTitleShouldBeInPositionExample(string $title, string $position) {
  $this->elementShouldBeInPosition('item-list-css-selector', $title, 'views-infinite-scroll-content-wrapper', $position);
}

等待上下文

此上下文提供在定义的步骤之后的等待时间和额外的等待步骤。

等待步骤 - 有时步骤的运行速度比我们的网站快,如果是这种情况,您可以延迟几秒钟。不要滥用此功能,如果Behat运行缓慢,可能存在需要首先解决的性能全局网站问题!

步骤

- Then I wait for :seconds second(s)
  Step wait a defined seconds before execute next step.

在执行下一个步骤之前配置一些等待时间

behat.yml 步骤操作中设置等待时间(以秒为单位),在执行下一个步骤之前。

配置
default:
  autoload:
    ...
  suites:
    default:
      ...
      contexts:
        - Metadrop\Behat\Context\WaitingContext:
            parameters:
              waiting_steps:
                'I go to': 1
                'I click': 1
                'I scroll': 1
                'I press': 2
操作

Then I press "Log in"Then the url should match "/example-page")之前等待1秒钟。

Then I press "Log in"
Then the url should match "/example-page"

配置

无需配置。