browser-provider 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/BrowserProvider.rb +19 -0
- data/lib/WatirBrowserFactory.rb +29 -0
- data/lib/WebDriverBrowserFactory.rb +48 -0
- data/lib/webdriver-user-agent-fil.rb +66 -0
- metadata +48 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
class BrowserProvider
|
2
|
+
def initialize(name,platform,agent)
|
3
|
+
@name = name
|
4
|
+
@platform = platform
|
5
|
+
@agent = agent
|
6
|
+
end
|
7
|
+
|
8
|
+
def getInstance
|
9
|
+
|
10
|
+
if @name == 'IE'
|
11
|
+
puts File.dirname(__FILE__)
|
12
|
+
require File.dirname(__FILE__)+'/WatirBrowserFactory'
|
13
|
+
return WatirBrowserFactory.new(@name)
|
14
|
+
else
|
15
|
+
require File.dirname(__FILE__)+'/WebDriverBrowserFactory'
|
16
|
+
return WebDriverBrowserFactory.new(@name,@platform,@agent)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'watir'
|
2
|
+
|
3
|
+
class WatirBrowserFactory
|
4
|
+
def initialize(name)
|
5
|
+
@name = name
|
6
|
+
end
|
7
|
+
|
8
|
+
def getInstance
|
9
|
+
if @name == 'IE'
|
10
|
+
begin
|
11
|
+
@br = Watir::Browser.new :ie
|
12
|
+
rescue FFI::NotFoundError
|
13
|
+
@br = Watir::Browser.new :ie
|
14
|
+
rescue =>err
|
15
|
+
end
|
16
|
+
return @br
|
17
|
+
# elsif @name == 'FF'
|
18
|
+
# profile = Selenium::WebDriver::Firefox::Profile.new
|
19
|
+
# profile.assume_untrusted_certificate_issuer = true
|
20
|
+
# @b = Watir::Browser.new :firefox, :profile => profile
|
21
|
+
# return @br
|
22
|
+
# elsif @name == 'chrome'
|
23
|
+
# @b = Watir::Browser.new :chrome, :switches => ['--ignore-certificate-errors']
|
24
|
+
# return @br
|
25
|
+
# else
|
26
|
+
# raise ArgumentError
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'watir-webdriver'
|
2
|
+
require 'webdriver-user-agent-fil'
|
3
|
+
class WebDriverBrowserFactory
|
4
|
+
def initialize(name,platform,agent)
|
5
|
+
@name = name
|
6
|
+
@platform = platform
|
7
|
+
@agent = agent
|
8
|
+
end
|
9
|
+
|
10
|
+
def getInstance
|
11
|
+
|
12
|
+
if @platform == 'PC'
|
13
|
+
if @name == 'firefox'
|
14
|
+
profile = Selenium::WebDriver::Firefox::Profile.new
|
15
|
+
profile.assume_untrusted_certificate_issuer = true
|
16
|
+
@b = Watir::Browser.new :firefox, :profile => profile
|
17
|
+
return @b
|
18
|
+
elsif @name == 'chrome'
|
19
|
+
@b = Watir::Browser.new :chrome, :switches => ['--ignore-certificate-errors']
|
20
|
+
return @b
|
21
|
+
else
|
22
|
+
raise ArgumentError
|
23
|
+
end
|
24
|
+
elsif @platform == 'mobile'
|
25
|
+
# :browser
|
26
|
+
# :firefox (default)
|
27
|
+
# :chrome
|
28
|
+
# :agent
|
29
|
+
# :iphone (default)
|
30
|
+
# :ipad
|
31
|
+
# :android_phone
|
32
|
+
# :android_tablet
|
33
|
+
# :orientation
|
34
|
+
# :portrait (default)
|
35
|
+
# :landscape
|
36
|
+
driver = UserAgent.driver(:browser => @name.intern, :agent => @agent.intern, :orientation => :portrait)
|
37
|
+
@b = Watir::Browser.new driver
|
38
|
+
else
|
39
|
+
raise ArgumentError
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
def getMobileDriver()
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'selenium-webdriver'
|
2
|
+
require 'facets/hash/except'
|
3
|
+
require 'yaml'
|
4
|
+
require 'json'
|
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
|
+
options[:profile]['assume_untrusted_certificate_issuer'] = true
|
19
|
+
when :chrome
|
20
|
+
options[:switches] ||= []
|
21
|
+
options[:switches] << "--user-agent=#{user_agent_string}"
|
22
|
+
options[:switches] << "--ignore-certificate-errors"
|
23
|
+
else
|
24
|
+
raise "WebDriver UserAgent currently only supports :firefox and :chrome."
|
25
|
+
end
|
26
|
+
driver = Selenium::WebDriver.for options[:browser], options.except(:browser, :agent, :orientation)
|
27
|
+
resize_inner_window(driver, *resolution_for(options[:agent], options[:orientation])) unless (downcase_sym(options[:agent]) == :random)
|
28
|
+
driver
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.devices
|
32
|
+
@devices ||= YAML.load_file File.expand_path("../device-info/devices.yaml", __FILE__)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.resolution_for device_name, orientation
|
36
|
+
device = devices[downcase_sym device_name][downcase_sym orientation]
|
37
|
+
[device[:width],device[:height]]
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.agent_string_for device
|
41
|
+
user_agent_string = downcase_sym(device) == :random ? random_user_agent : devices[downcase_sym device][:user_agent]
|
42
|
+
raise "Unsupported user agent: '#{options[:agent]}'." unless user_agent_string
|
43
|
+
user_agent_string
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def self.resize_inner_window driver, width, height
|
49
|
+
if driver.browser == :firefox or :chrome
|
50
|
+
driver.execute_script("window.open(#{driver.current_url.to_json},'_blank');")
|
51
|
+
driver.close
|
52
|
+
driver.switch_to.window driver.window_handles.first
|
53
|
+
end
|
54
|
+
driver.execute_script("window.innerWidth = #{width}; window.innerHeight = #{height};")
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.downcase_sym sym
|
58
|
+
sym.to_s.downcase.to_sym #to support ruby 1.8.x
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.random_user_agent
|
62
|
+
File.foreach(File.expand_path("../device-info/user_agents.txt", __FILE__)).each_with_index.reduce(nil) { |picked,pair|
|
63
|
+
rand < 1.0/(1+pair[1]) ? pair[0] : picked }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: browser-provider
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Jiang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ''
|
15
|
+
email: Ruixin.Jiang@fil.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/BrowserProvider.rb
|
21
|
+
- lib/WatirBrowserFactory.rb
|
22
|
+
- lib/WebDriverBrowserFactory.rb
|
23
|
+
- lib/webdriver-user-agent-fil.rb
|
24
|
+
homepage: ''
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.24
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: browser-provider
|
48
|
+
test_files: []
|