webdriver-user-agent 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rvmrc +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/lib/device-info/devices.yaml +32 -0
- data/lib/webdriver-user-agent.rb +54 -0
- data/spec/webdriver-user-agent_spec.rb +58 -0
- data/webdriver-user-agent.gemspec +19 -0
- metadata +91 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Alister Scott
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Webdriver::User::Agent
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'webdriver-user-agent'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install webdriver-user-agent
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
:iphone:
|
2
|
+
:user_agent: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5"
|
3
|
+
:portrait:
|
4
|
+
:width: 320
|
5
|
+
:height: 356
|
6
|
+
:landscape:
|
7
|
+
:width: 480
|
8
|
+
:height: 196
|
9
|
+
:ipad:
|
10
|
+
:user_agent: "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10"
|
11
|
+
:portrait:
|
12
|
+
:width: 768
|
13
|
+
:height: 946
|
14
|
+
:landscape:
|
15
|
+
:width: 1024
|
16
|
+
:height: 690
|
17
|
+
:android_phone:
|
18
|
+
:user_agent: "Mozilla/5.0 (Linux; U; Android 4.0.1; en-us; sdk Build/ICS_MR0) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
|
19
|
+
:portrait:
|
20
|
+
:width: 320
|
21
|
+
:height: 356
|
22
|
+
:landscape:
|
23
|
+
:width: 480
|
24
|
+
:height: 196
|
25
|
+
:android_tablet:
|
26
|
+
:user_agent: "Mozilla/5.0 (Linux; U; Android 3.0; en-us; GT-P7100 Build/HRI83) AppleWebkit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13"
|
27
|
+
:portrait:
|
28
|
+
:width: 768
|
29
|
+
:height: 946
|
30
|
+
:landscape:
|
31
|
+
:width: 1024
|
32
|
+
:height: 690
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'selenium-webdriver'
|
2
|
+
require 'facets/hash/except'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module UserAgent
|
6
|
+
def self.driver options={}
|
7
|
+
options[:browser] ||= :firefox
|
8
|
+
options[:agent] ||= :iphone
|
9
|
+
options[:orientation] ||= :portrait
|
10
|
+
|
11
|
+
user_agent_string = agent_string_for options[:agent]
|
12
|
+
device_resolution = resolution_for options[:agent], options[:orientation]
|
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."
|
23
|
+
end
|
24
|
+
driver = Selenium::WebDriver.for options[:browser], options.except(:browser, :agent, :orientation)
|
25
|
+
resize_inner_window driver, *device_resolution
|
26
|
+
driver
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.devices
|
30
|
+
@devices ||= YAML.load_file File.expand_path("../device-info/devices.yaml", __FILE__)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.resolution_for device_name, orientation
|
34
|
+
device = devices[device_name.downcase.to_sym][orientation.downcase.to_sym]
|
35
|
+
[device[:width],device[:height]]
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.agent_string_for device
|
39
|
+
user_agent_string = devices[device.downcase.to_sym][:user_agent]
|
40
|
+
raise "Unsupported user agent: '#{options[:agent]}'." unless user_agent_string
|
41
|
+
user_agent_string
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
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
|
51
|
+
end
|
52
|
+
driver.execute_script("window.innerWidth = #{width}; window.innerHeight = #{height};")
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
require 'webdriver-user-agent'
|
6
|
+
require 'selenium-webdriver'
|
7
|
+
|
8
|
+
describe "webdriver user agent" do
|
9
|
+
after :each do
|
10
|
+
@driver.quit if @driver
|
11
|
+
end
|
12
|
+
|
13
|
+
it "can create a new webdriver driver using firefox and iphone (portrait) by default" do
|
14
|
+
@driver = UserAgent.driver
|
15
|
+
@driver.browser.should == :firefox
|
16
|
+
@driver.execute_script('return navigator.userAgent').should include 'iPhone'
|
17
|
+
@driver.execute_script('return window.innerWidth').should == 320
|
18
|
+
@driver.execute_script('return window.innerHeight').should == 356
|
19
|
+
end
|
20
|
+
|
21
|
+
it "can create a new webdriver driver using chrome and iPad (landscape)" do
|
22
|
+
@driver = UserAgent.driver(:browser => :chrome, :agent => :iPad, :orientation => :landscape)
|
23
|
+
@driver.browser.should == :chrome
|
24
|
+
@driver.execute_script('return navigator.userAgent').should include 'iPad'
|
25
|
+
@driver.execute_script('return window.innerWidth').should == 1024
|
26
|
+
@driver.execute_script('return window.innerHeight').should == 690
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can create a new webdriver driver using firefox and android phone (landscape)" do
|
30
|
+
@driver = UserAgent.driver(:browser => :chrome, :agent => :android_phone, :orientation => :landscape)
|
31
|
+
@driver.browser.should == :chrome
|
32
|
+
@driver.execute_script('return navigator.userAgent').should include 'Android'
|
33
|
+
@driver.execute_script('return window.innerWidth').should == 480
|
34
|
+
@driver.execute_script('return window.innerHeight').should == 196
|
35
|
+
end
|
36
|
+
|
37
|
+
it "can create a new webdriver driver using firefox and android tablet (portrait)" do
|
38
|
+
@driver = UserAgent.driver(:browser => :chrome, :agent => :android_tablet, :orientation => :portrait)
|
39
|
+
@driver.browser.should == :chrome
|
40
|
+
@driver.execute_script('return navigator.userAgent').should include 'Android'
|
41
|
+
@driver.execute_script('return window.innerWidth').should == 768
|
42
|
+
@driver.execute_script('return window.innerHeight').should == 946
|
43
|
+
end
|
44
|
+
|
45
|
+
it "can create a new webdriver driver using an existing firefox profile" do
|
46
|
+
profile = Selenium::WebDriver::Firefox::Profile.new
|
47
|
+
#profile['browser.startup.page'] = 1
|
48
|
+
profile['browser.startup.homepage'] = "data:text/html,<title>hello</title>"
|
49
|
+
@driver = UserAgent.driver(:browser => :firefox, :profile => profile)
|
50
|
+
@driver.browser.should == :firefox
|
51
|
+
@driver.execute_script('return navigator.userAgent').should include 'iPhone'
|
52
|
+
@driver.execute_script('return window.innerWidth').should == 320
|
53
|
+
@driver.execute_script('return window.innerHeight').should == 356
|
54
|
+
@driver.title.should == 'hello'
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Alister Scott"]
|
5
|
+
gem.email = ["alister.scott@gmail.com"]
|
6
|
+
gem.description = %q{A helper gem to emulate populate device user agents and resolutions when using webdriver}
|
7
|
+
gem.summary = %q{A helper gem to emulate populate device user agents and resolutions when using webdriver}
|
8
|
+
gem.homepage = ""
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "webdriver-user-agent"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = "0.0.1"
|
16
|
+
gem.add_dependency 'selenium-webdriver'
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_development_dependency 'facets'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webdriver-user-agent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alister Scott
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: selenium-webdriver
|
16
|
+
requirement: &70161722386240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70161722386240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70161722385680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70161722385680
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: facets
|
38
|
+
requirement: &70161722385200 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70161722385200
|
47
|
+
description: A helper gem to emulate populate device user agents and resolutions when
|
48
|
+
using webdriver
|
49
|
+
email:
|
50
|
+
- alister.scott@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rvmrc
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- lib/device-info/devices.yaml
|
62
|
+
- lib/webdriver-user-agent.rb
|
63
|
+
- spec/webdriver-user-agent_spec.rb
|
64
|
+
- webdriver-user-agent.gemspec
|
65
|
+
homepage: ''
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.17
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: A helper gem to emulate populate device user agents and resolutions when
|
89
|
+
using webdriver
|
90
|
+
test_files:
|
91
|
+
- spec/webdriver-user-agent_spec.rb
|