webdriver-user-agent 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +4 -0
- data/lib/webdriver-user-agent.rb +50 -48
- data/spec/webdriver-user-agent_spec.rb +8 -8
- data/webdriver-user-agent.gemspec +1 -1
- metadata +2 -2
data/HISTORY.md
CHANGED
data/lib/webdriver-user-agent.rb
CHANGED
@@ -3,62 +3,64 @@ require 'facets/hash/except'
|
|
3
3
|
require 'yaml'
|
4
4
|
require 'json'
|
5
5
|
|
6
|
-
module
|
7
|
-
|
8
|
-
options
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def self.devices
|
31
|
+
@devices ||= YAML.load_file File.expand_path("../device-info/devices.yaml", __FILE__)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
45
|
+
private
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
56
|
+
def self.downcase_sym sym
|
57
|
+
sym.to_s.downcase.to_sym #to support ruby 1.8.x
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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.
|
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.
|
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:
|
12
|
+
date: 2013-02-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: selenium-webdriver
|