Skip to content

Instantly share code, notes, and snippets.

@chaomao
Created July 24, 2016 10:37
Show Gist options
  • Save chaomao/1b3a3db6c57a5d0cbbcaba1d8b1843d2 to your computer and use it in GitHub Desktop.
Save chaomao/1b3a3db6c57a5d0cbbcaba1d8b1843d2 to your computer and use it in GitHub Desktop.
list everything about feature test

Automation Test

  • End to End Test
  • Integration Test
  • Unit Test

In my mind, smoking test is end to end test, feature test is integration test. We want to enable everybody writing automation test.

End To End Test

  • cover main path/feature
  • make sure product 80% correct
  • good feedback for product
  • running time is long, slow feedback
  • easy to break, hard to maintain
  • not somebody's responsibility, everyone's

Integration Test

  • cover most case of specific feature
  • make sure all path/case is correct
  • good feedback for specific feature
  • running time is short, fast feedback
  • easy to break, not hard to maintain
  • not somebody's responsibility, everyone's

Selenium

  • start in ThoughtWorks, currently host by community
  • a lot of tools, such as IDE
  • webdriver introduced in version 2.0
  • different webdriver control different browser

WebDriver

control browser, simulate end user interacte with browser

  • selenium, firefox
  • webkit, web browser engine
  • poltergeist, phantomjs
  • watir, IE

Capybara

  • ruby gem
  • provide high level DSL, adapted by differ webdirver
  • integrate with rails, rspec, cucumber and minitest

Workflow

fill_in('username', 'support')

capybara -> webdriver -> browser

Rspec Feature Test

We use rspec to write feature test

short test == feature test

long test == smoking test

require "rails_helper"
RSpec.feature "Widget management", :type => :feature do
  scenario "User creates a new widget" do
    visit "/widgets/new"

    fill_in "Name", :with => "My Widget"
    click_button "Create Widget"

    expect(page).to have_text("Widget was successfully created.")
  end
end

PageObject

  • 咱哥的文章
  • extract visit, fill_in into object, ready to reuse in next test

Gizmo


Page Module

  • plain ruby module
  • use PageWith as prefix
  • need include Gizmo::PageMixin
  • has valid, state, query and action method
module PageWithGithubSearch

  include Gizmo::PageMixin

  # valid method
  def valid?
    has_selector?("form[action='/search']")
  end
  
  # state method
  def link_with_text(value)
    find("a", :text => value)
  end
  
  # query method
  def have_content(value)
    has_content?(value)
  end

  # action method
  define_action :click_element do |element|
    locate(element).click
  end
end

Rspec Feature Test

feature 'smoke test', :smoking do
  scenario 'support create organizations ' do
    on_page_with :github_search do
      expect(page.link_with_text('search')).to be_truthy
      page.click_element('.search')
      expect(page).to have_content('search results')
    end
  end
end

we refactor that

on_page_with : github_search do
  step :-, :click_element, with: { element: '.search' }
  sleep(1)
  expect(page).to have_text 'search results'
end

Folder Structure

  • spec/features/smokingtest, test file
  • lib/active_document/lib/dsl.rb, DSL in test file
  • lib/gizmo/pages, page object

How to Write Feature Test?

  • create new test fil in spec/features
  • create/reuse page object in lib/gizmo/pages
  • add new method in page object
  • run and debug

How to Run Write Feature Test?

  • start web server
  • setup database
  • write code to control browser
  • asset result on browser page

Run Smoking Test Locally

  • prepare assets file, such as js, css, running command webpack && gulp vendor
  • run feature test, running command TEST_TYPE=smoking rake test:smoking_locally, capybara will start web server
  • test file and page object file will be loaded and run

Run Feature Test Remotely

  • run feature test, runnning command REMOTE=https://www.gcbeta.com TEST_TYPE=smoking rake test:smoking_remotely
  • no need start web server, just simulate user click
  • need remove data after running

LET'S WRITE A FEATURE TEST NEXT TIME TOGETHER!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment