Commit 9320f82a18c2a69a1098225906c8275d46c67c35

Authored by Playtenders
1 parent ae10f7b3

rename files

Showing 1 changed file with 411 additions and 0 deletions
  1 +# -*- coding: utf-8 -
  2 +import re
  3 +import copy
  4 +import urllib
  5 +import urllib3
  6 +import string
  7 +
  8 +import dateutil.parser
  9 +from iso8601 import parse_date
  10 +from robot.libraries.BuiltIn import BuiltIn
  11 +from datetime import datetime, timedelta
  12 +import pytz
  13 +
  14 +TZ = pytz.timezone('Europe/Kiev')
  15 +
  16 +def get_library():
  17 + return BuiltIn().get_library_instance('Selenium2Library')
  18 +
  19 +
  20 +def get_webdriver_instance():
  21 + return get_library()._current_browser()
  22 +
  23 +
  24 +# return of variable is None
  25 +def get_variable_is_none(variable):
  26 + if variable is None:
  27 + return True
  28 + return False
  29 +
  30 +
  31 +# run specified keyword if condition is not none type
  32 +def run_keyword_if_condition_is_not_none(condition, name, *args):
  33 + if get_variable_is_none(condition) == False:
  34 + BuiltIn().run_keyword(name, *args)
  35 +
  36 +
  37 +# run specified keyword if condition is none type
  38 +def run_keyword_if_condition_is_none(condition, name, *args):
  39 + if get_variable_is_none(condition) == True:
  40 + BuiltIn().run_keyword(name, *args)
  41 +
  42 +
  43 +# return value for *keys (nested) in `element` (dict).
  44 +def get_from_dictionary_by_keys(element, *keys):
  45 + if not isinstance(element, dict):
  46 + raise AttributeError('keys_exists() expects dict as first argument.')
  47 + if len(keys) == 0:
  48 + raise AttributeError('keys_exists() expects at least two arguments, one given.')
  49 +
  50 + _element = element
  51 + for key in keys:
  52 + try:
  53 + _element = _element[key]
  54 + except KeyError:
  55 + return None
  56 + return _element
  57 +
  58 +
  59 +# returns if element exists on page. optimization
  60 +def get_is_element_exist(locator):
  61 + jquery_locator = convert_locator_to_jquery(locator)
  62 + if get_variable_is_none(jquery_locator) == False:
  63 + jquery_locator = jquery_locator.replace('"', '\\"')
  64 + length = get_webdriver_instance().execute_script('return $("' + jquery_locator + '").length;')
  65 + return length > 0
  66 +
  67 + try:
  68 + get_library()._element_find(locator, None, True)
  69 + except Exception:
  70 + return False
  71 + return True
  72 +
  73 +
  74 +# click
  75 +def js_click_element(locator):
  76 + element = get_library()._element_find(locator, None, True)
  77 + get_webdriver_instance().execute_script(
  78 + 'var $el = jQuery(arguments[0]); if($el.length) $el.click();',
  79 + element
  80 + )
  81 +
  82 +
  83 +# convert locator to jquery locator
  84 +def convert_locator_to_jquery(locator):
  85 + locator_params = locator.split('=', 1)
  86 + if locator_params[0] == 'id':
  87 + return '#' + locator_params[1]
  88 + if locator_params[0] == 'jquery':
  89 + return locator_params[1]
  90 + if locator_params[0] == 'css':
  91 + return locator_params[1]
  92 + return None
  93 +
  94 +
  95 +# set scroll to element in view
  96 +def set_element_scroll_into_view(locator):
  97 + element = get_library()._element_find(locator, None, True)
  98 + get_webdriver_instance().execute_script(
  99 + 'var $el = jQuery(arguments[0]); if($el.length) $el.get(0).scrollIntoView();',
  100 + element
  101 + )
  102 +
  103 +
  104 +# return text/value by specified locator
  105 +def get_value_by_locator(locator):
  106 + element = get_library()._element_find(locator, None, True)
  107 + text = get_webdriver_instance().execute_script(
  108 + 'var $element = jQuery(arguments[0]);'
  109 + 'if($element.is("input[type=checkbox]")) return $element.is(":checked") ? "1":"0";'
  110 + 'if($element.is("input,textarea,select")) return $element.val();'
  111 + 'return $element.text();',
  112 + element
  113 + )
  114 + return text
  115 +
  116 +
  117 +# input text to hidden input
  118 +def input_text_to_hidden_input(locator, text):
  119 + element = get_library()._element_find(locator, None, True)
  120 + get_webdriver_instance().execute_script(
  121 + 'jQuery(arguments[0]).val("' + text.replace('"', '\\"') + '");',
  122 + element
  123 + )
  124 +
  125 +
  126 +# select option by label for hidden select
  127 +def select_from_hidden_list_by_label(locator, label):
  128 + element = get_library()._element_find(locator, None, True)
  129 + get_webdriver_instance().execute_script(
  130 + 'var $option = jQuery("option:contains(' + label.replace('"', '\\"') + ')", arguments[0]);' +
  131 + 'if($option.length) jQuery(arguments[0]).val($option.attr("value"));',
  132 + element
  133 + )
  134 +
  135 +
  136 +# trigger change event for input by locator
  137 +def trigger_input_change_event(locator):
  138 + element = get_library()._element_find(locator, None, True)
  139 + get_webdriver_instance().execute_script(
  140 + 'var $el = jQuery(arguments[0]); if($el.length) $el.trigger("change");',
  141 + element
  142 + )
  143 +
  144 +
  145 +# convert all numners to string
  146 +def convert_float_to_string(number):
  147 + return repr(float(number))
  148 +
  149 +
  150 +def convert_esco__float_to_string(number):
  151 + return '{0:.5f}'.format(float(number))
  152 +
  153 +
  154 +def convert_float_to_string_3f(number):
  155 + return '{0:.3f}'.format(float(number))
  156 +
  157 +
  158 +# convert any variable to specified type
  159 +def convert_to_specified_type(value, type):
  160 + value = "%s" % (value)
  161 + if type == 'integer':
  162 + value = value.split()
  163 + value = ''.join(value)
  164 + print(value)
  165 + value = int(value)
  166 + if type == 'float':
  167 + value = value.split()
  168 + value = ''.join(value)
  169 + print(value)
  170 + value = float(value)
  171 + return value
  172 +
  173 +
  174 +# prepare isodate in needed format
  175 +def isodate_format(isodate, format):
  176 + iso_dt = parse_date(isodate)
  177 + return iso_dt.strftime(format)
  178 +
  179 +
  180 +def procuring_entity_name(tender_data):
  181 + tender_data.data.procuringEntity['name'] = u"ТОВ \"ПлейТендер\""
  182 + tender_data.data.procuringEntity['name_en'] = u"TOV \"playtender\""
  183 + tender_data.data.procuringEntity.identifier['id'] = u"1234567890-playtender"
  184 + tender_data.data.procuringEntity.identifier['legalName'] = u"ТОВ \"ПлейТендер\""
  185 + tender_data.data.procuringEntity.identifier['legalName_en'] = u"TOV \"playtender\""
  186 + if 'address' in tender_data.data.procuringEntity:
  187 + tender_data.data.procuringEntity.address['region'] = u"м. Київ"
  188 + tender_data.data.procuringEntity.address['postalCode'] = u"123123"
  189 + tender_data.data.procuringEntity.address['locality'] = u"Київ"
  190 + tender_data.data.procuringEntity.address['streetAddress'] = u"address"
  191 + if 'contactPoint' in tender_data.data.procuringEntity:
  192 + tender_data.data.procuringEntity.contactPoint['name'] = u"Test ЗамовникОборони"
  193 + tender_data.data.procuringEntity.contactPoint['name_en'] = u"Test"
  194 + tender_data.data.procuringEntity.contactPoint['email'] = u"chuzhin@mail.ua"
  195 + tender_data.data.procuringEntity.contactPoint['telephone'] = u"+3801111111111"
  196 + tender_data.data.procuringEntity.contactPoint['url'] = u"http://playtender.com.ua"
  197 + if 'buyers' in tender_data.data:
  198 + tender_data.data.buyers[0]['name'] = u"ТОВ \"ПлейТендер\""
  199 + tender_data.data.buyers[0].identifier['id'] = u"1234567890-playtender"
  200 + tender_data.data.buyers[0].identifier['legalName'] = u"ТОВ \"ПлейТендер\""
  201 + return tender_data
  202 +
  203 +# prepare data
  204 +def prepare_procuring_entity_data(data):
  205 + try:
  206 + data['name'] = u"Playtender"
  207 + data.identifier['id'] = u"playtender"
  208 + data.identifier['legalName'] = u"Playtender"
  209 + data.identifier['scheme'] = u"UA-EDR"
  210 + if 'name_en' in data:
  211 + data['name_en'] = u"Playtender"
  212 + if 'legalName_en' in data.identifier:
  213 + data.identifier['legalName_en'] = u"Playtender"
  214 + if 'address' in data:
  215 + data.address['countryName'] = u"Україна"
  216 + data.address['locality'] = u"Київ"
  217 + data.address['postalCode'] = u"01111"
  218 + data.address['region'] = u"місто Київ"
  219 + data.address['streetAddress'] = u"вулиця Тестова, 220, 8"
  220 + if 'contactPoint' in data:
  221 + data.contactPoint['email'] = u"chuzhin@mail.ua"
  222 + data.contactPoint['faxNumber'] = u"+3801111111111"
  223 + data.contactPoint['telephone'] = u"+3801111111111"
  224 + data.contactPoint['name'] = u"Test"
  225 + if 'name_en' in data.contactPoint:
  226 + data.contactPoint['name_en'] = u"Test"
  227 + data.contactPoint['url'] = u"https://playtender.com.ua"
  228 + except Exception:
  229 + raise Exception('data is not a dictionary')
  230 +
  231 +
  232 +# prepare data
  233 +def prepare_buyers_data(data):
  234 + if type(data) is not list:
  235 + raise Exception('data is not a list')
  236 +
  237 + # preventing console errors about changing buyer data in cases
  238 + if len(data) != 1:
  239 + return
  240 +
  241 + item = next(iter(data), None)
  242 + item['name'] = u"Playtender"
  243 + item.identifier['id'] = u"playtender"
  244 + item.identifier['legalName'] = u"Playtender"
  245 + item.identifier['scheme'] = u"UA-EDR"
  246 +
  247 +
  248 +# prepare dictionary from field path + value
  249 +def generate_dictionary_from_field_path_and_value(path, value):
  250 + data = dict()
  251 + path_keys_list = path.split('.')
  252 + if len(path_keys_list) > 1:
  253 + key = path_keys_list.pop(0)
  254 + value = generate_dictionary_from_field_path_and_value('.'.join(path_keys_list), value)
  255 + indexRegex = re.compile(r'(\[(\d+)\]$)')
  256 + matchObj = indexRegex.search(key)
  257 + print matchObj
  258 + if matchObj:
  259 + key = indexRegex.sub('', key)
  260 + value['list_index'] = matchObj.group(2)
  261 + value = [value]
  262 + data[key] = value
  263 + else:
  264 + data = dict()
  265 + data[path] = value
  266 + return data
  267 +
  268 +
  269 +# Percentage conversion
  270 +def multiply_hundred(number):
  271 + return number * 100
  272 +
  273 +
  274 +# prepares data for filling form in easiest way
  275 +def prepare_tender_data(data_original):
  276 + # preventing change data in global view
  277 + data = copy.deepcopy(data_original)
  278 +
  279 + # check if data is for multilot
  280 + if 'lots' not in data:
  281 + return data
  282 +
  283 + # moves features to its related items
  284 + if 'features' in data:
  285 + i = 0
  286 + l = len(data['features'])
  287 + while i < l:
  288 + if data['features'][i]['featureOf'] == 'lot':
  289 + for lot in data['lots']:
  290 + if lot['id'] == data['features'][i]['relatedItem']:
  291 + if 'features' not in lot:
  292 + lot['features'] = []
  293 + lot['features'].append(data['features'].pop(i))
  294 + l = l - 1
  295 + i = i - 1
  296 + break
  297 + if data['features'][i]['featureOf'] == 'item':
  298 + for item in data['items']:
  299 + if item['id'] == data['features'][i]['relatedItem']:
  300 + if 'features' not in item:
  301 + item['features'] = []
  302 + item['features'].append(data['features'].pop(i))
  303 + l = l - 1
  304 + i = i - 1
  305 + break
  306 + i = i + 1
  307 +
  308 + if 'features' in data:
  309 + if len(data['features']) == 0:
  310 + del data['features']
  311 +
  312 + # moves items to its related lots
  313 + i = 0
  314 + l = len(data['items'])
  315 + while i < l:
  316 + for lot in data['lots']:
  317 + if lot['id'] == data['items'][i]['relatedLot']:
  318 + if 'items' not in lot:
  319 + lot['items'] = []
  320 + lot['items'].append(data['items'].pop(i))
  321 + l = l - 1
  322 + i = i - 1
  323 + break
  324 + i = i + 1
  325 +
  326 + del data['items']
  327 +
  328 + if 'milestones' not in data:
  329 + return data
  330 + # moves milestones to its related lots
  331 + i = 0
  332 + l = len(data['milestones'])
  333 + while i < l:
  334 + for lot in data['lots']:
  335 + if lot['id'] == data['milestones'][i]['relatedLot']:
  336 + if 'milestones' not in lot:
  337 + lot['milestones'] = []
  338 + lot['milestones'].append(data['milestones'].pop(i))
  339 + l = l - 1
  340 + i = i - 1
  341 + break
  342 + i = i + 1
  343 +
  344 + del data['milestones']
  345 +
  346 + return data
  347 +
  348 +
  349 +def split_agreementDuration(str, type):
  350 + if type in 'year':
  351 + year_temp = str.split('Y', 1)
  352 + value = year_temp[0].split('P', 1)
  353 + elif type in 'month':
  354 + month_temp = str.split('M', 1)
  355 + value = month_temp[0].split('Y', 1)
  356 + else:
  357 + day_temp = str.split('D', 1)
  358 + value = day_temp[0].split('M', 1)
  359 + return value[1]
  360 +
  361 +
  362 +def convert_date_to_string_contr(date):
  363 + date = dateutil.parser.parse(date)
  364 + date = date.strftime("%d.%m.%Y %H:%M:%S")
  365 + return date
  366 +
  367 +def get_value_minimalStepPercentage(value):
  368 + value = value / 100
  369 + return value
  370 +
  371 +def set_value_minimalStepPercentage(value):
  372 + value = value * 100
  373 + return value
  374 +
  375 +def convert_esco__float_to_string(number):
  376 + return '{0:.5f}'.format(float(number))
  377 +
  378 +def convert_string_to_float(number):
  379 + return float(number)
  380 +
  381 +def download_file(url, file_name, output_dir):
  382 + urllib.urlretrieve(url, ('{}/{}'.format(output_dir, file_name)))
  383 +
  384 +def parse_complaintPeriod_date(date_string):
  385 + date_str = datetime.strptime(date_string, "%d.%m.%Y %H:%M")
  386 + date = datetime(date_str.year, date_str.month, date_str.day, date_str.hour, date_str.minute, date_str.second,
  387 + date_str.microsecond)
  388 + date = TZ.localize(date).isoformat()
  389 + return date
  390 +
  391 +def parse_deliveryPeriod_date1(date):
  392 + date = dateutil.parser.parse(date)
  393 + date = date.strftime("%d.%m.%Y")
  394 + return date
  395 +
  396 +def parse_deliveryPeriod_date(date_string):
  397 +# date_str = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S+03:00")
  398 + if '+03' in date_string:
  399 + date_str = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S+03:00")
  400 + else:
  401 + date_str = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S+02:00")
  402 + date = datetime(date_str.year, date_str.month, date_str.day)
  403 + date = date.strftime("%d.%m.%Y")
  404 + return date
  405 +
  406 +def split_joinvalue(str_value):
  407 + str_value = str_value.split()
  408 + str_value = ''.join(str_value)
  409 + print(str_value)
  410 + str_value.replace(" ", "")
  411 + return str_value
Please register or login to post a comment