def set_cfg_fp(cfg_fp): gv.set_value('cfg_fp', cfg_fp)
def set_data_fp(data_fp): gv.set_value('data_fp', data_fp)
def traverse_judge(casename, currentlists): """decide import or reload testcase file""" import os import sys import global_variables import time import basic_class import importlib import shutil testcasename = casename + '.py' setup_num = int(global_variables.get_value('setup_num')) run_num = int(global_variables.get_value('run_num')) teardowm_num = int(global_variables.get_value('teardowm_num')) temppath = global_variables.get_value('temppath') if testcasename in currentlists: # print part test case names :setup or run or teardown shutil.copyfile(testcasename, temppath + '/' + testcasename) try: shutil.rmtree(temppath + '/__pycache__') time.sleep(0.01) except FileNotFoundError: basic_class.mylogger_record.debug('__pycache__ not exists') if 'setup' in casename.lower(): basic_class.mylogger_recordnf.title('[-->Executing setup.py ...]') if setup_num == 1: tmp_module_setup = importlib.import_module('setup') global_variables.set_value('tmp_module_setup', tmp_module_setup) setup_num += 1 global_variables.set_value('setup_num', setup_num) else: tmp_module_setup = global_variables.get_value( 'tmp_module_setup') importlib.reload(tmp_module_setup) if 'run' in casename.lower(): basic_class.mylogger_recordnf.title('[-->Executing run.py ...]') if run_num == 1: tmp_module_run = importlib.import_module('run') global_variables.set_value('tmp_module_run', tmp_module_run) run_num += 1 global_variables.set_value('run_num', run_num) else: tmp_module_run = global_variables.get_value('tmp_module_run') importlib.reload(tmp_module_run) if 'teardown' in casename.lower(): basic_class.mylogger_recordnf.title( '[-->Executing teardown.py ...]') if teardowm_num == 1: tmp_module_teardown = importlib.import_module('teardown') global_variables.set_value('tmp_module_teardown', tmp_module_teardown) teardowm_num += 1 global_variables.set_value('teardowm_num', teardowm_num) else: tmp_module_teardown = global_variables.get_value( 'tmp_module_teardown') importlib.reload(tmp_module_teardown)
def set_global_variables(args): cfg_fp = args.darknet_p + '/cfg/' + args.cfg_fn data_fp = args.darknet_p + '/cfg/' + args.data_fn weight_fp = args.weight_fp with open(data_fp, 'r') as f: options = f.readlines() options = [op.strip('\n').split('=') for op in options] options = dict(options) args.cfg_fn = args.cfg_fn.strip('.cfg') args.data_fn = args.data_fn.strip('.data') key_name = args.data_fn train_fn = options['train'].split('/')[-1] valid_fn = options['valid'].split('/')[-1] cls_fp = options['names'] result_p = options['results'] backup_p = options['backup'] filetools.check_makedir(result_p) filetools.check_makedir(backup_p) filetools.check_makedir(result_p + '/cache') dataset_p = re.sub('/filelist/.*', '', options['train']) gv.set_value('key_name', key_name) gv.set_value('cfg_fp', cfg_fp) gv.set_value('data_fp', data_fp) gv.set_value('cls_fp', cls_fp) gv.set_value('result_p', result_p) gv.set_value('backup_p', backup_p) gv.set_value('dataset_p', dataset_p) gv.set_value('train_fn', train_fn) gv.set_value('valid_fn', valid_fn) gv.set_value('weight_fp', weight_fp) show_gv()
def region_init_sep(self): test_window = ImageCapture(gl.get_value('version').lower()) region = test_window.icapture('main', False) tree = ImgTree() RD0 = tree.next_depth(region, 1) regions_up = [] for i in range(len(RD0)): if RD0[i][0][0][1] >= 30: regions_up.append(RD0[i]) if len(regions_up) > 4: break RD1 = tree.next_depth([regions_up[-1]], 1) regions = regions_up[0:len(regions_up)-1] for i in range(3): if len(regions) > 7: break if RD1[i][0][0][1] >= 30: regions.append(RD1[i]) for i in range(len(regions)): cv2.imwrite('scratch\\'+str(i)+'.png', regions[i][0][1].reshape(regions[i][0][0][1], regions[i][0][0][0])) gl.set_value('top_border', regions[0][0][0]) gl.set_value('ribbon_up', regions[1][0][0]) gl.set_value('ribbon_down', regions[2][0][0]) gl.set_value('quick_access', regions[3][0][0]) gl.set_value('left_border', regions[4][0][0]) gl.set_value('navigator', regions[5][0][0]) gl.set_value('main_window', regions[6][0][0])
def diff_image(self, gray_orig, gray_mod): margin = eval(gl.get_value('margin')) gray_orig_1 = cv2.adaptiveThreshold(gray_orig, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 3) gray_mod_1 = cv2.adaptiveThreshold(gray_mod, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 3) area_max = 0 rect = [] rect1 = [] (score, diff) = compare_ssim(gray_orig_1, gray_mod_1, full=True) diff = (diff * 255).astype("uint8") thresh = cv2.threshold(diff, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) for c in cnts: (x, y, w, h) = cv2.boundingRect(c) area = w * h if area >= area_max: area_max = area rect = [x, y, w, h] gray_diff = [] rect_m = [rect[0], rect[1], rect[2], rect[3]] for i in range(rect[1], rect[1] + rect[3]): tmp = gray_mod[i][rect[0]:rect[0] + rect[2]] gray_diff.append(tmp) gray_diff = np.array(gray_diff, dtype='uint8') thresh = cv2.adaptiveThreshold(gray_diff, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 3, 3) cnts1 = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts2 = imutils.grab_contours(cnts1) area_max = 0 for c in cnts2: (x, y, w, h) = cv2.boundingRect(c) area = w * h if area >= area_max: area_max = area rect = [x+rect_m[0], y+rect_m[1], w, h] gray_diff1 = [] rect_m1 = [rect[0], rect[1], rect[2], rect[3]] for i in range(rect[1] + margin, rect[1] + rect[3] - margin): tmp = gray_mod[i][rect[0] + margin:rect[0] + rect[2] - margin] gray_diff1.append(tmp) cv2.imwrite(gl.get_value('scratch_path') + 'diff.png', np.array(gray_diff1)) gl.set_value('DIFF_POSITION', rect_m1) return gray_diff1
def icapture(self, window_type, diff_flag): size = [] position = [] if window_type == 'main': position = eval(gl.get_value('main_window_position')) size = eval(gl.get_value('main_window_size')) elif window_type == 'dialog': position = eval(gl.get_value('dialog_position')) size = eval(gl.get_value('dialog_size')) else: print('Window type should be only main or dialog !!') margin = eval(gl.get_value('margin')) scale = eval(gl.get_value('scale')) x_axis = 1*(size[0] - 2 * margin) y_axis = 1*(size[1] - 2 * margin) screen_shot = [[np.array([int(scale*x_axis), int(scale*y_axis), position[0], position[1], 0])], ] win32gui.EnumWindows(self.handle_catch, None) if self.handle is None: print('No Main Window Found!') else: reset_flag = gl.get_value(self.title_flag_nm) if reset_flag != 'close': win32gui.SetForegroundWindow(self.handle) gl.set_value(self.title_flag_nm, 'close') rect = win32gui.GetWindowRect(self.handle) window_position_current = rect[0:2] window_size_current = rect[2:4] if (list(window_position_current) != position) or (list(window_size_current) != size): win32gui.SetWindowPos(self.handle, win32con.HWND_TOPMOST, position[0], position[1], size[0], size[1], win32con.SWP_SHOWWINDOW) time.sleep(0.5) img = np.array(ImageGrab.grab((position[0]+margin, position[1]+margin, position[0]+size[0]-margin, position[1]+size[1]-margin))) img = cv2.resize(img, (int(scale*(size[0]-2*margin)), int(scale*(size[1]-2*margin)))) cv2.imwrite(gl.get_value('scratch_path')+'workbench.png', img) gray_mod = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if diff_flag is True: gray_orig = cv2.imread(gl.get_value('scratch_path') + 'previous.png') gray_orig = cv2.cvtColor(gray_orig, cv2.COLOR_BGR2GRAY) gray_diff = ImageDiff().diff_image(gray_orig, gray_mod) rect = gl.get_value('DIFF_POSITION') x_axis = 1 * (rect[2]-2*margin) y_axis = 1 * (rect[3]-2*margin) screen_shot1 = [[np.array([int(1 * x_axis), int(1 * y_axis), rect[0], rect[1], 0])], ] screen_shot1[0].append(np.array(gray_diff).ravel()) screen_shot = screen_shot1 else: screen_shot[0].append(np.array(gray_mod).ravel()) cv2.imwrite(gl.get_value('scratch_path') + 'previous.png', gray_mod) return [screen_shot]
def coordinates_act(script_input, search_area): mother_canvas, child_canvas, act_key, target, act_input, act_type, anchor = script_input scale = eval(gl.get_value('scale')) margin = eval(gl.get_value('margin')) search_iteration = eval(gl.get_value('thresh_hold_iteration')) if mother_canvas in ['main', 'dialog']: if mother_canvas == 'main': window_position = eval(gl.get_value('main_window_position')) window_size = eval(gl.get_value('main_window_size')) else: window_position = eval(gl.get_value('dialog_position')) window_size = eval(gl.get_value('dialog_size')) if act_key in ['mb1', 'mb3', 'dmb1', 'rinput', 'binput', 'rmb1', 'bmb1']: coordinates = itm.icon_match(target, gl.get_value('icon_path')) if len(coordinates) == 0: for m in range(search_iteration): strings_all = StringLoc().string_ocr(search_area) print(strings_all) if anchor == 'invalid': string_combination = StringLoc().string_process(target) target_index = StringLoc().string_index(string_combination, strings_all) if len(target_index) == 0: th = eval(gl.get_value('cutting_threshold')) print('Cutting Threshold = ', th) gl.set_value('cutting_threshold', str(th+eval(gl.get_value('thresh_hold_step')))) continue else: coordinates = StringLoc().action_coordinates(target_index, strings_all) break else: target_index_final = [] string_combination = StringLoc().string_process(target) target_index = StringLoc().string_index(string_combination, strings_all) if len(target_index) == 0: th = eval(gl.get_value('cutting_threshold')) print('Cutting Threshold = ', th) gl.set_value('cutting_threshold', str(th + eval(gl.get_value('thresh_hold_step')))) continue else: string_combination_anchor = StringLoc().string_process(anchor + ' ' + target) target_index_anchor = StringLoc().string_index(string_combination_anchor, strings_all) minimal_target = StringLoc().target_anchor_achieve(target_index_anchor, strings_all) for k in range(len(target_index[0])): if target_index[0][k][0] in minimal_target[0]: target_index_final = [[target_index[0][k]]] break coordinates = StringLoc().action_coordinates(target_index_final, strings_all) break if len(coordinates) == 0: print('Act Location Not Found !!!') coordinates = np.array(coordinates)/scale + np.array([margin, margin])\ + np.array([window_position[0], window_position[1]])/scale else: coordinates = np.array(coordinates) / scale + np.array([margin, margin]) \ + np.array([window_position[0], window_position[1]]) if act_key == 'mb1': MKAction().MB1(coordinates) elif act_key == 'mb3': MKAction().MB3(coordinates) elif act_key == 'dmb1': MKAction().DMB1(coordinates) elif act_key == 'rinput': MKAction().RINPUT(coordinates, act_input) elif act_key == 'binput': MKAction().BINPUT(coordinates, act_input) elif act_key == 'rmb1': MKAction().RMB1(coordinates) elif act_key == 'bmb1': MKAction().BMB1(coordinates) else: print('Wrong Mouse Action Keyword !!!') elif act_key == 'press': MKAction().MB1(np.array([window_position[0]+window_size[0]/2, window_position[1]]) + np.array([0, 10])) time.sleep(0.5) MKAction().PRESS(act_input) elif act_key == 'mb2': MKAction().MB1(np.array([window_position[0] + window_size[0] / 2, window_position[1]]) + np.array([0, 10])) time.sleep(0.5) MKAction().MB2() else: print('Wrong Keyboard Action Keyword !!!') MKAction().CURSOR_REST() else: if act_key == 'wait': time.sleep(act_input) elif act_key == 'finish': MKAction().BUS_STOP() else: print('Wrong System Command!!!')
def traverse(Path): """traverse testcases under give Path,normal first execute setup,then run,last teardown for each testcase""" import os import sys import basic_class import global_variables import datetime os.chdir(Path) # switch to current Path currentlists = sorted( os.listdir('.')) # get current folder and file names in current path for list in currentlists: # delete the hiden files and folders if list.startswith('.'): currentlists.remove(list) for list in currentlists: # delete the '__pycache__/' folderss if '__pycache__' in list: currentlists.remove(list) # print test case title if this is a final testcase folder ,means no sub folders #bottom_flag = [os.path.isfile(list) for list in currentlists] no_subfolder_flag = [os.path.isfile(list) for list in currentlists] has_script_flag = [] for list in currentlists: if 'setup.py' in list.lower(): has_script_flag.append('True') if 'run.py' in list.lower(): has_script_flag.append('True') if 'teardown.py' in list.lower(): has_script_flag.append('True') testsuit_flag = [] # determibe if sub fldr contains script or not for root, dirs, files in os.walk('.'): for file in files: if 'setup.py' in file.lower(): testsuit_flag.append('True') if 'run.py' in file.lower(): testsuit_flag.append('True') if 'teardown.py' in file.lower(): testsuit_flag.append('True') basic_class.mylogger_record.debug('no_subfolder_flag= ' + str(no_subfolder_flag)) basic_class.mylogger_record.debug('has_script_flag= ' + str(has_script_flag)) basic_class.mylogger_record.debug('testsuit_flag= ' + str(testsuit_flag)) if has_script_flag.count('True') > 0 and has_script_flag.count( 'True' ) == testsuit_flag.count( 'True' ): # contains 'setup' or 'run' or 'teardown' in current fplder,and no scripts in sub folders,is a testcase folder testcase_name = os.getcwd().split('/')[-1] #basic_class.mylogger_recordnf.title('\n'+'-'*(17+len(testcase_name))) basic_class.mylogger_recordnf.title('\n<---Testcase: ' + testcase_name + ' --->') testcase_starttime = datetime.datetime.now( ) # record testcase start time basic_class.mylogger_record.debug('testcase_starttime= ' + str(testcase_starttime)) global_variables.set_value('testcase_starttime', testcase_starttime) #basic_class.mylogger_recordnf.title('-'*(17+len(testcase_name))) global_variables.set_value('testcase_name', testcase_name) elif has_script_flag.count('True') == 0 and testsuit_flag.count( 'True' ) > 0: # has sub folders ,and sub folder contains scripts, is a test suits folder folder_name = os.getcwd().split('/')[-1] #basic_class.mylogger_recordnf.title('\n'+'='*(17+len(folder_name))) basic_class.mylogger_recordnf.title('\n<<===Testsuit: ' + folder_name + ' ===>>') #basic_class.mylogger_recordnf.title('='*(17+len(folder_name))) else: pass # ignore currentpath #print('currentlists:',currentlists) traverse_judge('setup', currentlists) # run setup if exists traverse_judge('run', currentlists) # run run if exists for list in currentlists: if os.path.isdir(list): traverse(list) #os.chdir(list) #print (os.getcwd()) traverse_judge('teardown', currentlists) # run teardown if exists testcase_stoptime = datetime.datetime.now() # record testcase stop time global_variables.set_value('testcase_stoptime', testcase_stoptime) basic_class.mylogger_record.debug('testcase_stoptime= ' + str(testcase_stoptime)) #os.system('chmod +x setup.py;./setup.py') os.chdir('..')