Skip to content

Commit

Permalink
Merge pull request #78 from spriteCloud/development
Browse files Browse the repository at this point in the history
Development version 2.0.0
  • Loading branch information
barzilay authored Jul 14, 2017
2 parents 39718bf + ae28381 commit 973e6e6
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 81 deletions.
27 changes: 18 additions & 9 deletions lib/lapis_lazuli/browser/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def find(*args)
def multi_find_all(*args)
# Parse args into options
options = {
:mode => :match_one,
:mode => :match_any,
}
options = parse_find_options(options, *args)
throw_opt, options = do_throw?(options)
Expand Down Expand Up @@ -170,7 +170,7 @@ def pick_one(pick, elems)
:pick => :first,
:throw => true,
:mode => :match_one,
:error => nil,
:error => nil
}

##
Expand All @@ -185,7 +185,7 @@ def parse_find_options(options, *args)
# Verify/sanitize common options
if options.has_key? :mode
options[:mode] = options[:mode].to_sym
assert [:match_all, :match_one].include?(options[:mode]), ":mode needs to be one of :match_one or :match_all"
assert [:match_all, :match_one, :match_any].include?(options[:mode]), ":mode needs to be one of :match_one, :match_all or :match_any"
end

if options.has_key? :pick
Expand Down Expand Up @@ -349,9 +349,8 @@ def find_lambda(options)
def find_lambda_filtered(options)
options = options.dup

filter_by = options.fetch(:filter_by, nil)
filter_by = options.fetch(:filter_by, :present?)
options.delete(:filter_by)

options, inner = find_lambda(options)

# Wrap into filter function
Expand Down Expand Up @@ -420,15 +419,14 @@ def multi_find_lambda(options)
s, func = find_lambda_filtered(selector)
lambdas << func
end

# Depending on mode, we need to execute something slightly different
case options[:mode]
when :match_all
when :match_all, :match_any
return options, lambda {
all = []
lambdas.each do |func|
res = func.call
if 0 == res.length
if 0 == res.length and options[:mode] == :match_all
all = []
break
end
Expand Down Expand Up @@ -485,7 +483,18 @@ def dispatch_call(throw_opt, message, selectors, opts, func)
ret = func.call

if throw_opt and (ret.nil? or ret.length <= 0)
raise "Cannot find elements with selectors: #{selectors}"
msg = "Cannot find elements with selectors: "
if selectors[:selectors].length < 2
msg += "#{selectors[:selectors]}\n"
else
msg += "\n"
selectors[:selectors].each do |s|
msg += "- #{s} \n"
end
end
selectors.delete(:selectors)
msg += "With the options: #{selectors}"
raise msg
end

return ret
Expand Down
126 changes: 73 additions & 53 deletions lib/lapis_lazuli/browser/screenshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,90 @@
#

module LapisLazuli
module BrowserModule
module BrowserModule

##
# Screenshot functionality for browser
module Screenshots
##
# Returns the name of the screenshot, if take_screenshot is called now.
def screenshot_name(suffix="")
dir = world.env_or_config("screenshot_dir")
# Screenshot functionality for browser
module Screenshots
##
# Returns the name of the screenshot, if take_screenshot is called now.
def screenshot_name(suffix="")
dir = world.env_or_config("screenshot_dir")

# Generate the file name according to the new or old scheme.
case world.env_or_config("screenshot_scheme")
when "new"
# For non-cucumber cases: we don't have world.scenario.data
if not world.scenario.data.nil?
name = world.scenario.id
# Generate the file name according to the new or old scheme.
case world.env_or_config("screenshot_scheme")
when "new"
# For non-cucumber cases: we don't have world.scenario.data
if not world.scenario.data.nil?
name = world.scenario.id
end
# FIXME random makes this non-repeatable, sadly
name = "#{world.scenario.time[:iso_short]}-#{@browser.object_id}-#{name}-#{Random.rand(10000).to_s}.png"
else # 'old' and default
# For non-cucumber cases: we don't have world.scenario.data
if not world.scenario.data.nil?
name = world.scenario.data.name.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/, '_').squeeze('_')
end
name = world.time[:timestamp] + "_" + name + '.png'
end
# FIXME random makes this non-repeatable, sadly
name = "#{world.scenario.time[:iso_short]}-#{@browser.object_id}-#{name}-#{Random.rand(10000).to_s}.png"
else # 'old' and default
# For non-cucumber cases: we don't have world.scenario.data
if not world.scenario.data.nil?
name = world.scenario.data.name.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/, '_').squeeze('_')
end
name = world.time[:timestamp] + "_" + name + '.png'

