· 5 years ago · Jun 01, 2020, 03:16 PM
1$main_big_boom_address = "https://snaptest.ps.surfstudio.ru/api/"
2$api_create_execution = "create_execution/"
3$api_create_step = "create_step/"
4$api_compare = "compare/"
5
6#запрос POST возвращающий весь response
7def request_post(address, json = {})
8 begin
9 headers = {content_type: :json, accept: :json, Authorization: $token}
10 RestClient::Request.execute(
11 :url => $main_big_boom_address + address,
12 :method => :post,
13 :headers => headers,
14 :payload => json.to_json,
15 :verify_ssl => false
16 )
17 rescue RestClient::ExceptionWithResponse => e
18 if e.code == 401
19 fail("Указан недействительный Api-Key")
20 else
21 fail("Запрос завершился не успешно: #{e.code}")
22 end
23 end
24end
25
26# формируем json и отправляем post запрос create_execution, полученный execution_id сохраняем в глобальную переменную
27def create_execution_base(execution_name, device, app_version, os_version)
28 object = {
29 'execution_name' => execution_name.to_s,
30 'device' => device.to_s,
31 'app_version' => app_version.to_s,
32 'os_version' => os_version.to_s
33 }
34 get_execution_data = request_post($api_create_execution, object)
35 execution_data = JSON.parse(get_execution_data)
36 $execution_id = execution_data['execution_id']
37end
38
39# формируем json и отправляем post запрос create_step, полученный step_id сохраняем в глобальную переменную
40def create_step_base(execution_id, step_name, device, resolution)
41 object = {
42 'execution_id' => execution_id,
43 'step_name' => step_name.to_s,
44 'device' => device.to_s,
45 'resolution' => resolution.to_s
46 }
47
48 get_step_data = request_post($api_create_step, object)
49 body_step_data = JSON.parse(get_step_data)
50 $step_id = body_step_data['step_id']
51end
52
53# формируем json и отправляем post запрос compare
54def compare_base(step_id, screenshot)
55 object = {
56 'step_id' => step_id.to_s,
57 'photo' => screenshot.to_s,
58 }
59 request_post($api_compare, object)
60end
61
62# создаём прогон с передаваемым названием и сохраняем его id в глобальную переменную
63def create_execution(execution_name)
64 get_api_key
65 $common.get_device_data
66 $execution_id = create_execution_base(execution_name, $device_name, $app_version, $os_version)
67end
68
69# создаём шаг, делаем скриншот с устройства и запускаем процесс сравнения на сервере
70def create_test_step(step_name)
71 create_step(step_name)
72 screenshot_path = take_screenshot(step_name)
73 img_base64 = img_to_base64(screenshot_path)
74 compare(img_base64)
75end
76
77# создаём шаг в созданном прогоне с передаваемым названием и сохраняем его id в глобальную переменную
78def create_step(execution_id = $execution_id, step_name)
79 $step_id = create_step_base(execution_id, step_name, $device_name, $resolution)
80end
81
82# запускаем процесс сравнения на бэке, отправляем айди шага и скриншот для сравнения
83def compare(step_id = $step_id, screenshot)
84 compare_base(step_id, screenshot)
85end
86
87# достаём из файла api key и сохраняем его в глобальную переменную
88def get_api_key
89 file_path = "./api_key.txt"
90 if File.exist?(file_path)
91 file = File.new("./api_key.txt","r:UTF-8")
92 api_key = file.readlines
93 $token = "Api-Key #{api_key[0]}"
94 file.close
95 else
96 fail("Файл с Api Key не найден.")
97 end
98end
99
100# сделать скриншот и записать его куда надо
101def take_screenshot(name)
102 path = './big_boom/'
103 name = "#{name}.png"
104 screenshot_path = screenshot(:prefix => path, :name => name)
105end
106
107# на входе получаем фото и форматируем его в base64
108def img_to_base64(screenshot_path)
109 base64_image =
110 File.open(screenshot_path, "rb") do |file|
111 Base64.strict_encode64(file.read)
112 end
113 return base64_image
114end