Top Level Namespace

Defined Under Namespace

Modules: ADBDevices, ADBScreenShot, Base, CSVhelper, Config, ExcelHelper, HTTPHelper, Minitest, Shell, TXThelper, Tabby, Tabbyx Classes: BatchGetResponse, Hash

Constant Summary collapse

SERVER =

Setup global constants

USER =
PASSWORD =
APP =
DEVICE =
HOST =
USER_AGENT =

Relevant configurations for API Testing

"QDReaderAppStore/3.7.3 (iPhone; iOS 9.3; Scale/2.00)"
"QDHeader=NzRiMjdkYzIxMmY2NjNlYmRlNWI2NzdkY2RkYzRmNWJ8My43LjN8NzUwfDEzMzR8UWlZZXw5LjMwfDV8aVBob25lIE9TL2lQaG9uZS94ODZfNjR8MTY0fFFpWWV8Mw==; _ga=GA1.2.207823887.1473851687; cmfuToken=N((cfBPzO963RhWhcOx2uGENWANvs2qlZ4zKVpEK8Tlo1RKq3oztfJ4onZu5bFWY3-a4XjIMPYkQOmqWfUrob5VIX1i7EQP6OP6TaPc9tWnPtMxigj0_wm4hU3rxDO24U-ewbhHaG_Ibeca9uUrJXCZe9JkwBizrFwDXUJoCGAMbQPTJwGekSHeQeJWMJF0OlJzPmLXmcIHeoxQ_hrACayIuQTxkLaSAXwBUjkXdzXukBQXW7P5nVMQkkHvTfhs8WajMOuPuv5X1hh9I_M0xNG2CbtyChJBVxvVSKf8_yDOxHaLdEvglmHAEg2; stat_gid=8208333844"
NORMAL_SLEEP_SECS =

Wait durations

2
MID_SLEEP_SECS =
5
MAX_SLEEP_SECS =
10
MAX_WAIT_SECS =
30
MAX_TIME_OUT =
180
MAX_RETRY_TIMES =
3
MIN_WAIT =

for web

3
MID_WAIT =
5
MAX_WAIT =
10
WAIT_TIME_OUT =
60
NETWORK_WAIT =
5

Instance Method Summary collapse

Instance Method Details

#assert_screen_match(screen, x = 100, invert = false) ⇒ Object



123
124
125
126
127
128
# File 'lib/tabbyx/helpers/screenshot_helpers.rb', line 123

def assert_screen_match(screen,x = 100,invert = false)
  match = screen_match?(screen,x,invert)
  fail(msg="Screenshots differ too much.") if !match && !invert
  fail(msg="Screenshots differ too little.") if !match && invert
  true
end

#compare_screen(screen) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tabbyx/helpers/screenshot_helpers.rb', line 29

