How to use div_rect method in Airtest

Best Python code snippet using Airtest

draw_score.py

Source:draw_score.py Github

copy

Full Screen

1from pygame import image, font, rect, time2from constants import *3from menu.save_handler import set_save_arcade, get_save_arcade4from menu.account_handler import set_online_score, get_online_score5from threading import Thread6import pygame7import os8"""9Gestion de l'interface des scores en ligne et en local10"""11class Score_Interface:12 """13 stats : tuple [score, plane_kill]14 """15 def __init__(self, screen, stats=(0, 0)):16 #Initialisation des variables de base17 self.json_data_local = get_save_arcade()18 self.json_data_online = {}19 t1 = Thread(target=self.load_online_score)20 self.stats = stats21 self.screen = screen22 self.clock = time.Clock()23 self.keep_drawing = True24 self.add_stats = False25 self.disp_stats = False26 self.disp_button_state = False27 self.online_loaded = False28 29 #Chargement de la police d'écriture30 self.write_font = font.Font("ath.ttf", 30)31 #Chargement de toutes les images32 self.bg = image.load(path.join(SCORE_DIR, "scores_background.png")).convert_alpha()33 self.divs = (image.load(path.join(SCORE_DIR, "div_local.png")).convert_alpha(),34 image.load(path.join(SCORE_DIR, "div_online.png")).convert_alpha(),35 image.load(path.join(SCORE_DIR, "div_share.png")).convert_alpha())36 self.tabs = (image.load(path.join(SCORE_DIR, "tab_local.png")).convert_alpha(),37 image.load(path.join(SCORE_DIR, "tab_online.png")).convert_alpha(),38 image.load(path.join(SCORE_DIR, "tab_share.png")).convert_alpha())39 self.tabs_click = (image.load(path.join(SCORE_DIR, "tab_local_click.png")).convert_alpha(),40 image.load(path.join(SCORE_DIR, "tab_online_click.png")).convert_alpha(),41 image.load(path.join(SCORE_DIR, "tab_share_click.png")).convert_alpha())42 self.score_window = image.load(path.join(SCORE_DIR, "div_ask_score.png")).convert_alpha()43 self.disp_score = image.load(path.join(SCORE_DIR, "div_disp_score.png")).convert_alpha()44 self.confirm_flash = image.load(path.join(SCORE_DIR, "button_confirm.png")).convert_alpha()45 self.score_loading = image.load(path.join(SCORE_DIR, "score_loading.png")).convert_alpha()46 self.score_error = image.load(path.join(SCORE_DIR, "score_error.png")).convert_alpha()47 self.tab_score = image.load(path.join(SCORE_DIR, "btn_score.png")).convert_alpha()48 self.btn_enter = (image.load(path.join(SCORE_DIR, "btn_enter.png")).convert_alpha(), 49 image.load(path.join(SCORE_DIR, "btn_enter_click.png")).convert_alpha())50 self.overlay = image.load(path.join(SCORE_DIR, "overlay.png")).convert_alpha()51 self.logos = list()52 self.logos_click = list()53 54 for logo in sorted(os.listdir(path.join(SCORE_DIR, "logos", "classic"))):55 self.logos.append(image.load(path.join(SCORE_DIR, "logos", "classic", logo)).convert_alpha())56 for logo in sorted(os.listdir(path.join(SCORE_DIR, "logos", "click"))):57 self.logos_click.append(image.load(path.join(SCORE_DIR, "logos", "click", logo)).convert_alpha())58 #Chargement des flèches directive et initialisation des variables les concernants59 self.last_anim = 060 self.last_flash = 061 self.arrow_l = image.load(path.join(ARCADE_DIR, "arrow_left.png")).convert_alpha()62 self.arrow_r = image.load(path.join(ARCADE_DIR, "arrow_right.png")).convert_alpha()63 self.arrow_l_left = 10064 self.arrow_r_left = WIDTH-10065 self.forward = True66 #Position des flèches67 self.arrow_r_rect = self.arrow_r.get_rect()68 self.arrow_r_rect.center = (WIDTH-40, HEIGHT//2)69 self.arrow_l_rect = self.arrow_l.get_rect()70 self.arrow_l_rect.center = (40, HEIGHT//2)71 self.div_index = 072 self.logo_index = 073 self.input_str = ""74 75 #Doit être appelé avant check score pour récupérer tous les scores76 #Récupération des scores en lignes77 t1.start()78 score_loading_rect = self.score_loading.get_rect()79 score_loading_rect.center = self.bg.get_rect().center80 self.screen.blit(self.bg, (0, 0))81 self.screen.blit(self.score_loading, score_loading_rect)82 pygame.display.flip()83 t1.join()84 #si ya une erreur on change la page d'affichage des scores en ligne85 if not self.online_loaded:86 self.divs = (self.score_error, self.divs[1])87 self.div_index = 188 #On check le score si c'est un bon ou ajoute les stats si c'est un pas bon on affiche les stats89 if self.stats[0] > 1 and self.online_loaded:90 self.check_score()91 if not self.add_stats:92 self.disp_stats = True93 """94 Méthode thread pour récupérer les scores en ligne95 """96 def load_online_score(self):97 self.json_data_online = get_online_score()98 if self.json_data_online:99 self.online_loaded = True100 """101 Méthode thread pour sauvegarder les scores en ligne102 """103 def save_online_score(self):104 if set_online_score(self.data_to_save):105 print("Score saved")106 """107 Méthode de récup des evenements de fermeture108 """109 def getBasicEvent(self, event):110 if event.type == pygame.QUIT: 111 return True112 if event.type == pygame.KEYDOWN:113 if event.key == pygame.K_F4:114 return True115 """116 Méthode qui détermine si l'on est dans le top 10 ou pas en mode local et en mode hors ligne117 si name est passé en argument on sauvegarde les scores118 Cette méthode est appelée deux fois, la première pour savoir si on peut ajouter un score la seconde pour l'ajouter avec le nom119 """120 def check_score(self, name=None):121 #Si il reste encore de la place parmis les 5 meilleurs local122 if len(self.json_data_local) < 5:123 self.add_stats = True124 if name:125 self.save_score(name, False)126 #Si on est parmis les 5 meilleurs local127 if len(self.json_data_local) >= 5 and self.json_data_local[-1]["score"] < self.stats[0]:128 self.add_stats = True129 if name:130 self.save_score(name, True)131 #Si il reste encore de la place parmis les 10 meilleurs online132 if len(self.json_data_online) < 5:133 self.add_stats = True134 if name:135 self.save_score(name, False, online=True)136 #Si on est parmis les 10 meilleurs online137 if len(self.json_data_online) >= 5 and self.json_data_online[-1]["score"] < self.stats[0]:138 self.add_stats = True139 if name:140 self.save_score(name, True, online=True)141 142 """143 Méthode qui sauvegarde le score144 """145 def save_score(self, name, remove_last, online = False):146 if online: json_data = self.json_data_online147 else: json_data = self.json_data_local148 if remove_last:149 del json_data[-1]150 json_data.append({151 "name": name,152 "score": self.stats[0],153 "plane_kills": self.stats[1],154 })155 self.data_to_save = json_data #json data global pour la fonction Thread156 if online: Thread(target=self.save_online_score).start()157 else: set_save_arcade(json_data)158 """159 Méthode de gestion de l'affichage des flèches160 """161 def arrow_handler(self):162 #si les flèches doivent avancer on change les positions en fonction163 if self.forward == True:164 self.arrow_r_rect.left += 1165 self.arrow_l_rect.left -= 1166 #si la position de la fleches dépasse un maximum on la fait reculer167 if self.arrow_r_rect.left > self.arrow_r_left + ARROW_MOVE:168 self.forward = False169 #si les fleches doivent reculer on change les positions en fonction170 else:171 self.arrow_r_rect.left -= 1172 self.arrow_l_rect.left += 1173 #si la position de la fleche depasse sa position initiale on la fait a nouveau avancer174 if self.arrow_r_rect.left <= self.arrow_r_left:175 self.forward = True176 def event_handler(self):177 #on récupère les evenements178 for event in pygame.event.get():179 if event.type == pygame.KEYDOWN:180 #Si la fenetre d'un nouveau score est affiché181 if self.add_stats:182 #Gestion de la touche effacer183 if event.key == pygame.K_BACKSPACE and len(self.input_str) > 0:184 self.input_str = self.input_str[:-1]185 #Touche echap186 elif event.key == pygame.K_ESCAPE:187 self.add_stats = False188 #Touche entrer189 elif event.key == pygame.K_RETURN:190 if len(self.input_str) > 2:191 self.check_score(self.input_str)192 self.add_stats = False193 #Ecriture du texte dans l'input194 elif len(self.input_str) < 15 and len(self.input_str) >= 0:195 if event.key != pygame.K_BACKSPACE and event.key != pygame.K_TAB:196 self.input_str += event.unicode197 elif self.disp_stats:198 if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:199 self.disp_stats = False200 elif self.div_index == 2:201 if event.key == pygame.K_RETURN or event.key == pygame.K_ESCAPE:202 self.keep_drawing = False203 return False204 elif event.key == pygame.K_RIGHT and self.logo_index < len(self.logos) -1:205 self.logo_index += 1206 elif event.key == pygame.K_LEFT and self.logo_index > 0:207 self.logo_index -= 1208 elif event.key == pygame.K_LEFT and self.logo_index == 0:209 self.div_index -=1210 else:211 if event.key == pygame.K_RETURN or event.key == pygame.K_ESCAPE:212 self.keep_drawing = False213 return False214 #Navigation entre les tabulations215 elif event.key == pygame.K_RIGHT and self.div_index < 2:216 self.div_index += 1217 elif event.key == pygame.K_LEFT and self.div_index > 0:218 self.div_index -=1219 if self.getBasicEvent(event):220 self.keep_drawing = False221 return pygame.QUIT222 """223 Méthode de rendu final224 """225 def draw_score(self):226 while self.keep_drawing:227 self.now = time.get_ticks()228 #si il ya a un event retourné alors on le retourne a nouveau229 event = self.event_handler()230 if event != None:231 return event232 #Arrière plan233 self.screen.blit(self.bg, (0,0))234 #Fenètres de scores235 div_rect = self.divs[self.div_index].get_rect()236 div_rect.center = (WIDTH//2, HEIGHT//2)237 self.screen.blit(self.divs[self.div_index], div_rect)238 tab_rect = (self.tabs[0].get_rect(), self.tabs[1].get_rect(), self.tabs[2].get_rect())239 tab_rect[0].topleft = (div_rect.left + 100, div_rect.top)240 tab_rect[1].topleft = (tab_rect[0].right, div_rect.top)241 tab_rect[2].topleft = (tab_rect[1].right, div_rect.top)242 self.screen.blit(self.tabs[0], tab_rect[0])243 self.screen.blit(self.tabs[1], tab_rect[1])244 self.screen.blit(self.tabs[2], tab_rect[2])245 self.screen.blit(self.tabs_click[self.div_index], tab_rect[self.div_index])246 #En cas d'affichage des scores en ligne et si ils sont chargés247 if self.div_index == 1 and self.online_loaded:248 i = 0249 #On écrit tous les scores250 for score in self.json_data_online:251 name_text = self.write_font.render(str(score["name"]), True, WHITE)252 name_rect = name_text.get_rect()253 name_rect.topleft = (div_rect.left + 170, div_rect.top + 222 + 48*i)254 plane_text = self.write_font.render(str(score["plane_kills"]), True, WHITE)255 plane_rect = plane_text.get_rect()256 plane_rect.topleft = (div_rect.left + 480, div_rect.top + 222 + 48*i)257 score_text = self.write_font.render(str(score["score"]), True, WHITE)258 score_rect = score_text.get_rect()259 score_rect.topleft = (div_rect.left + 605, div_rect.top + 222 + 48*i)260 self.screen.blit(score_text, score_rect)261 self.screen.blit(name_text, name_rect)262 self.screen.blit(plane_text, plane_rect)263 i += 1264 elif self.div_index == 0:265 i = 0266 for score in self.json_data_local:267 name_text = self.write_font.render(str(score["name"]), True, WHITE)268 name_rect = name_text.get_rect()269 name_rect.topleft = (div_rect.left + 170, div_rect.top + 222 + 48*i)270 plane_text = self.write_font.render(str(score["plane_kills"]), True, WHITE)271 plane_rect = plane_text.get_rect()272 plane_rect.topleft = (div_rect.left + 480, div_rect.top + 222 + 48*i)273 score_text = self.write_font.render(str(score["score"]), True, WHITE)274 score_rect = score_text.get_rect()275 score_rect.topleft = (div_rect.left + 605, div_rect.top + 222 + 48*i)276 self.screen.blit(score_text, score_rect)277 self.screen.blit(name_text, name_rect)278 self.screen.blit(plane_text, plane_rect)279 i += 1280 elif self.div_index == 2:281 i = 0282 for logo in self.logos:283 284 logo_rect = logo.get_rect()285 logo_rect.topleft = (175 + i*150, 300)286 if i == self.logo_index:287 self.screen.blit(self.logos_click[self.logo_index], logo_rect)288 else:289 self.screen.blit(logo, logo_rect)290 i += 1291 #Si on doit afficher la fenetre d'un nouveau score292 if self.add_stats:293 window_rect = self.score_window.get_rect()294 window_rect.center = (WIDTH//2, HEIGHT//2)295 input_rect = rect.Rect(window_rect.left + 172, window_rect.top + 288, 388, 34)296 text = self.write_font.render(self.input_str, True, BLACK)297 self.screen.blit(self.overlay, (0,0))298 self.screen.blit(self.score_window, window_rect)299 self.screen.blit(text, input_rect)300 if self.disp_button_state:301 self.screen.blit(self.confirm_flash, (627, 402))302 #Si on doit afficher la fenetre d'un score au hasard303 elif self.disp_stats:304 window_rect = self.disp_score.get_rect()305 window_rect.center = (WIDTH//2, HEIGHT//2)306 btn_enter_rect = self.btn_enter[0].get_rect()307 btn_enter_rect.topleft = (600, 270)308 btn_rect = self.tab_score.get_rect()309 btn_rect.topleft = (window_rect.left+113, window_rect.top +13)310 text_score = self.write_font.render(str(self.stats[0]), True, WHITE)311 text_plane = self.write_font.render(str(self.stats[1]), True, WHITE)312 self.screen.blit(self.overlay, (0,0))313 self.screen.blit(self.disp_score, window_rect)314 self.screen.blit(self.tab_score, btn_rect)315 self.screen.blit(text_score, (410, 269))316 self.screen.blit(text_plane, (410, 342))317 if self.disp_button_state:318 self.screen.blit(self.btn_enter[1], btn_enter_rect)319 else:320 self.screen.blit(self.btn_enter[0], btn_enter_rect)321 322 #on fait bouger les fleches avec un certain délai pour pas que ca aille trop vite323 if self.now - self.last_anim > ARROW_DELAY:324 self.last_anim = self.now325 self.arrow_handler()326 327 #On fait clignoter le bouton de confirmation328 if self.now - self.last_flash > FLASH_DELAY:329 self.last_flash = self.now330 self.disp_button_state = not self.disp_button_state331 332 #on affiche les fleches333 if self.div_index == 0 and not self.add_stats and not self.disp_stats:334 self.screen.blit(self.arrow_r, self.arrow_r_rect)335 elif self.div_index == 2 and not self.add_stats and not self.disp_stats:336 self.screen.blit(self.arrow_l, self.arrow_l_rect) 337 elif not self.add_stats and not self.disp_stats:338 self.screen.blit(self.arrow_r, self.arrow_r_rect)339 self.screen.blit(self.arrow_l, self.arrow_l_rect)340 ...

Full Screen

Full Screen

pointer_tripleclick.py

Source:pointer_tripleclick.py Github

copy

Full Screen

1from tests.perform_actions.support.refine import get_events2from tests.support.asserts import assert_move_to_coordinates3from tests.support.helpers import filter_dict4lots_of_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor "\5 "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud "\6 "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."7def test_tripleclick_at_coordinates(session, mouse_chain, inline):8 """9 This test does a triple click on a coordinate. On desktop platforms10 this will select a paragraph. On mobile this will not have the same11 desired outcome as taps are handled differently on mobile.12 """13 session.url = inline("""<div>14 {}15 </div>""".format(lots_of_text))16 div = session.find.css("div", all=False)17 div_rect = div.rect18 div_centre = {19 "x": div_rect["x"] + int(div_rect["width"]/2),20 "y": div_rect["y"] + int(div_rect["height"]/2)21 }22 mouse_chain \23 .pointer_move(div_centre["x"], div_centre["y"]) \24 .click() \25 .click() \26 .click() \27 .perform()28 actual_text = session.execute_script("return document.getSelection().toString();")...

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 Airtest 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