Cukes C0 Coverage Information - Simploco - RCov

support/browser_settings.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
support/browser_settings.rb 264 111
70.45%
29.73%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

2 =begin
3 *Name           : BrowserSettings
4 *Description    : Browser settings definition for different web and mobile browsers
5 *Author         : Chandra sekaran
6 *Creation Date  : 24/04/2015
7 *Updation Date  :
8 =end
9 
10 module CUKES
11   module BrowserSettings
12     include FileLibrary     # module that defines all file related manipulations
13     include DateTimeLibrary # module that defines all date time manipulations
14 
15     # Description       : launches a browser and returns the browser object
16     # Author            : Chandra sekaran
17     # Arguments         :
18     #   str_browser_name: name of the browser
19     # Return values     :
20     #   @browser        : browser object
21     #
22     def self.browser_setup(str_browser_name)
23       @browser_name = str_browser_name
24       @browser = start_browser(@browser_name)
25       raise "Nil class error : No valid browser object found for #{str_browser_name}" if @browser.nil?
26       return @browser
27     rescue Exception => ex
28       $log.error("Error in launching browser #{str_browser_name} : #{ex}")
29       exit
30     end
31 
32     # Description       : deletes all cookies from the current browser
33     # Author            : Chandra sekaran
34     #
35     def self.delete_cookies
36       @browser.manage.delete_all_cookies
37       $log.success("#{@browser_name} browser cookies deleted successfully")
38     rescue Exception => ex
39       $log.error("Error while deleting the " + @browser_name + " browser cookies - #{ex}")
40       exit
41     end
42 
43     # Description       : launches the given URL in the current browser
44     # Author            : Chandra sekaran
45     # Arguments         :
46     #   str_url         : url of the web site to be launched
47     #
48     def self.launch_url(str_url)
49       # delete_cookies
50       @browser.navigate.to(str_url)
51       $log.info("#{str_url} launched successfully")
52     rescue Exception => ex
53       $log.error("Error in launching URL - #{str_url}")
54       exit
55     end
56 
57     # Description       : sets the timeout limit to find elements
58     # Author            : Chandra sekaran
59     # Arguments         :
60     #   num_timeout     : numeric timeout value
61     #
62     def self.set_timeout(num_timeout)
63       @browser.manage.timeouts.implicit_wait = num_timeout
64       $log.success("Selenium timeout set to " + num_timeout.to_s)
65     rescue Exception => ex
66       $log.error("Error in setting the selenium timeout to: " + num_timeout.to_s)
67       exit
68     end
69 
70     # Description       : starts the browser and returns the browser object
71     # Author            : Chandra sekaran
72     # Arguments         :
73     #   str_browser     : browser name
74     # Return values     :
75     #   @browser        : browser object of the launched browser
76     #
77     def self.start_browser(str_browser)
78       @browser = ''
79       if PLATFORM == "desktop"
80         @browser = setup_desktop_browser(str_browser)
81         @browser.manage.window.maximize if !@browser.nil?
82       else
83         @browser = setup_mobile_browser
84       end
85       return @browser
86     end
87 
88     # Description       : closes the current browser
89     # Author            : Chandra sekaran
90     #
91     def self.close_browser
92       @browser.close
93       $log.success("Current browser closed successfully")
94       $log_env.success("Current browser closed successfully")
95     rescue Exception => ex
96       $log.error("Error while closing the current browser - #{ex}")
97       exit
98     end
99 
100     # Description       : closes the current browser
101     # Author            : Chandra sekaran
102     #
103     def self.quit_browser
104       @browser.quit
105       $log_env.success("Current browser closed successfully")
106     rescue Exception => ex
107       $log.error("Error while closing the current browser - #{ex}")
108       exit
109     end
110 
111     # Description       : restarts the current browser
112     # Author            : Chandra sekaran
113     # Return value      :
114     #   @browser        : browser object of the new browser
115     #
116     def self.restart_browser
117       quit_browser
118       @browser_name = BROWSER
119       $log.info("Restarting the browser (#{@browser_name})")
120       @browser = browser_setup(@browser_name)
121       @browser
122     rescue Exception => ex
123       $log.error("Error while restarting browser - #{ex}")
124       exit
125     end
126 
127     # Description       : launches the desktop browser
128     # Author            : Chandra sekaran
129     # Arguments         :
130     #   str_browser     : browser name
131     # Return value      :
132     #   @browser        : browser object of the new browser
133     #
134     def self.setup_desktop_browser(str_browser)
135       @browser = nil
136       case str_browser.downcase
137         when "internet_explorer"
138           @browser = Selenium::WebDriver.for :internet_explorer
139 
140         when "firefox"
141           @browser = Selenium::WebDriver.for :firefox
142 
143         when "chrome"
144           caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => ["test-type"]})
145           @browser = Selenium::WebDriver.for :chrome, desired_capabilities: caps
146 
147           # if you want to set the download path to your local framework directory, then you can use the below
148           # selenium capabilities, which will set the current download directory to your current test result directory
149           #caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => ["test-type" ]})
150           #prefs = {
151           #  :download => {
152           #    :prompt_for_download => false,
153           #    :default_directory => File.expand_path($current_log_dir)
154           #  }
155           #}
156           #@browser = Selenium::WebDriver.for :chrome, :prefs => prefs, desired_capabilities: caps
157 
158         when "safari"
159           @browser = Selenium::WebDriver.for :safari
160 
161         when "ios"
162           if RUBY_PLATFORM.downcase.include?("darwin")
163             @browser = Selenium::WebDriver.for :iphone
164           else
165             raise "You can't run IOS tests on non-mac machine"
166           end
167         else
168           raise "Could not determine the browser - #{str_browser}"
169       end
170       $log.success("Successfully launched desktop #{str_browser} browser (Version : #{@browser.capabilities[:version]}, Resolution : '#{@browser.execute_script('return screen.width')}x#{@browser.execute_script('return screen.height')}')")
171       return @browser
172     rescue Exception => ex
173       $log.error("Error in starting desktop browser #{str_browser} : " + ex)
174       exit
175     end
176 
177     # Description       : saves the screenshot of the webpage as png file
178     # Author            : Chandra sekaran
179     # Arguments         :
180     #   str_module_name : module name under features directory
181     #
182     def self.capture_screenshot(str_module_name)
183       str_imgdir_path = "#{$current_log_dir}/screenshot"
184       unless File.directory?(str_imgdir_path)   # creates a new directory
185         FileUtils.mkdir_p(str_imgdir_path)
186       end
187       str_image = "/#{str_module_name}_#{Time.now.strftime(DATETIME_FORMAT)}.png"
188       str_imgdir_path << str_image    # adds image file name to the directory
189       @browser.save_screenshot(str_imgdir_path)       # saves the screenshot image to the directory
190       $log.info("Screenshot is saved in #{str_imgdir_path}")
191       return "screenshot#{str_image}"
192     rescue Exception => ex
193       $log.error("Error in taking screenshot for #{str_imgdir_path} : #{ex}")
194       exit
195     end
196 
197     # Description       : launches the mobile browser
198     # Author            : Chandra sekaran
199     # Arguments         :
200     #   @browser        : browser object of the mobile browser
201     #
202     def self.setup_mobile_browser
203       bool_device_enabled = DEVICE.nil? ? false : (DEVICE.downcase.eql?("true")? true : false)
204       case(BROWSER.downcase)
205         when "android", "chrome", "browser"
206           @browser = setup_android(bool_device_enabled, BROWSER)
207         when "safari"
208           if is_emulator_enabled
209             caps = {
210                 :platform           => 'Mac',
211                 :device             => 'iPhone Simulator',
212                 :browser_name       => 'iOS',
213                 :version            => '7.1',
214                 :app                => 'safari',
215                 :newCommandTimeout  => 60000,
216                 :javascript_enabled => true
217             }
218             client = Selenium::WebDriver::Remote::Http::Default.new
219             client.timeout = 1000 # seconds
220             @browser = Selenium::WebDriver.for :remote, :url => app_url , :desired_capabilities => caps , :http_client => client
221           end
222         else
223           raise "Invalid browser name : #{BROWSER}"
224       end
225       return @browser
226     rescue Exception => ex
227       $log.error("Error while setting up mobile browser : #{ex}")
228       exit
229     end
230 
231     # Description       : launches mobile browser
232     # Author            : Chandra sekaran
233     # Arguments         :
234     #   str_module_name : module name under features directory
235     #
236     def self.setup_android(bool_device_enabled, str_browser_name)
237       browser = ""
238       if str_browser_name.downcase == "chrome"
239         browser_name = "Chrome"
240       elsif str_browser_name.downcase == "browser" || str_browser_name.downcase == "android"
241         browser_name = "Browser"
242       else
243         raise "Profile for '#{str_browser_name}' browser does not exists"
244       end
245 
246       if bool_device_enabled
247         caps = { :browserName => browser_name, :platformName => "Android", :newCommandTimeout => 60000, :deviceName => "4D00C0124B174161" }
248         client = Selenium::WebDriver::Remote::Http::Default.new
249         client.timeout = 5000
250         browser = Selenium::WebDriver.for(:remote, :url => "http://localhost:4723/wd/hub/", :http_client => client, :desired_capabilities => caps)
251       else
252         caps = { :browserName => browser_name, :platformName => "Android", :Version => "4.4.2", :deviceName => "EMULATOR-5554" }
253         client = Selenium::WebDriver::Remote::Http::Default.new
254         client.timeout = 5000
255         browser = Selenium::WebDriver.for(:remote, :url => "http://localhost:4723/wd/hub/", :http_client => client, :desired_capabilities => caps)
256       end
257       $log.success("Launched #{str_browser_name} in Android #{bool_device_enabled ? 'device' : 'emulator'} successfully")
258       browser
259     rescue Exception => ex
260       $log.error("Error while setting Android browser profile for '#{str_browser_name}' : #{ex}")
261       exit
262     end
263 
264   end
265 end

Generated on 2015-05-08 10:40:30 +0530 with SimpleCov-RCov 0.2.3