Top Level Namespace

Defined Under Namespace

Modules: MediawikiSelenium, URL Classes: APIPage, LoginPage, RandomPage, ResetPreferencesPage

Instance Method Summary collapse

Instance Method Details

#browser(test_name, configuration = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/mediawiki_selenium/support/env.rb', line 28

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

#browser_nameObject



35
36
37
38
39
40
41
# File 'lib/mediawiki_selenium/support/env.rb', line 35

def browser_name
  if ENV["BROWSER"]
    ENV["BROWSER"].to_sym
  else
    :firefox
  end
end

#environmentObject



42
43
44
45
46
47
48
# File 'lib/mediawiki_selenium/support/env.rb', line 42

def environment
  if ENV["SAUCE_ONDEMAND_USERNAME"] and ENV["SAUCE_ONDEMAND_ACCESS_KEY"] and ENV["BROWSER"] != "phantomjs" and ENV["HEADLESS"] != "true"
    :saucelabs
  else
    :local
  end
end

#local_browser(configuration) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mediawiki_selenium/support/env.rb', line 49

def local_browser(configuration)
  if ENV["BROWSER_TIMEOUT"] && browser_name == :firefox
    timeout = ENV["BROWSER_TIMEOUT"].to_i

    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = timeout

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["dom.max_script_run_time"] = timeout
    profile["dom.max_chrome_script_run_time"] = timeout
    browser = Watir::Browser.new browser_name, :http_client => client, :profile => profile
  elsif configuration && configuration[:language] && browser_name == :firefox
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["intl.accept_languages"] = configuration[:language]
    browser = Watir::Browser.new browser_name, profile: profile
  elsif configuration && configuration[:language] && browser_name == :chrome
    prefs = {intl: {accept_languages: configuration[:language]}}
    browser = Watir::Browser.new browser_name, prefs: prefs
  elsif configuration && configuration[:language] && browser_name == :phantomjs
    capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs
    capabilities["phantomjs.page.customHeaders.Accept-Language"] = configuration[:language]
    browser = Watir::Browser.new browser_name, desired_capabilities: capabilities
  elsif configuration && configuration[:user_agent] && browser_name == :firefox
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["general.useragent.override"] = configuration[:user_agent]
    browser = Watir::Browser.new browser_name, profile: profile
  else
    browser = Watir::Browser.new browser_name
  end

  browser.window.resize_to 1280, 1024
  set_cookie(browser)
  browser
end

#sauce_api(json, session_id) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/mediawiki_selenium/support/env.rb', line 83

def sauce_api(json, session_id)
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, configuration) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mediawiki_selenium/support/env.rb', line 93

def sauce_browser(test_name, configuration)
  abort "Environment variables BROWSER, PLATFORM and VERSION have to be set" if (ENV["BROWSER"] == nil) or (ENV["PLATFORM"] == nil) or (ENV["VERSION"] == nil)

  client = Selenium::WebDriver::Remote::Http::Default.new

  if ENV["BROWSER_TIMEOUT"] && ENV["BROWSER"] == "firefox"
    timeout = ENV["BROWSER_TIMEOUT"].to_i
    client.timeout = timeout

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["dom.max_script_run_time"] = timeout
    profile["dom.max_chrome_script_run_time"] = timeout
    caps = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
  elsif configuration && configuration[:language] && ENV["BROWSER"] == "firefox"
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["intl.accept_languages"] = configuration[:language]
    caps = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
  elsif configuration && configuration[:language] && ENV["BROWSER"] == "chrome"
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile["intl.accept_languages"] = configuration[:language]
    caps = Selenium::WebDriver::Remote::Capabilities.chrome("chrome.profile" => profile.as_json["zip"])
  elsif configuration && configuration[:user_agent] && ENV["BROWSER"] == "firefox"
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["general.useragent.override"] = configuration[:user_agent]
    caps = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
  else
    caps = Selenium::WebDriver::Remote::Capabilities.send(ENV["BROWSER"])
  end

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

  browser = Watir::Browser.new(
    :remote,
    http_client: client,
    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


139
140
141
# File 'lib/mediawiki_selenium/support/env.rb', line 139

def set_cookie(browser)
  # implement this method in env.rb of the repository where it is needed
end

#test_name(scenario) ⇒ Object

implement this method in env.rb of the repository where it is needed



142
143
144
145
146
147
148
# File 'lib/mediawiki_selenium/support/env.rb', line 142

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