Jump to content

Selenium/Ruby/MediaWiki API

From mediawiki.org

This tutorial will assume that you are running tests from your machine, targeting beta cluster.

You can use the MediaWiki action API for testing, reusing parts of Selenium/Stack, but without Selenium.

Examples will:

  • Check if a page exists using MediaWiki action API.

Stack

[edit]

The stack:

Language Ruby
Assertion library RSpec Expectations (ships with RSpec)
Testing framework RSpec
Mediawiki API mediawiki_api

Advantages

[edit]
  • Simple stack with assertions and testing framework (setup, teardown, reporting...).

Disadvantages

[edit]
  • Another tool to learn (testing framework).

Code

[edit]
require 'mediawiki_api'

RSpec.describe 'Page' do
  before(:all) do
    @client = MediawikiApi::Client.new 'https://en.wikipedia.beta.wmflabs.org/w/api.php'
  end

  it 'Main Page should exist' do
    main_page = @client.action(:query, titles: ['Main Page']).data['pages']
    # => {"1"=>{"pageid"=>1, "ns"=>0, "title"=>"Main Page"}}

    # "1" means the page exists
    expect(main_page['1']).to be_instance_of Hash

    # "-1" means the page does not exist
    expect(main_page['-1']).to be_instance_of NilClass
  end

  it 'Missing Page should not exist' do
    missing_page =
      @client.action(:query, titles: ['Missing Page']).data['pages']
    # => {"-1"=>{"ns"=>0, "title"=>"Missing Page", "missing"=>""}}

    # "1" means the page exists
    expect(missing_page['1']).to be_instance_of NilClass

    # "-1" means the page does not exist
    expect(missing_page['-1']).to be_instance_of Hash
  end
end

Output

[edit]

Everything is fine.

$ bundle exec rspec spec/api_spec.rb 
..
Finished in 1.78 seconds (files took 0.13949 seconds to load)
2 examples, 0 failures

There is a problem.

$ bundle exec rspec spec/api_spec.rb 
F.

Failures:

  1) Page Main Page should exist
     Failure/Error: expect(main_page['1']).to be_instance_of Hash
       expected nil to be an instance of Hash
     # ./spec/api_spec.rb:14:in `block (2 levels) in <top (required)>'

Finished in 2.43 seconds (files took 0.13955 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/api_spec.rb:8 # Page Main Page should exist