How to use content_desc method in fMBT

Best Python code snippet using fMBT_python

get_widget_feature_ATM.py

Source:get_widget_feature_ATM.py Github

copy

Full Screen

1# this is for ATM and Craftdroid2# the dataset is Fruiter test3# the features are from ATM and Craftdroid4import jsonlines5import pandas as pd6import src.stage2.parameter_stage2 as para27from lxml import etree8import numpy as np9from src.stage2.get_same_state import get_all_file10from src.stage2.get_widget_feature import CropImage4File11def get_widget(json_path,json_name):12 # get widgets in test set13 widgets = set()14 with open(json_path+json_name,'r+',encoding='utf8') as f:15 for item in jsonlines.Reader(f):16 widget1 = item['widget1']17 widget2 = item['widget2']18 widgets.add(widget1)19 widgets.add(widget2)20 return widgets21# def get_widget_in_event(csv_path):22# # get widgets in test_event_map23# widgets = set()24# csv_name = 'test_event_map.csv'25# df = pd.read_csv(csv_path+csv_name)26# for index in range(len(df)):27# new_test_event = df.loc[index,'new_test_event']28# widgets.add(new_test_event)29# return widgets30def extract_ori_widget(widgets,xml_path_prefix):31 df = pd.DataFrame(columns=['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','class'],index=[])32 shopping_dict ={'aliexpress':1,33 'ebay':2,34 'etsy':3,35 '5miles':4,36 'geek':5,37 'googleshopping':6,38 'groupon':7,39 'home':8,40 '6pm':9,41 'wish':10,42 'google_shopping':11}43 for widget in widgets:44 prefix = ""45 xml_path = widget.split(":")[0]46 widget_path = widget.split(":")[1]47 app_name = xml_path.split("/")[0]48 if app_name in shopping_dict:49 prefix = 'shopping/'50 else:51 prefix = 'news/'52 full_xml_path = xml_path_prefix + prefix + xml_path53 tree = etree.parse(full_xml_path)54 root = tree.getroot()55 findall = etree.XPath(widget_path)56 nodelist = findall(root) # get the work57 id = np.nan58 if nodelist[0].get("resource-id") != "":59 id = nodelist[0].get("resource-id")60 text = np.nan61 if nodelist[0].get("text") != "":62 text = nodelist[0].get("text")63 content_desc = np.nan64 if nodelist[0].get("content-desc") != "":65 content_desc = nodelist[0].get("content-desc")66 if nodelist[0].get("clickable") == 'true':67 type = 'clickable'68 else:69 type = 'fillable'70 parent_text = np.nan71 parent_node = nodelist[0].getparent()72 if parent_node.get("text")!="":73 parent_text = parent_node.get("text")74 sibling_text = np.nan75 sibling_node = ""76 widget_class = nodelist[0].get("class")77 if len(parent_node)!=0:78 for index in range(len(parent_node)-1):79 test_sibling_node = parent_node[index]80 test_node = parent_node[index+1]81 if test_node == nodelist[0]:82 sibling_node = test_sibling_node83 break84 if sibling_node!="":85 if sibling_node.get("text")!="":86 sibling_text = sibling_node.get("text")87 atm_neighbor = sibling_text88 df_line = pd.DataFrame({89 'widget':widget,90 'text':text,91 'id':id,92 'content_desc':content_desc,93 'parent_text':parent_text,94 'sibling_text':sibling_text,95 'atm_neighbor':atm_neighbor,96 'type':type,97 'class':widget_class,98 },index=[1])99 df = df.append(df_line,ignore_index=True)100 return df101def extract_or_widget_map(csv_path,xml_path_prefix,csv_save_path):102 # extract atm /craftdrodi features in GT_news_revise/ GT_shopping103 df=pd.read_csv(csv_path)104 shopping_dict = {'aliexpress': 1,105 'ebay': 2,106 'etsy': 3,107 '5miles': 4,108 'geek': 5,109 'googleshopping': 6,110 'groupon': 7,111 'home': 8,112 '6pm': 9,113 'wish': 10,114 'google_shopping': 11}115 df_new = pd.DataFrame(columns=['app_name','widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','widget_name','class','current_activity','next_activity','canonical'],index=[])116 app_name = ''117 for index in range(len(df)):118 if isinstance(df.loc[index,'add-id'],float) and isinstance(df.loc[index,'add-xpath'],float):119 continue120 state_name = df.loc[index,'state-name'].strip().split('\t')[-1]121 app_name=state_name.split("/")[0]122 widget_name = state_name.split("/")[-1] # -1 not 1 because the last widget_name is corresponding features and xpath123 if app_name in shopping_dict:124 prefix = 'shopping/'125 else:126 prefix = 'news/'127 add_id = df.loc[index,'add-id']128 add_xpath = df.loc[index,'add-xpath']129 widget_path = ""130 widget_path= add_xpath131 full_xml_path = xml_path_prefix + prefix + app_name+"/"+widget_name132 tree = etree.parse(full_xml_path)133 root = tree.getroot()134 findall = etree.XPath(widget_path)135 nodelist = findall(root) # get the work136 ori_xpath = df.loc[index,'ori_xpath']137 id = np.nan138 if nodelist[0].get("resource-id") != "":139 id = nodelist[0].get("resource-id")140 text = np.nan141 if nodelist[0].get("text") != "":142 text = nodelist[0].get("text")143 content_desc = np.nan144 if nodelist[0].get("content-desc") != "":145 content_desc = nodelist[0].get("content-desc")146 widget_class = nodelist[0].get("class")147 if nodelist[0].get("clickable") == 'true':148 type = 'clickable'149 else:150 type = 'fillable'151 parent_text = np.nan152 parent_node = nodelist[0].getparent()153 if parent_node.get("text")!="":154 parent_text = parent_node.get("text")155 sibling_text = np.nan156 sibling_node = ""157 if len(parent_node)!=0:158 for idx in range(len(parent_node)-1):159 test_sibling_node = parent_node[idx]160 test_node = parent_node[idx+1]161 if test_node == nodelist[0]:162 sibling_node = test_sibling_node163 break164 if sibling_node!="":165 if sibling_node.get("text")!="":166 sibling_text = sibling_node.get("text")167 atm_neighbor = sibling_text168 widget_bounded = nodelist[0].get("bounds")169 # todo: need to revise to regular expression170 former_coordination = widget_bounded.split("][")[0]171 latter_coordination = widget_bounded.split("][")[1]172 former_x_bound = int(former_coordination.split(",")[0][1:])173 former_y_bound = int(former_coordination.split(",")[1])174 latter_x_bound = int(latter_coordination.split(",")[0])175 latter_y_bound = int(latter_coordination.split(",")[1][:-1])176 widget_bounded = [former_x_bound, former_y_bound,177 latter_x_bound, latter_y_bound]178 # only for server179 ocr_text = CropImage4File(full_xml_path,widget_bounded)180 if text == "":181 text = ocr_text182 current_activity = df.loc[index,'current Activity']183 next_activity = df.loc[index,'next Activity']184 canonical = df.loc[index,'canonical']185 df_line = pd.DataFrame({186 'app_name': app_name,187 'widget':widget_path,188 'text':text,189 'id':id,190 'content_desc':content_desc,191 'parent_text':parent_text,192 'sibling_text':sibling_text,193 'atm_neighbor':atm_neighbor,194 'type':type,195 'widget_name':widget_name,196 'class':widget_class,197 'current_activity':current_activity,198 'next_activity':next_activity,199 'canonical':canonical,200 'ori_xpath':ori_xpath,201 },index=[1])202 df_new = df_new.append(df_line,ignore_index=True)203 csv_save_name = app_name+".csv"204 df_new.to_csv(csv_save_path+csv_save_name)205def split_csv(test_data_feature_csv,csv_save_path):206 df_abc = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])207 df_bbcnews = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])208 df_buzzfeed = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])209 df_cnn = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])210 df_fox = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])211 df_newsrepublic = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])212 df_reuters = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])213 df_smartnews = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])214 df_theguardian = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])215 df_usatoday = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])216 df_6pm = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])217 df_aliexpress = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])218 df_ebay = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])219 df_etsy = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])220 df_geek = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])221 df_googleshopping = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])222 df_groupon = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])223 df_home = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])224 df_wish = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])225 df_5miles = pd.DataFrame(columns = ['widget','text','id','content_desc','parent_text','sibling_text','atm_neighbor','type','app'],index=[])226 df = pd.read_csv(test_data_feature_csv)227 for index in range(len(df)):228 app_name = df.loc[index,'widget'].split('/')[0]229 df_line = pd.DataFrame(230 {231 'widget':df.loc[index,'widget'],232 'text':df.loc[index,'text'],233 'id':df.loc[index,'id'],234 'content_desc':df.loc[index,'content_desc'],235 'parent_text':df.loc[index,'parent_text'],236 'sibling_text':df.loc[index,'sibling_text'],237 'atm_neighbor':df.loc[index,'atm_neighbor'],238 'type':df.loc[index,'type'],239 'app':app_name,240 },index=[1]241 )242 if app_name =='abc':243 df_abc =df_abc.append(df_line)244 elif app_name == 'bbcnews':245 df_bbcnews = df_bbcnews.append(df_line)246 elif app_name == 'buzzfeed':247 df_buzzfeed = df_buzzfeed.append(df_line)248 elif app_name == 'cnn':249 df_cnn = df_cnn.append(df_line)250 elif app_name == 'fox':251 df_fox = df_fox.append(df_line)252 elif app_name == 'newsrepublic':253 df_newsrepublic = df_newsrepublic.append(df_line)254 elif app_name == 'reuters':255 df_reuters = df_reuters.append(df_line)256 elif app_name == 'smartnews':257 df_smartnews = df_smartnews.append(df_line)258 elif app_name == 'theguardian':259 df_theguardian = df_theguardian.append(df_line)260 elif app_name == 'usatoday':261 df_usatoday = df_usatoday.append(df_line)262 elif app_name == '6pm':263 df_6pm = df_6pm.append(df_line)264 elif app_name == 'aliexpress':265 df_aliexpress = df_aliexpress.append(df_line)266 elif app_name == 'ebay':267 df_ebay = df_ebay.append(df_line)268 elif app_name == 'etsy':269 df_etsy = df_etsy.append(df_line)270 elif app_name == 'geek':271 df_geek = df_geek.append(df_line)272 elif app_name == 'googleshopping':273 df_googleshopping = df_googleshopping.append(df_line)274 elif app_name == 'groupon':275 df_groupon = df_groupon.append(df_line)276 elif app_name == 'home':277 df_home = df_home.append(df_line)278 elif app_name == 'wish':279 df_wish = df_wish.append(df_line)280 elif app_name == '5miles':281 df_5miles = df_5miles.append(df_line)282 df_abc.to_csv(csv_save_path + 'news/abc.csv')283 df_bbcnews.to_csv(csv_save_path+'news/bbc.csv')284 df_buzzfeed.to_csv(csv_save_path+'news/buzzfeed.csv')285 df_cnn.to_csv(csv_save_path+'news/cnn.csv')286 df_fox.to_csv(csv_save_path+'news/fox.csv')287 df_newsrepublic.to_csv(csv_save_path+'news/newsrepublic.csv')288 df_reuters.to_csv(csv_save_path+'news/reuters.csv')289 df_smartnews.to_csv(csv_save_path+'news/smartnews.csv')290 df_theguardian.to_csv(csv_save_path+'news/theguardian.csv')291 df_usatoday.to_csv(csv_save_path+'news/usatoday.csv')292 df_6pm.to_csv(csv_save_path+'shopping/6pm.csv')293 df_aliexpress.to_csv(csv_save_path+'shopping/aliexpress.csv')294 df_ebay.to_csv(csv_save_path+'shopping/ebay.csv')295 df_etsy.to_csv(csv_save_path+'shopping/etsy.csv')296 df_geek.to_csv(csv_save_path+'shopping/geek.csv')297 df_googleshopping.to_csv(csv_save_path+'shopping/googleshopping.csv')298 df_groupon.to_csv(csv_save_path+'shopping/groupon.csv')299 df_home.to_csv(csv_save_path+'shopping/home.csv')300 df_wish.to_csv(csv_save_path+'shopping/wish.csv')301 df_5miles.to_csv(csv_save_path+'shopping/5miles.csv')302if __name__=="__main__":303 file_List = []304 xml_path_prefix = '/Fruiter/TestBenchmark-Java-client-master/screenshots/'305 csv_path = '../../resources/GT_revise_0213/shopping_same_row/'306 get_all_file(csv_path, file_List)307 csv_save_path = '/Migration/resources/experiment/test-tool/0308/shopping_same_row/'308 index = 0309 for file_path in file_List:310 if '.DS_Store' in file_path or 'ground_truth_pair' in file_path:311 continue312 print(file_path)313 extract_or_widget_map(file_path, xml_path_prefix,csv_save_path)314 # index += 1315 # print(index)...