def compare_screen(screen)
  screen.tr!(" ", "_")
  dirs = screenshot_dirs

  screenshot_filename = screenshot({:prefix => "#{dirs[:check_dir]}/", :name => "#{screen}.png"})
  check_filename = "#{dirs[:check_dir]}/#{screen}.png"
  check_rotated_filename = "#{dirs[:check_dir]}/#{screen}_rotated.png"
  orig_filename = "#{dirs[:orig_dir]}/#{screen}.png"
  diff_filename = "#{dirs[:diff_dir]}/#{screen}.png"
  diff_rotated_filename = "#{dirs[:diff_dir]}/#{screen}_rotated.png"
  
  # Moving files is instant, sleep 1 sec is needed to ensure that the movement is done before compaing two images.
  FileUtils.mv(screenshot_filename,check_filename) # discard number, only keep one
  sleep 1

  if (File.exists? orig_filename)
    # If the two images don't have the same width and height, ImageMagic's compare method will throw an exception
    # get the original image's dimension
    orig_image_size = (%x[identify -format "%[fx:w]x%[fx:h]" "#{orig_filename}"]).chomp  # might be 1536 × 2048, 768x1024 or return error

    # get the dimension of the image that is to be checked
    check_image_size = (%x[identify -format "%[fx:w]x%[fx:h]" "#{check_filename}"]).chomp

    # occasionally identify method isn't reliable, you will get 0x0 above, the image's width and height should have 3-4 digits
    if /^(\d{3,4})x(\d{3,4})$/.match(orig_image_size).nil?
      fail(msg="Cannot get image size, please replace the original image")
    end

    if /^(\d{3,4})x(\d{3,4})$/.match(check_image_size).nil?
      fail(msg="the original screenshot's info isn't readable to ImageMagic, please check.")
    end

    # convert the check_image to the same size of the orig_image if necessary, so they are comparable 
    %x[convert "#{check_filename}" -resize #{orig_image_size} "#{check_filename}"] unless orig_image_size == check_image_size
    sleep 2
    # Create a rotated version of the screenshot
    # shell_res = %x[convert "#{check_filename}" -rotate 180 #{check_rotated_filename}]

    # Compare pixel-by-pixel  
    # -fuzz %1 is too strict, change it to 5%
    diff = (%x[compare -metric AE -fuzz 5% "#{orig_filename}" "#{check_filename}" "#{diff_filename}" 2>&1]).chomp
    diff_rotated = (%x[compare -metric AE -fuzz 5% "#{orig_filename}" "#{check_rotated_filename}" "#{diff_rotated_filename}" 2>&1]).chomp
    if (!diff.match(/^\d.*$/))
      fail(msg="Error comparing pictures. '#{diff}'")
    end
    
    # Use rotated file if it has a better match
    if (diff.to_f > diff_rotated.to_f)
      log "Using rotated screenshot instead, since it provides a better match"
      FileUtils.cp(check_rotated_filename,check_filename)
      FileUtils.cp(diff_rotated_filename,diff_filename)
      diff = diff_rotated
    end
    FileUtils.rm check_rotated_filename
    FileUtils.rm diff_rotated_filename
    
    pixel_count = (%x[convert "#{orig_filename}" -format "%[fx:w*h]" info:]).chomp # occasionally unreliable 
    percent_diff = 100*diff.to_f/pixel_count.to_f
    puts "Screenshots diff: #{percent_diff}"
    
    # Remove diff if no difference
    FileUtils.rm diff_filename if (percent_diff == 0)
    
  else
    # No original file. Use the new screenshot. Keep the one in "check".
    FileUtils.cp(check_filename,orig_filename)
    log "No original screenshot. New has been created at #{orig_filename}. Please check."
    percent_diff = 0
  end
  
  {
    :check_filename => check_filename,
    :orig_filename => orig_filename,
    :diff_filename => diff_filename,
    :percent_diff => percent_diff
  }
end

#remember_screen_as(screen) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tabbyx/helpers/screenshot_helpers.rb', line 17

def remember_screen_as(screen)
  screen.tr!(" ", "_")
  dirs = screenshot_dirs
  orig_filename = "#{dirs[:orig_dir]}/" + "#{screen}.png"
  capture_filename = screenshot({:prefix => "#{dirs[:orig_dir]}/", :name => "#{screen}.png"})
  puts orig_filename
  puts capture_filename
  if (orig_filename != capture_filename)
    FileUtils.mv(capture_filename,orig_filename) # calabash might add a counter to the file
  end
end

#screen_match?(screen, x = 100, invert = false, always_clean = false) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tabbyx/helpers/screenshot_helpers.rb', line 107

def screen_match?(screen, x = 100, invert = false, always_clean = false)
  res = compare_screen(screen)

  FileUtils.rm res[:check_filename] if always_clean

  return false if (!invert && (100-res[:percent_diff]) < x.to_f)
  return false if (invert && (100-res[:percent_diff]) >= x.to_f)

  # Remove check file if everything is ok
  FileUtils.rm res[:check_filename] if !always_clean
  
  true
end

#screen_not_match?(screen, x = 100) ⇒ Boolean

Returns:

  • (Boolean)


121
# File 'lib/tabbyx/helpers/screenshot_helpers.rb', line 121

def screen_not_match?(screen, x = 100) ; screen_match?(screen, x, true) end

#screenshot_dirsObject

This requires ImageMagick to be installed



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/tabbyx/helpers/screenshot_helpers.rb', line 3

def screenshot_dirs
  base_dir = screenshot_base_dir
  orig_dir = base_dir + 'orig'
  check_dir = base_dir + 'check'
  diff_dir = base_dir + 'diff'

  # Create directories if they don't exist
  FileUtils.mkdir_p(orig_dir)
  FileUtils.mkdir_p(check_dir)
  FileUtils.mkdir_p(diff_dir)

  {:orig_dir => orig_dir, :check_dir => check_dir, :diff_dir => diff_dir}
end