# Full file location
fileloc = "#{dir}#{File::SEPARATOR}#{name}"

return fileloc
end

# Full file location
fileloc = "#{dir}#{File::SEPARATOR}#{name}"
##
# Taking a screenshot of the current page.
# Using the name as defined at the start of every scenario
def take_screenshot(suffix="")
# If the target directory does not exist, create it.
dir = world.env_or_config("screenshot_dir")
begin
Dir.mkdir dir
rescue SystemCallError => ex
# Swallow this error; it occurs (amongst other situations) when the
# directory exists. Checking for an existing directory beforehand is
# not concurrency safe.
end

return fileloc
end
fileloc = self.screenshot_name(suffix)

##
# Taking a screenshot of the current page.
# Using the name as defined at the start of every scenario
def take_screenshot(suffix="")
# If the target directory does not exist, create it.
dir = world.env_or_config("screenshot_dir")
begin
Dir.mkdir dir
rescue SystemCallError => ex
# Swallow this error; it occurs (amongst other situations) when the
# directory exists. Checking for an existing directory beforehand is
# not concurrency safe.
end
# Write screenshot
begin
# First make sure the window size is the full page size
if world.env_or_config("screenshots_height") == 'full'
original_height = @browser.window.size.height
width = @browser.window.size.width
target_height = @browser.execute_script('
var body = document.body,
html = document.documentElement;
return Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
')
if target_height > original_height
@browser.window.resize_to(width, target_height)
end
end

fileloc = self.screenshot_name(suffix)
# Save the screenshot
@browser.screenshot.save fileloc

# Write screenshot
begin
# Save the screenshot
@browser.screenshot.save fileloc
world.log.debug "Screenshot saved: #{fileloc}"
if world.env_or_config("screenshots_height") == 'full'
if target_height > original_height
@browser.window.resize_to(width, original_height)
end
end
world.log.debug "Screenshot saved: #{fileloc}"

# Try to store the screenshot name
if world.respond_to? :annotate
world.annotate :screenshot => fileloc
# Try to store the screenshot name
if world.respond_to? :annotate
world.annotate :screenshot => fileloc
end
rescue RuntimeError => e
world.log.debug "Failed to save screenshot to '#{fileloc}'. Error message #{e.message}"
end
rescue RuntimeError => e
world.log.debug "Failed to save screenshot to '#{fileloc}'. Error message #{e.message}"
return fileloc
end
return fileloc
end
end # module Screenshots
end # module BrowserModule
end # module Screenshots
end # module BrowserModule
end # module LapisLazuli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
default_env: production # defines the environment
default_device: desktop720 # See devices.yml for your options
close_browser_after: end # Can be `scenario`, `feature` or `end`
screenshots_height: full # When 'full' the window will be resized to max height before taking a screenshot

################################################################################
# List of error strings. `browser.html_error` will return an array of detected strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# validation_steps.rb is used to confirm that certain elements are displayed on the page.

Then(/text "([^"]*)" should display/) do |string|
Then(/^text "([^"]*)" should display$/) do |string|
# Note: The following is *really* slow, as it'll apply the regex to all
# elements in the page, one after the other. Of course, if any element
# includes the regex, all its parent elements also will, so you have
Expand Down
2 changes: 1 addition & 1 deletion lib/lapis_lazuli/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
# All rights reserved.
#
module LapisLazuli
VERSION = "1.1.0"
VERSION = "2.0.0"
end
2 changes: 1 addition & 1 deletion lib/lapis_lazuli/world/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def error(settings=nil)

# Include URL if we have a browser
if self.has_browser?
message += " [ #{self.browser.url} ]"
message += "\n---[ #{self.browser.url} ]---"
end

# Add the groups to the message
Expand Down
3 changes: 2 additions & 1 deletion test/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
################################################################################
# Set the global variables
default_env: production # defines the environment
default_device: desktop720 # set the default browser dimensions and/or user agent (See devices.yml)
default_device: desktop720 # set the default browser dimensions and/or user agent (See devices.yml)
screenshots_dir: screenshots # where to save the screenshots
screenshots_height: full # When 'full' the window will be resized to max height before taking a screenshot
step_pause_time: 1 # Waiting time in seconds defined after a step completes
make_screenshot_on_failed_scenario: true # make a screenshot after a scenario fails
old_portal: true
Expand Down
12 changes: 6 additions & 6 deletions test/features/bindings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ I want to run a webserver with some test files
And test if I can parse bindings when starting the browsers