Full Screen

Full Screen

puzzle_2.py

Source:puzzle_2.py Github

copy

Full Screen

1def get_contained_bags(bags, start_bag):2 total_bags = 03 for contained_bag in bags[start_bag]:4 num_contained_bags = int(bags[start_bag][contained_bag])5 total_bags += num_contained_bags6 total_bags += num_contained_bags*get_contained_bags(bags, contained_bag)7 return total_bags8def parse_line(line):9 name, contents_txt = line.split(" bags contain ")10 contents = {}11 contents_txt = contents_txt[:-1] #drop the period12 if contents_txt == "no other bags":13 contents = []14 else:15 csplit = contents_txt.split(", ")16 for x in csplit:17 content_desc = x.split(" ")18 num = content_desc[0]19 content_bag_name = content_desc[1]+" "+content_desc[2]20 contents[content_bag_name] = num21 return name, contents22groups = []23with open("data.txt") as data:24 lines = data.read().splitlines()25bags = {}26for line in lines:27 name, contents = parse_line(line)28 bags[name] = contents29contained_bags = get_contained_bags(bags, "shiny gold")30print(contained_bags)...

Full Screen

Full Screen

puzzle_1.py

Source:puzzle_1.py Github

copy

Full Screen

1def get_containing_bags(bags, start_bag):2 top_bags = []3 for name, contents in bags.items():4 if start_bag in contents:5 top_bags.append(name)6 top_bags.extend(get_containing_bags(bags, name))7 return top_bags8def parse_line(line):9 name, contents_txt = line.split(" bags contain ")10 contents = {}11 contents_txt = contents_txt[:-1] #drop the period12 if contents_txt == "no other bags":13 contents = []14 else:15 csplit = contents_txt.split(", ")16 for x in csplit:17 content_desc = x.split(" ")18 num = content_desc[0]19 content_bag_name = content_desc[1]+" "+content_desc[2]20 contents[content_bag_name] = num21 return name, contents22groups = []23with open("data.txt") as data:24 lines = data.read().splitlines()25bags = {}26for line in lines:27 name, contents = parse_line(line)28 bags[name] = contents29top_level_bags = get_containing_bags(bags, "shiny gold")30#print(top_level_bags)31print(len(set(top_level_bags)))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful