webdriver-user-agent 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Gem History
2
2
 
3
+ ## 0.2.1 - 11 February 2012
4
+
5
+ * Small changes for more compatibility alongside user-agent gem
6
+
3
7
  ## 0.2.0 - 20 September 2012
4
8
 
5
9
  * Added support for a :random user agent (Idea based upon Christoph Pilka)
@@ -3,62 +3,64 @@ require 'facets/hash/except'
3
3
  require 'yaml'
4
4
  require 'json'
5
5
 
6
- module UserAgent
7
- def self.driver options={}
8
- options[:browser] ||= :firefox
9
- options[:agent] ||= :iphone
10
- options[:orientation] ||= :portrait
11
-
12
- user_agent_string = agent_string_for options[:agent]
13
-
14
- case options[:browser]
15
- when :firefox
16
- options[:profile] ||= Selenium::WebDriver::Firefox::Profile.new
17
- options[:profile]['general.useragent.override'] = user_agent_string
18
- when :chrome
19
- options[:switches] ||= []
20
- options[:switches] << "--user-agent=#{user_agent_string}"
21
- else
22
- raise "WebDriver UserAgent currently only supports :firefox and :chrome."
6
+ module Webdriver
7
+ module UserAgent
8
+ def self.driver options={}
9
+ options[:browser] ||= :firefox
10
+ options[:agent] ||= :iphone
11
+ options[:orientation] ||= :portrait
12
+
13
+ user_agent_string = agent_string_for options[:agent]
14
+
15
+ case options[:browser]
16
+ when :firefox
17
+ options[:profile] ||= Selenium::WebDriver::Firefox::Profile.new
18
+ options[:profile]['general.useragent.override'] = user_agent_string
19
+ when :chrome
20
+ options[:switches] ||= []
21
+ options[:switches] << "--user-agent=#{user_agent_string}"
22
+ else
23
+ raise "WebDriver UserAgent currently only supports :firefox and :chrome."
24
+ end
25
+ driver = Selenium::WebDriver.for options[:browser], options.except(:browser, :agent, :orientation)
26
+ resize_inner_window(driver, *resolution_for(options[:agent], options[:orientation])) unless (downcase_sym(options[:agent]) == :random)
27
+ driver
23
28
  end
24
- driver = Selenium::WebDriver.for options[:browser], options.except(:browser, :agent, :orientation)
25
- resize_inner_window(driver, *resolution_for(options[:agent], options[:orientation])) unless (downcase_sym(options[:agent]) == :random)
26
- driver
27
- end
28
29
 
29
- def self.devices
30
- @devices ||= YAML.load_file File.expand_path("../device-info/devices.yaml", __FILE__)
31
- end
30
+ def self.devices
31
+ @devices ||= YAML.load_file File.expand_path("../device-info/devices.yaml", __FILE__)
32
+ end
32
33
 
33
- def self.resolution_for device_name, orientation
34
- device = devices[downcase_sym device_name][downcase_sym orientation]
35
- [device[:width],device[:height]]
36
- end
34
+ def self.resolution_for device_name, orientation
35
+ device = devices[downcase_sym device_name][downcase_sym orientation]
36
+ [device[:width],device[:height]]
37
+ end
37
38
 
38
- def self.agent_string_for device
39
- user_agent_string = downcase_sym(device) == :random ? random_user_agent : devices[downcase_sym device][:user_agent]
40
- raise "Unsupported user agent: '#{options[:agent]}'." unless user_agent_string
41
- user_agent_string
42
- end
39
+ def self.agent_string_for device
40
+ user_agent_string = downcase_sym(device) == :random ? random_user_agent : devices[downcase_sym device][:user_agent]
41
+ raise "Unsupported user agent: '#{options[:agent]}'." unless user_agent_string
42
+ user_agent_string
43
+ end
43
44
 
44
- private
45
+ private
45
46
 
46
- def self.resize_inner_window driver, width, height
47
- if driver.browser == :firefox or :chrome
48
- driver.execute_script("window.open(#{driver.current_url.to_json},'_blank');")
49
- driver.close
50
- driver.switch_to.window driver.window_handles.first
47
+ def self.resize_inner_window driver, width, height
48
+ if driver.browser == :firefox or :chrome
49
+ driver.execute_script("window.open(#{driver.current_url.to_json},'_blank');")
50
+ driver.close
51
+ driver.switch_to.window driver.window_handles.first
52
+ end
53
+ driver.execute_script("window.innerWidth = #{width}; window.innerHeight = #{height};")
51
54
  end
52
- driver.execute_script("window.innerWidth = #{width}; window.innerHeight = #{height};")
53
- end
54
55
 
55
- def self.downcase_sym sym
56
- sym.to_s.downcase.to_sym #to support ruby 1.8.x
57
- end
56
+ def self.downcase_sym sym
57
+ sym.to_s.downcase.to_sym #to support ruby 1.8.x
58
+ end
58
59
 