@bindings_01
Scenario: Custom user-agent firefox
Scenario: bindings_01 - Custom user-agent firefox
Given I use browser bindings "1"
And I navigate to URL "http://whatsmyua.com/"
Then within 2 seconds I should see "CUSTOM-USER-AGENT"
And I close the browser

@bindings_02
Scenario: Custom user-agent chrome
Scenario: bindings_02 - Custom user-agent chrome
Given I use browser bindings "2"
And I navigate to URL "http://whatsmyua.com/"
Then within 2 seconds I should see "CUSTOM-CHROME-USER-AGENT"
And I close the browser

# Known issue with maximizing the window using the chrome option --start-maximized
@bindings_03 @maximize_issue
Scenario: Custom user-agent chrome
Scenario: bindings_03 - Custom user-agent chrome
Given I use browser bindings "3"
And I navigate to URL "http://whatsmyua.com/"
Then the browser window size should be "full screen"
And I close the browser

@bindings_04
Scenario: Using a pre-defined device (iphone5)
Scenario: bindings_04 - Using a pre-defined device (iphone5)
Given I restart the browser to device setting "iphone5"
When I navigate to URL "http://whatsmyua.com"
Then within 2 seconds I should see "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
Then within 2 seconds I should see "CPU iPhone OS 5_0 like Mac OS X"
And the browser window size should be "640x1136"
And I close the browser

@bindings_05
Scenario: Using a pre-defined device (desktop1080)
Scenario: bindings_05 - Using a pre-defined device (desktop1080)
Given I restart the browser to device setting "desktop1080"
When I navigate to URL "http://whatsmyua.com"
Then within 2 seconds I should see "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3" disappear
Expand Down
52 changes: 52 additions & 0 deletions test/features/multifind.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@multifind @p
Feature: testing multifind

@multifind01
Scenario: multifind01 - multifind with 1 results
Given I navigate to the find test page
Then the user expects a result in a multi_find lookup

@multifind02
Scenario: multifind02 - multifind no results
Given I navigate to the find test page
Then the user expects an error in a multi_find lookup

@multifind03
Scenario: multifind03 - multifind with no results and no error
Given I navigate to the find test page
Then the user expects no error in a multi_find lookup

@multifind04
Scenario: multifind04 - multifind all with 8 results
Given I navigate to the find test page
Then the user expects 8 results in a multi_find_all lookup

@multifind05
Scenario: multifind05 - multifind all with 1 results
Given I navigate to the find test page
Then the user expects 1 results in a multi_find_all lookup

@multifind06
Scenario: multifind06 - multifind all with 5 results
Given I navigate to the find test page
Then the user expects 5 existing results in a multi_find_all lookup

@multifind07
Scenario: multifind07 - multifind all with no results
Given I navigate to the find test page
Then the user expects an error in a multi_find_all lookup

@multifind08
Scenario: multifind08 - multifind all with no results and no error
Given I navigate to the find test page
Then the user expects no error in a multi_find_all lookup

@multifind09
Scenario: multifind09 - multifind all natching all elements with no results
Given I navigate to the find test page
Then the user expects an error in a multi_find_all lookup matching all elements

@multifind10
Scenario: multifind10 - multifind all natching all elements with no error
Given I navigate to the find test page
Then the user expects no error in a multi_find_all lookup matching all elements
35 changes: 35 additions & 0 deletions test/features/screenshot.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@screenshot @p
Feature: Screenshot
When I want to test the Lapis Lazuli library
I want to run a webserver with some test files
And execute the each library function searches for elements.

@screenshot_01
Scenario: screenshot_01 - Successfull scenario with screenshots
Given I navigate to the screenshot test page
When I take a screenshot
Then I take a screenshot

@screenshot_02
Scenario: screenshot_02 - Taking a screenshot on a failed scenario
Given I navigate to the screenshot test page
Then I fail this step

@screenshot_03
Scenario Outline: screenshot_03 - Successfull scenario outlines with screenshots
Given I navigate to the screenshot test page
When I take a screenshot
Then I take a screenshot
Examples:
| fails |
| failing once |
| failing twice |

@screenshot_04
Scenario Outline: screenshot_04 - Failing in a scenario outline
Given I navigate to the screenshot test page
Then I fail this step
Examples:
| fails |
| failing once |
| failing twice |
Loading

0 comments on commit 973e6e6

Please sign in to comment.