|
| 1 | +from browserstack.local import Local |
| 2 | +import json |
| 3 | +import csv |
| 4 | +import os |
| 5 | +from robot.libraries.BuiltIn import BuiltIn |
| 6 | +from selenium.webdriver.common.action_chains import ActionChains |
| 7 | +from selenium import webdriver |
| 8 | +from selenium.webdriver.chrome.options import Options |
| 9 | +from selenium.webdriver.common.keys import Keys |
| 10 | +import urllib3 |
| 11 | + |
| 12 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 13 | +bstack_local = 0 |
| 14 | +se2lib = BuiltIn().get_library_instance('SeleniumLibrary') |
| 15 | + |
| 16 | +user_data = '../../data/user.csv' |
| 17 | +product_data = '../../data/product.csv' |
| 18 | + |
| 19 | +def combine_dict(dict1, dict2): |
| 20 | + dict_1 = json.loads(str(dict1)) |
| 21 | + dict_2 = json.loads(str(dict2)) |
| 22 | + |
| 23 | + dict_1.update(dict_2) |
| 24 | + |
| 25 | + return dict_1 |
| 26 | + |
| 27 | + |
| 28 | +def get_caps_single(caps_path, username, access_key): |
| 29 | + |
| 30 | + with open(caps_path) as f: |
| 31 | + data = json.load(f) |
| 32 | + |
| 33 | + single_caps_common = data['tests']['single']['common_caps'] |
| 34 | + single_caps_env = data['tests']['single']['env_caps'][0] |
| 35 | + |
| 36 | + single_caps_common.update(single_caps_env) |
| 37 | + single_caps_common['browserstack.user'] = username |
| 38 | + single_caps_common['browserstack.key'] = access_key |
| 39 | + |
| 40 | + return single_caps_common |
| 41 | + |
| 42 | + |
| 43 | +def get_caps_local(caps_path, username, access_key): |
| 44 | + |
| 45 | + with open(caps_path) as f: |
| 46 | + data = json.load(f) |
| 47 | + |
| 48 | + caps_common = data['tests']['local']['common_caps'] |
| 49 | + caps_env = data['tests']['local']['env_caps'][0] |
| 50 | + |
| 51 | + caps_common.update(caps_env) |
| 52 | + caps_common['browserstack.user'] = username |
| 53 | + caps_common['browserstack.key'] = access_key |
| 54 | + |
| 55 | + return caps_common |
| 56 | + |
| 57 | + |
| 58 | +def start_local(key): |
| 59 | + |
| 60 | + global bstack_local |
| 61 | + bstack_local = Local() |
| 62 | + |
| 63 | + bstack_local_args = {"key": key} |
| 64 | + |
| 65 | + bstack_local.start(**bstack_local_args) |
| 66 | + |
| 67 | + return bstack_local |
| 68 | + |
| 69 | + |
| 70 | +def stop_local(): |
| 71 | + |
| 72 | + global bstack_local |
| 73 | + |
| 74 | + bstack_local.stop() |
| 75 | + |
| 76 | + |
| 77 | +def get_row_item_from_file(filepath, key): |
| 78 | + |
| 79 | + dirname = os.path.dirname(__file__) |
| 80 | + filename = os.path.normpath(os.path.join(dirname, filepath)) |
| 81 | + |
| 82 | + with open(filename, 'r') as data: |
| 83 | + for line in csv.reader(data): |
| 84 | + if line[0] == key: |
| 85 | + return line[1] |
| 86 | + |
| 87 | + |
| 88 | +def get_password(username): |
| 89 | + |
| 90 | + password = get_row_item_from_file(user_data, username) |
| 91 | + return password |
| 92 | + |
| 93 | + |
| 94 | +def get_product_prices(): |
| 95 | + dirname = os.path.dirname(__file__) |
| 96 | + filename = os.path.normpath(os.path.join( |
| 97 | + dirname, product_data)) |
| 98 | + |
| 99 | + prices = [] |
| 100 | + with open(filename, 'r') as data: |
| 101 | + for line in csv.reader(data): |
| 102 | + if line[3] != 'price': |
| 103 | + prices.append(int(line[3])) |
| 104 | + |
| 105 | + prices = sorted(prices) |
| 106 | + |
| 107 | + for i in range(0, len(prices)): |
| 108 | + prices[i] = str('$' + str(prices[i]) + '.00') |
| 109 | + |
| 110 | + return prices |
| 111 | + |
| 112 | + |
| 113 | +def set_loc(lat, long): |
| 114 | + |
| 115 | + global se2lib |
| 116 | + |
| 117 | + se2lib.driver.execute_script( |
| 118 | + "navigator.geolocation.getCurrentPosition = function(cb){cb({ coords: {accuracy: 20,altitude: null,altitudeAccuracy: null,heading: null,latitude: " + lat + ",longitude: " + long + ",speed: null}}); }") |
| 119 | + se2lib.driver.execute_script( |
| 120 | + "window.navigator.geolocation.getCurrentPosition = function(cb){cb({ coords: {accuracy: 20,altitude: null,altitudeAccuracy: null,heading: null,latitude: " + lat + ",longitude: " + long + ",speed: null}}); }") |
| 121 | + |
| 122 | + |
| 123 | +def mark_test_session(result): |
| 124 | + global se2lib |
| 125 | + |
| 126 | + if result == 'PASS': |
| 127 | + se2lib.driver.execute_script( |
| 128 | + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Test Ran Succesfully"}}') |
| 129 | + else: |
| 130 | + se2lib.driver.execute_script( |
| 131 | + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Oops! my sample test failed"}}') |
| 132 | + |
| 133 | + |
| 134 | +def set_session_name(new_name): |
| 135 | + global se2lib |
| 136 | + se2lib.driver.execute_script( |
| 137 | + 'browserstack_executor: {"action": "setSessionName", "arguments": {"name": "' + new_name + '"}}') |
0 commit comments