Skip to content

Commit 2a890d4

Browse files
committed
finished faker
1 parent 7cb9517 commit 2a890d4

File tree

5 files changed

+131
-4
lines changed

5 files changed

+131
-4
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
require 'faker'
2+
require 'kraken-mobile/utils/k'
3+
require 'json'
4+
5+
class KrakenFaker
6+
#-------------------------------
7+
# Fields
8+
#-------------------------------
9+
attr_accessor :process_id
10+
11+
#-------------------------------
12+
# Constructors
13+
#-------------------------------
14+
def initialize(process_id:)
15+
raise 'ERROR: Can\'t create faker for null process id' if process_id.nil?
16+
17+
@process_id = process_id
18+
end
19+
20+
#-------------------------------
21+
# Helpers
22+
#-------------------------------
23+
def generate_value_for_key(key:)
24+
value = if key.start_with?('$name')
25+
generate_name
26+
elsif key.start_with?('$number')
27+
generate_number
28+
elsif key.start_with?('$email')
29+
generate_email
30+
elsif key.start_with?('$string')
31+
generate_string
32+
elsif key.start_with?('$date')
33+
generate_date
34+
else
35+
raise 'ERROR: Faker key not supported'
36+
end
37+
save_key_value_in_dictionary(key: key, value: value)
38+
value
39+
end
40+
41+
def reuse_value_for_key(key:)
42+
dictionary = dictionary_json
43+
key = key.delete_prefix('$')
44+
45+
raise 'ERROR: Key does not exist' if dictionary[process_id.to_s].nil?
46+
if dictionary[process_id.to_s][key.to_s].nil?
47+
raise 'ERROR: Key does not exist'
48+
end
49+
50+
dictionary[process_id.to_s][key.to_s]
51+
end
52+
53+
def dictionary_json
54+
create_dictionary_json_file unless dictionary_json_file_exists?
55+
56+
absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH)
57+
file = open(absolute_dictionary_path)
58+
content = file.read
59+
JSON.parse(content)
60+
end
61+
62+
def create_dictionary_json_file
63+
absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH)
64+
File.open(absolute_dictionary_path, 'w') do |f|
65+
f.puts({}.to_json)
66+
end
67+
end
68+
69+
def dictionary_json_file_exists?
70+
absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH)
71+
File.file?(absolute_dictionary_path)
72+
end
73+
74+
def save_key_value_in_dictionary(key:, value:)
75+
current_json = dictionary_json
76+
current_json[process_id.to_s] = {} if current_json[process_id.to_s].nil?
77+
current_json[process_id.to_s][key.to_s] = value
78+
79+
absolute_dictionary_path = File.expand_path(K::DICTIONARY_PATH)
80+
open(absolute_dictionary_path, 'w') do |f|
81+
f.puts(current_json.to_json)
82+
end
83+
end
84+
85+
#-------------------------------
86+
# Generators
87+
#-------------------------------
88+
def generate_name
89+
Faker::Name.name
90+
end
91+
92+
def generate_number
93+
Faker::Number.number(digits: rand(10))
94+
end
95+
96+
def generate_email
97+
Faker::Internet.email
98+
end
99+
100+
def generate_string
101+
Faker::String.random(length: rand(100))
102+
end
103+
104+
def generate_date
105+
Faker::Date.in_date_period.to_s
106+
end
107+
end

lib/kraken-mobile/steps/general_steps.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'kraken-mobile/helpers/kraken_faker'
2+
13
ParameterType(
24
name: 'property',
35
regexp: /[^\"]*/,
@@ -7,6 +9,8 @@
79
string.slice!('<')
810
string.slice!('>')
911
handle_property(string)
12+
elsif string_is_a_faker_reuse?(string)
13+
handle_faker_reuse(string)
1014
elsif string_is_a_faker?(string)
1115
handle_faker(string)
1216
else
@@ -31,7 +35,11 @@ def string_is_a_property?(string)
3135
end
3236

3337
def string_is_a_faker?(string)
34-
string.start_with?('$') || string.start_with?('$$')
38+
string.start_with?('$')
39+
end
40+
41+
def string_is_a_faker_reuse?(string)
42+
string.start_with?('$$')
3543
end
3644

3745
def handle_property(property)
@@ -59,6 +67,16 @@ def all_user_properties_as_json
5967
JSON.parse(content)
6068
end
6169

62-
def handle_faker(faker)
63-
nil
70+
def handle_faker(key)
71+
faker = KrakenFaker.new(process_id: current_process_id)
72+
faker.generate_value_for_key(
73+
key: key
74+
)
75+
end
76+
77+
def handle_faker_reuse(key)
78+
faker = KrakenFaker.new(process_id: current_process_id)
79+
faker.reuse_value_for_key(
80+
key: key
81+
)
6482
end

lib/kraken-mobile/steps/web/kraken_steps.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'selenium-webdriver'
2-
require 'faker'
32
require 'uri'
43

54
driver = Selenium::WebDriver.for :chrome

lib/kraken-mobile/test_scenario.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def initialize(kraken_app:, feature_file_path:)
3030
def before_execution
3131
delete_all_web_inboxes
3232
File.delete(K::DIRECTORY_PATH) if File.exist?(K::DIRECTORY_PATH)
33+
File.delete(K::DICTIONARY_PATH) if File.exist?(K::DICTIONARY_PATH)
3334
K::PROCESS_STATE_FILE_PATH.each do |_state, file_path|
3435
File.delete(file_path) if File.exist?(file_path)
3536
end
@@ -48,6 +49,7 @@ def run
4849

4950
def after_execution
5051
File.delete(K::DIRECTORY_PATH) if File.exist?(K::DIRECTORY_PATH)
52+
File.delete(K::DICTIONARY_PATH) if File.exist?(K::DICTIONARY_PATH)
5153
K::PROCESS_STATE_FILE_PATH.each do |_state, file_path|
5254
File.delete(file_path) if File.exist?(file_path)
5355
end

lib/kraken-mobile/utils/k.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module K
44
SEPARATOR = ';' unless defined? SEPARATOR
55
DIRECTORY_PATH = '.device_directory' unless defined? DIRECTORY_PATH
6+
DICTIONARY_PATH = 'dictionary.json' unless defined? DICTIONARY_PATH
67
INBOX_FILE_NAME = 'inbox.txt' unless defined? INBOX_FILE_NAME
78
FEATURES_PATH = './features' unless defined? FEATURES_PATH
89
DEFAULT_TIMEOUT_SECONDS = 30 unless defined? DEFAULT_TIMEOUT_SECONDS

0 commit comments

Comments
 (0)