Top Level Namespace

Defined Under Namespace

Modules: Mediawiki

Instance Method Summary collapse

Instance Method Details

#browser(environment, test_name, language) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mediawiki/selenium/env.rb', line 22

def browser(environment, test_name, language)
  if environment == :saucelabs
    sauce_browser(test_name, language)
  else
    local_browser(language)
  end
end

#environmentObject



29
30
31
32
33
34
35
# File 'lib/mediawiki/selenium/env.rb', line 29

def environment
  if ENV['BROWSER_LABEL'] and ENV['SAUCE_ONDEMAND_USERNAME'] and ENV['SAUCE_ONDEMAND_ACCESS_KEY']
    :saucelabs
  else
    :local
  end
end

#local_browser(language) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mediawiki/selenium/env.rb', line 36

def local_browser(language)
  if ENV['BROWSER_LABEL']
    browser_label = ENV['BROWSER_LABEL'].to_sym
  else
    browser_label = :firefox
  end

  if language == 'default'
    Watir::Browser.new browser_label
  else
    if browser_label == :firefox
      profile = Selenium::WebDriver::Firefox::Profile.new
    elsif browser_label == :chrome
      profile = Selenium::WebDriver::Chrome::Profile.new
    else
      raise "Changing default language is currently supported only for Firefox and Chrome!"
    end
    profile['intl.accept_languages'] = language
    Watir::Browser.new browser_label, :profile => profile
  end
end

#sauce_api(json) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/mediawiki/selenium/env.rb', line 57

def sauce_api(json)
RestClient::Request.execute(
  :method => :put,
  :url => "https://saucelabs.com/rest/v1/#{ENV['SAUCE_ONDEMAND_USERNAME']}/jobs/#{$session_id}",
  :user => ENV['SAUCE_ONDEMAND_USERNAME'],
  :password => ENV['SAUCE_ONDEMAND_ACCESS_KEY'],
  :headers => {:content_type => "application/json"},
  :payload => json
)
end

#sauce_browser(test_name, language) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mediawiki/selenium/env.rb', line 67

def sauce_browser(test_name, language)
  config = YAML.load_file('config/config.yml')
  browser_label = config[ENV['BROWSER_LABEL']]

  if language == 'default'
    caps = Selenium::WebDriver::Remote::Capabilities.send(browser_label['name'])
  elsif browser_label['name'] == 'firefox'
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['intl.accept_languages'] = language
    caps = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
  elsif browser_label['name'] == 'chrome'
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['intl.accept_languages'] = language
    caps = Selenium::WebDriver::Remote::Capabilities.chrome('chrome.profile' => profile.as_json['zip'])
  end

  caps.platform = browser_label['platform']
  caps.version = browser_label['version']
  caps[:name] = "#{test_name} #{ENV['JOB_NAME']}##{ENV['BUILD_NUMBER']}"

  require 'selenium/webdriver/remote/http/persistent' # http_client
  browser = Watir::Browser.new(
    :remote,
    http_client: Selenium::WebDriver::Remote::Http::Persistent.new,
    url: "http://#{ENV['SAUCE_ONDEMAND_USERNAME']}:#{ENV['SAUCE_ONDEMAND_ACCESS_KEY']}@ondemand.saucelabs.com:80/wd/hub",
    desired_capabilities: caps)

  browser.wd.file_detector = lambda do |args|
    # args => ['/path/to/file']
    str = args.first.to_s
    str if File.exist?(str)
  end

  browser
end

#test_name(scenario) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/mediawiki/selenium/env.rb', line 102

def test_name(scenario)
  if scenario.respond_to? :feature
    "#{scenario.feature.name}: #{scenario.name}"
  elsif scenario.respond_to? :scenario_outline
    "#{scenario.scenario_outline.feature.name}: #{scenario.scenario_outline.name}: #{scenario.name}"
  end
end