Module: Zap

Defined in:
lib/utils/zap.rb

Overview

Created on 22 Oct 2018 @author: Andy Perrett

Versions: 1.0 - Baseline

Zap.rb - Zap functions

Class Method Summary collapse

Class Method Details

.bObject

define browser value



94
95
96
# File 'lib/utils/zap.rb', line 94

def self.b
  browser = @browser
end

.browser_full_screenObject

makes the browser full screen.



86
87
88
89
90
91
# File 'lib/utils/zap.rb', line 86

def self.browser_full_screen
  screen_width = @browser.execute_script('return screen.width;')
  screen_height = @browser.execute_script('return screen.height;')
  @browser.driver.manage.window.resize_to(screen_width, screen_height)
  @browser.driver.manage.window.move_to(0, 0)
end

.browser_versionObject

Check browser version



99
100
101
102
103
# File 'lib/utils/zap.rb', line 99

def self.browser_version
  browserversion = @browser.driver.capabilities[:version]
rescue StandardError
  browserversion = 'No Browser version'
end

.check_save_screenshot(full_sc_dirname, screen_shot) ⇒ Object

create screenshot filename and save the screenshot if the test has failed or if explictly required



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/utils/zap.rb', line 107

def self.check_save_screenshot(full_sc_dirname, screen_shot)
  if ($currentTestFail || screen_shot)
    time = Time.now.strftime('%H%M')
    if ($currentTestFail)
      scFileName = full_sc_dirname + "/Test_step-#{$testStep}_Failed_"\
                                      "#{time}.png"
    else
      # file name will be teststep.png
      scFileName = full_sc_dirname + "/Test_step-#{$testStep}_#{time}.png"
    end

    # Screenshot capture for websites
    Browser.b.screenshot.save scFileName
    Report.results.puts("Screenshot saved to: #{scFileName}")
    else
      Report.results.puts 'No screenshot requested'
    end

  # if any issues with saving the screenshot then log a warning
  rescue StandardError => error
  # construct the error message from custom text and the actual system
  # error message (converted to a string).
  $log.puts("Error saving the screenshot: #{scFileName}   #{error.to_s}")
end

.chrome_headlessObject

chrome headless browser details



38
39
40
41
42
43
44
# File 'lib/utils/zap.rb', line 38

def self.chrome_headless
  @browser = Watir::Browser.new :chrome, switches: %w[
    --start-maximized --disable-gpu --headless --acceptInsecureCerts-true
    --no-sandbox --window-size=1920,1080
  ]
  browser_version
end

.firefoxObject

firefox browser details



47
48
49
50
51
52
53
54
# File 'lib/utils/zap.rb', line 47

def self.firefox
  caps = Selenium::WebDriver::Remote::Capabilities.firefox
  caps['acceptInsecureCerts'] = true
  driver = Selenium::WebDriver.for(:firefox, desired_capabilities: caps)
  @browser = Watir::Browser.new(driver)
  browser_full_screen
  browser_version
end

.firefox_headlessObject

firefox headless browser details



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/utils/zap.rb', line 57

def self.firefox_headless
  caps = Selenium::WebDriver::Remote::Capabilities.firefox
  options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless'])
  caps['acceptInsecureCerts'] = true
  driver = Selenium::WebDriver.for(:firefox, options: options,
                                  desired_capabilities: caps)
  @browser = Watir::Browser.new(driver)
  # makes the browser full screen.
  @browser.driver.manage.window.resize_to(1920, 1200)
  @browser.driver.manage.window.move_to(0, 0)
  browser_version
  # browser
end

.ieObject

ie browser details



72
73
74
75
76
# File 'lib/utils/zap.rb', line 72

def self.ie
  @browser = Watir::Browser.new :ie
  browser_full_screen
  browser_version
end

.run_zapObject

run zap security test



30
31
32
33
34
35
# File 'lib/utils/zap.rb', line 30

def self.run_zap
  @browser = Watir::Browser.new :chrome, switches: %w[
    --acceptInsecureCerts-true --start-maximized --window-size=1920,1080
  ]
  browser_version
end

.safariObject

sarfari headless browser details



79
80
81
82
83
# File 'lib/utils/zap.rb', line 79

def self.safari
  @browser = Watir::Browser.new :safari
  browser_full_screen
  browser_version
end

.zap_checkObject

require ‘./taf_config.rb’ zap_check function



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/utils/zap.rb', line 11

def self.zap_check
  security_test = $securityTest.downcase
  case security_test
  when 'yes'
    run_zap
  when 'no'
    puts "No Security Test required."
  else
    puts "unable to run security test: #{security_test}"
    raise SecurityFailedOpen
  end
rescue SecurityFailedOpen => error
  # construct the error message from custom text and the actual system error
  # message (converted to a string)
  error_text = "unable to run security test: #{security_test} " + error.to_s
  raise error_text
end