59
- def self.random_user_agent
60
- File.foreach(File.expand_path("../device-info/user_agents.txt", __FILE__)).each_with_index.reduce(nil) { |picked,pair|
61
- rand < 1.0/(1+pair[1]) ? pair[0] : picked }
62
- end
60
+ def self.random_user_agent
61
+ File.foreach(File.expand_path("../device-info/user_agents.txt", __FILE__)).each_with_index.reduce(nil) { |picked,pair|
62
+ rand < 1.0/(1+pair[1]) ? pair[0] : picked }
63
+ end
63
64
 
65
+ end
64
66
  end
@@ -12,7 +12,7 @@ describe "webdriver user agent" do
12
12
  end
13
13
 
14
14
  it "can create a new webdriver driver using firefox and iphone (portrait) by default" do
15
- @driver = UserAgent.driver
15
+ @driver = Webdriver::UserAgent.driver
16
16
  @driver.browser.should == :firefox
17
17
  @driver.execute_script('return navigator.userAgent').should include 'iPhone'
18
18
  @driver.execute_script('return window.innerWidth').should == 320
@@ -20,7 +20,7 @@ describe "webdriver user agent" do
20
20
  end
21
21
 
22
22
  it "can create a new webdriver driver using chrome and iPad (landscape)" do
23
- @driver = UserAgent.driver(:browser => :chrome, :agent => :iPad, :orientation => :landscape)
23
+ @driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iPad, :orientation => :landscape)
24
24
  @driver.browser.should == :chrome
25
25
  @driver.execute_script('return navigator.userAgent').should include 'iPad'
26
26
  @driver.execute_script('return window.innerWidth').should == 1024
@@ -28,7 +28,7 @@ describe "webdriver user agent" do
28
28
  end
29
29
 
30
30
  it "can create a new webdriver driver using firefox and android phone (landscape)" do
31
- @driver = UserAgent.driver(:browser => :chrome, :agent => :android_phone, :orientation => :landscape)
31
+ @driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :android_phone, :orientation => :landscape)
32
32
  @driver.browser.should == :chrome
33
33
  @driver.execute_script('return navigator.userAgent').should include 'Android'
34
34
  @driver.execute_script('return window.innerWidth').should == 480
@@ -36,7 +36,7 @@ describe "webdriver user agent" do
36
36
  end
37
37
 
38
38
  it "can create a new webdriver driver using firefox and android tablet (portrait)" do
39
- @driver = UserAgent.driver(:browser => :chrome, :agent => :android_tablet, :orientation => :portrait)
39
+ @driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :android_tablet, :orientation => :portrait)
40
40
  @driver.browser.should == :chrome
41
41
  @driver.execute_script('return navigator.userAgent').should include 'Android'
42
42
  @driver.execute_script('return window.innerWidth').should == 768
@@ -44,7 +44,7 @@ describe "webdriver user agent" do
44
44
  end
45
45
 
46
46
  it "can create a new webdriver driver using firefox and random user agent" do
47
- @driver = UserAgent.driver(:agent => :random)
47
+ @driver = Webdriver::UserAgent.driver(:agent => :random)
48
48
  @driver.browser.should == :firefox
49
49
  @driver.execute_script('return navigator.userAgent').should_not be_nil
50
50
  @driver.execute_script('return window.innerWidth').should_not == 320
@@ -52,7 +52,7 @@ describe "webdriver user agent" do
52
52
  end
53
53
 
54
54
  it "can create a new webdriver driver using chrome and random user agent" do
55
- @driver = UserAgent.driver(:browser => :chrome, :agent => :random)
55
+ @driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :random)
56
56
  @driver.browser.should == :chrome
57
57
  @driver.execute_script('return navigator.userAgent').should_not be_nil
58
58
  @driver.execute_script('return window.innerWidth').should_not == 320
@@ -62,7 +62,7 @@ describe "webdriver user agent" do
62
62
  it "can create a new webdriver driver using an existing firefox profile" do
63
63
  profile = Selenium::WebDriver::Firefox::Profile.new
64
64
  profile['browser.startup.homepage'] = "data:text/html,<title>hello</title>"
65
- @driver = UserAgent.driver(:browser => :firefox, :profile => profile)
65
+ @driver = Webdriver::UserAgent.driver(:browser => :firefox, :profile => profile)
66
66
  @driver.browser.should == :firefox
67
67
  @driver.execute_script('return navigator.userAgent').should include 'iPhone'
68
68
  @driver.execute_script('return window.innerWidth').should == 320
@@ -71,7 +71,7 @@ describe "webdriver user agent" do
71
71
  end
72
72
 
73
73
  it "can allow using selenium driver for watir browser" do
74
- @driver = UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation => :portrait)
74
+ @driver = Webdriver::UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation => :portrait)
75
75
  @browser = Watir::Browser.new @driver
76
76
  @browser.url.should == "about:blank"
77
77
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "webdriver-user-agent"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = "0.2.0"
15
+ gem.version = "0.2.1"
16
16
  gem.add_dependency 'selenium-webdriver'
17
17
  gem.add_dependency 'facets'
18
18
  gem.add_dependency 'json'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webdriver-user-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-20 00:00:00.000000000 Z
12
+ date: 2013-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: selenium-webdriver