跳转到主要内容

E2E 测试

为主题编写自动化测试是主题开发过程中的重要环节,有助于确保主题所引入的功能随时间推移仍能与 Discourse 核心功能正常协同工作。

目前,Discourse 支持两种为主题编写回归测试的方式。第一种主要遵循 EmberJS 的方式,仅涉及测试客户端代码。第二种是编写 Rails 系统测试,可以同时测试服务端代码和客户端代码。本文将重点介绍如何为主题编写 Rails 系统测试,这也是我们推荐主题作者在为主题编写测试时采用的方式。

主题的 Rails 系统测试

在底层,Discourse 使用 RSpec 和 Capybara 测试框架来运行 Rails 系统测试。入门前需要具备 RSpec 和 Capybara 的基本知识,我们建议在开始之前先通读以下链接:

  • https://github.com/rspec/rspec-core#basic-structure
  • https://github.com/teamcapybara/capybara#the-dsl

编写主题系统测试的准则与提示

编写系统测试时需遵循以下准则:

  • 系统测试应位于主题目录下的 spec/system 目录中。

  • spec/system 目录中的每个文件都应遵循 <description_of_system_test>_spec.rb 格式。

  • 顶层的 RSpec describe 代码块需要包含 system: true 元数据。示例:

    RSpec.describe "Testing A Theme or Theme Component", system: true do
      it "displays the component" do
        ...
      end
    end
    
  • upload_theme 和 upload_theme_component 辅助方法可用,且需要在测试运行前调用。示例:

    RSpec.describe "Testing A Theme or Theme Component", system: true do
      let!(:theme) do
        upload_theme
      end
    
      # or `upload_theme_component` if your theme is a component
      #
      # let!(:theme_component) do
      #   upload_theme_component
      # end
    
      it "displays the component" do
        ...
      end
    end
    
  • 在系统测试中,可以通过调用 theme 对象上的 update_setting 方法来更改主题的设置,然后保存主题。

    示例:

    RSpec.describe "Testing A Theme", system: true do
      let!(:theme) do
        upload_theme
      end
    
      it "does not display the theme when `should_render` theme setting is false" do
        theme.update_setting(:should_render, false)
        theme.save!
    
        expect(page).not_to have_css("#some-identifier")
      end
    end
    
  • Discourse 使用 fabrication gem,让我们能够轻松设置每个测试所需的测试数据。Discourse 核心中可用的完整 fabricator 列表也可以在主题的系统测试中使用。

    示例:

    RSpec.describe "Testing A Theme", system: true do
      let!(:theme) do
        upload_theme
      end
    
      it "displays the theme" do
        user = Fabricate(:user)
        category = Fabricate(:category)
        topic = Fabricate(:topic)
        topic_2 = Fabricate(:topic, title: "This is topic number 2")
    
        ...
      end
    end
    
  • 使用 sign_in 辅助方法来针对不同的用户资料进行测试。

    示例:

    RSpec.describe "Testing A Theme", system: true do
      let!(:theme) do
        upload_theme
      end
    
      it "does not display the theme for a regular user" do
        user = Fabricate(:user)
        sign_in(user)
    
        ...
      end
    
      it "displays the theme for a staff user" do
        admin = Fabricate(:admin)
        sign_in(admin)
    
        ...
      end
    end
    
  • 有时你希望让系统测试中对页面各部分的查询和检查更简单、更可复用。为此你可以使用 PageObjects 的概念,在核心代码中经常能看到这种做法。

    示例:

    # frozen_string_literal: true
    
    module PageObjects
      module Components
        class MyCustomComponent < PageObjects::Components::Base
          COMPONENT_SELECTOR = ".my-custom-component"
    
          def click_action_button
            find("#{COMPONENT_SELECTOR} .action-button").click
          end
    
          def has_content?(content)
            has_css?("#{COMPONENT_SELECTOR} .content", text: content)
          end
        end
      end
    end
    

    然后可以在系统测试文件的顶部,使用 Ruby 内置的 require_relative 将其导入后使用。

    require_relative "page_objects/components/my_custom_component"
    

运行主题系统测试

主题系统测试可以使用 discourse_theme CLI rubygem 运行,并可以按照这些说明安装。

安装 discourse_theme CLI 后,你可以在主题目录中运行以下命令来运行所有系统测试:

discourse_theme rspec .

第一次对新主题运行 rspec 命令时,系统会提示你是想使用本地 Discourse 开发环境还是 Docker 容器来运行系统测试,后者会为你配置好开发环境。除非你是经验丰富的 Discourse 插件或主题开发者,否则我们推荐选择 n 并使用 Docker 容器运行测试,因为一切都开箱即用。

discourse_theme rspec 命令还支持运行单个 spec 目录、单个文件以及带行号的文件。

discourse_theme rspec /path/to/theme/spec/system
discourse_theme rspec /path/to/theme/spec/system/my_system_spec.rb
discourse_theme rspec /path/to/theme/spec/system/my_system_spec.rb:12

有头模式

默认情况下,主题系统测试使用 Google Chrome 的无头模式运行。在这种模式下,浏览器不会在屏幕上渲染任何内容,使测试运行能更快完成。不过,让 Google Chrome 以有头模式运行,从而看到你编写的系统测试正在做什么,通常很有用。你可以通过向 discourse_theme rspec 命令传递 --headful 选项来启用这种模式。

discourse_theme rspec . --headful

上面的命令将以有头模式运行系统测试,可以直观地看到测试的运行过程。

你还可以在测试用例中使用 pause_test 辅助方法暂停测试的执行,以便在浏览器中检查应用程序的当前状态。

示例:

RSpec.describe "Testing A Theme", system: true do
  let!(:theme) do
    upload_theme
  end

  it "displays the theme" do
    visit("/")
    click("#some-button")
    pause_test
    # ...
  end
end