How to use getRaw method in Airtest

Best Python code snippet using Airtest

mem.py

Source:mem.py Github

copy

Full Screen

...48 >>> myio = TestIO()49 >>> m.addSpecial(10, myio.dummy_get, None)50 >>> m.addSpecial(11, None, myio.dummy_set)51 >>> m.addSpecial(12, myio.dummy_get, myio.dummy_set)52 >>> print m.getRaw(0)53 054 >>> print m.getRaw(10)55 get at 1056 4257 >>> m.setRaw(10, 55)58 >>> m.setRaw(11, 6)59 set at 11, val 660 >>> print m.getRaw(11)61 062 >>> print m.getRaw(12)63 get at 1264 4265 >>> m.setRaw(12, 7)66 set at 12, val 767 >>> print m.getRaw(12)68 get at 1269 770 >>> print m.getRaw(13)71 072 >>> m.setRaw(13, 5)73 >>> print m.getRaw(13)74 575 >>> m.addSpecial(12, myio.dummy_get, myio.dummy_set) #doctest: +ELLIPSIS76 Traceback (most recent call last):77 ...78 ValueError: Special memory handler already registered to 1279 """80 if mem in self._specials:81 raise ValueError("Special memory handler already registered to %s" % mem)82 self._specials[mem] = (handler_get, handler_set)83 def getSpecial(self, pos):84 """ Get special location85 @pos Position of special memory86 """87 return self._specials.get(pos, None)88 try:89 return self._specials[pos]90 except KeyError:91 return None92 """93 for k in self._specials.keys():94 if pos >= k and pos < (k + 4):95 return self._specials[k]96 """97 def subMemory(self, mem, pos):98 """ Define this as a submemory on other memory instance99 @param mem Another memory instance100 @param pos Position in real memory101 """102 self._submem = mem103 self._sub_pos = pos104 def enlarge(self, size):105 """ Enlarge memory size106 @param size New memory size107 """108 self._size = size109 def setData(self, pos, data, size=4):110 """ Set data, can be sized as 1..size bytes111 @param pos Position112 @param data data113 @param size Maximum data length in bytes114 >>> m = Mem(100)115 >>> m.setData(4, 0x123456)116 >>> print "%x" % m.getData(4, 4)117 123456118 >>> m.setData(8, 0x12345678)119 >>> print "%x" % m.getData(8, 4)120 12345678121 >>> print "%x" % m.getData(8, 2)122 5678123 """124 tmp = data125 cnt = 0126 while tmp:127 bval = tmp & 0xFF128 tmp >>= 8129 self.setRaw(pos, bval)130 pos += 1131 cnt += 1132 if cnt > size:133 raise ValueError('Gave too big number: %s, %s > %s' % (data, cnt, size))134 break135 def getData(self, pos, size=1):136 """ Get data with size137 @param pos Position138 @param size Size in bytes139 @returns Data140 """141 res = 0142 while size > 0:143 tmp = self.getRaw(pos + size - 1)144 res = res | tmp145 size -= 1146 if size > 0:147 res <<= 8148 return res149 def getBlock(self, pos, size=1):150 """ Get memory blocks at given position and given size151 May return multiple blocks if size is big enough or boundaries crossed152 >>> m = Mem(0x1000*5+2)153 >>> m.setData(0xffe, 0x12345678)154 >>> len(m.getBlock(0xffe, 4))155 2156 >>> len(m.getBlock(0xff0, 4))157 1158 >>> len(m.getBlock(0x1010, 4))159 1160 >>> len(m.getBlock(0xffe, 0x1020))161 3162 """163 res = []164 start = int(pos / self._pagesize)165 items = int((pos + size) / self._pagesize)166 mod = (pos + size) % self._pagesize167 if start == 0:168 items += 1169 pos = start170 for i in xrange(items):171 try:172 res.append((pos, self._datas[pos]))173 except:174 res.append((pos, None))175 pos += 1176 return res177 def getPage(self, pos, create=False):178 """ Get page index179 >>> m = Mem(0x1000*3)180 >>> m.getPage(100)181 (None, None)182 >>> (page, index) = m.getPage(100, create=True)183 >>> index184 100185 >>> (page, index) = m.getPage(0x1002, create=True)186 >>> index187 2188 """189 index = int(pos / self._pagesize)190 if not index in self._datas:191 if not create:192 return (None, None)193 self._datas[index] = bytearray(self._pagesize)194 subindex = (pos) - (index * self._pagesize)195 return (self._datas[index], subindex)196 def setRaw(self, pos, data):197 """ Set raw memory value at position to given data198 @param pos Index199 @param data Data to set200 >>> m = Mem(100)201 >>> m.setRaw(0, 100)202 >>> m.setRaw(1, 42)203 >>> m.getRaw(0)204 100205 >>> m.getRaw(1)206 42207 >>> m2 = Mem()208 >>> m2.setRaw(0, 100)209 >>> m2.setRaw(1, 200)210 >>> m2.setData(2, 300)211 >>> m2.setData(2000, 123456)212 >>> m2.getRaw(0)213 100214 >>> m2.getRaw(1)215 200216 >>> m2.getData(2, 2)217 300218 >>> m2.getRaw(1999)219 0220 >>> m2.getData(2000, 3)221 123456222 >>> m2.getRaw(2010) #doctest: +ELLIPSIS223 Traceback (most recent call last):224 ...225 IndexError: Given memory position is invalid: 2010, max size: ...226 >>> m.setRaw(9, 10)227 >>> m.setRaw(11, 42)228 >>> m2 = Mem()229 >>> m2.subMemory(m, 9)230 >>> print m2.getRaw(0)231 10232 >>> print m2.getRaw(1)233 0234 >>> print m2.getRaw(2)235 42236 >>> m3 = Mem(10)237 >>> m3.subMemory(m, 0)238 >>> print m3.getRaw(0)239 100240 >>> print m3.getRaw(1)241 42242 >>> print m3.getRaw(9)243 10244 >>> print m3.getRaw(10)245 0246 >>> print m3.getRaw(11) #doctest: +ELLIPSIS247 Traceback (most recent call last):248 ...249 IndexError: Sub memory size limit hit!250 """251 if self._submem is not None:252 if self._size > 0 and pos > self._size:253 raise IndexError("Sub memory size limit hit!")254 return self._submem.setRaw(self._sub_pos + pos, data)255 if self._size == 0:256 self.enlarge(pos + 1)257 special = self.getSpecial(pos)258 if self._size < (pos + 1) and special is None:259 if self._autosize:260 self.enlarge(pos + 1)261 else:262 #print ("%s" % pos, self._specials.keys())263 raise IndexError("Given memory position is invalid: %s, max size: %s" % (pos, self._size))264 if pos < 0:265 raise IndexError("Memory position needs to be positive number, got: %s" % (pos))266 if special is not None:267 (hget, hset) = special268 if hset is not None:269 return hset(pos, data)270 return271 (page, subindex) = self.getPage(pos, create=True)272 page[subindex] = data273 def getRaw(self, pos):274 """ Get data from specified position275 @param pos Index276 @returns Data277 """278 if self._submem is not None:279 if self._size > 0 and pos > self._size:280 raise IndexError("Sub memory size limit hit!")281 return self._submem.getRaw(self._sub_pos + pos)282 special = self.getSpecial(pos)283 if self._size < pos and special is None:284 raise IndexError("Given memory position is invalid: %s, max size: %s" % (pos, self._size))285 if pos < 0:286 raise IndexError("Memory position needs to be positive number, got: %s" % (pos))287 if special is not None:288 (hget, hset) = special289 if hget is not None:290 return hget(pos)291 return 0292 (page, subindex) = self.getPage(pos)293 if page == None:294 return 0295 return page[subindex]

Full Screen

Full Screen

data.py

Source:data.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2# Author: Vladimir Pichugin <vladimir@pichug.in>3import datetime4import dateutil.parser5from bson.objectid import ObjectId6from settings import Settings7class SDict(dict):8 def __init__(self, *args, **kwargs):9 self.changed = False10 super().__init__(*args, **kwargs)11 def __getitem__(self, item):12 return super().__getitem__(item)13 def __setitem__(self, item, value):14 try:15 if super().__getitem__(item) != value:16 self.changed = True17 except KeyError:18 self.changed = True19 return super().__setitem__(item, value)20 def __delitem__(self, item):21 self.changed = True22 super().__delitem__(item)23 def getraw(self, item, default=None):24 try:25 return super().__getitem__(item)26 except KeyError:27 return default28 def setraw(self, item, value):29 super().__setitem__(item, value)30 def delraw(self, item):31 super().__delitem__(item)32class Contract(SDict):33 def __init__(self, *args, **kwargs):34 super().__init__(*args, **kwargs)35 @staticmethod36 def create_from_form_data(form):37 f = Settings.CONTRACT_OBJECT38 f.update(form)39 return Contract(40 program=f.get('program'),41 customer_email=f.get('customer[email]'),42 customer_phone=f.get('customer[phone]'),43 customer_role=f.get('customer[role]'),44 customer_lastName=f.get('customer[lastName]'),45 customer_firstName=f.get('customer[firstName]'),46 customer_middleName=f.get('customer[firstName]'),47 customer_birthDate=f.get('customer[birthDate]'),48 customer_sex=f.get('customer[sex]'),49 customer_docType=f.get('customer[docType]'),50 customer_docSerial=f.get('customer[docSerial]'),51 customer_docNumber=f.get('customer[docNumber]'),52 customer_docIssued=f.get('customer[docIssued]'),53 customer_docWhenIssued=f.get('customer[docWhenIssued]'),54 customer_address=f.get('customer[address]'),55 student_email=f.get('student[email]'),56 student_phone=f.get('student[phone]'),57 student_lastName=f.get('student[lastName]'),58 student_firstName=f.get('student[firstName]'),59 student_middleName=f.get('student[middleName]'),60 student_sex=f.get('student[sex]'),61 student_birthDate=f.get('student[birthDate]'),62 student_docType=f.get('student[docType]'),63 student_docSerial=f.get('student[docSerial]'),64 student_docNumber=f.get('student[docNumber]'),65 student_docIssued=f.get('student[docIssued]'),66 student_docWhenIssued=f.get('student[docWhenIssued]'),67 student_address=f.get('student[address]')68 )69 def get_obj_id(self) -> ObjectId:70 return self.getraw('_id')71 def get_contract_id(self):72 return self.getraw('contract_id')73 def get_session_id(self):74 return self.getraw('session_id')75 def get_program(self):76 return self.getraw('program')77 def get_confirm_code(self):78 return self.getraw('confirm_code')79 def get_to_addr(self):80 return self.getraw('customer_email')81 def get_to_name(self):82 return self.get_name()83 def get_customer_name(self):84 return self.getraw('customer_firstName')85 def get_customer_age(self):86 return self.get_age()87 def get_customer_role(self):88 return self.getraw('customer_role')89 def get_student_age(self):90 return self.get_age('student')91 def get_name(self, t='customer', include_middle_name=False) -> str:92 name = []93 if t:94 t += '_'95 else:96 t = ''97 if self.getraw(f'{t}firstName'):98 name.append(self.getraw(f'{t}firstName'))99 if self.getraw(f'{t}middleName') and include_middle_name:100 name.append(self.getraw(f'{t}middleName'))101 if self.getraw(f'{t}lastName'):102 name.append(self.getraw(f'{t}lastName'))103 if name:104 return ' '.join(name)105 return ''106 def get_age(self, t='customer') -> int:107 birth_date = self._decode_date(f'{t}_birthDate')108 if not birth_date:109 return 0110 today = datetime.date.today()111 return (today.year - birth_date.year -112 ((today.month, today.day) <113 (birth_date.month,114 birth_date.day)))115 def get_sex(self, t='customer') -> str:116 value = self.getraw(f'{t}_sex')117 return 'female' if value == '2' else 'male'118 def get_dt(self, key, default=None):119 return self._decode_date(key) or default120 def _decode_date(self, key):121 birth_date = self.getraw(key)122 if not birth_date:123 return None124 return dateutil.parser.parse(birth_date)125 def created_dt(self) -> datetime.datetime:126 return datetime.datetime.fromtimestamp(self.getraw('timestamp_created'))127class MessageDeliveryReport(SDict):128 def __init__(self, *args, **kwargs):129 super().__init__(*args, **kwargs)130 self.id = self.getraw('_id')131 @staticmethod132 def create(data):...

Full Screen

Full Screen

spider_config.py

Source:spider_config.py Github

copy

Full Screen

...25 而是在程序初始化的时候统一加载,各处直接调用26 """27 def __init__(self):28 # config 的 config29 self.USE_COOKIE_POOL = True if global_config.getRaw('config', 'use_cookie_pool') == 'True' else False30 self.COOKIE = global_config.getRaw('config', 'Cookie')31 self.USER_AGENT = global_config.getRaw('config', 'user-agent')32 self.SAVE_MODE = global_config.getRaw('config', 'save_mode')33 self.MONGO_PATH = global_config.getRaw('config', 'mongo_path')34 self.REQUESTS_TIMES = global_config.getRaw('config', 'requests_times')35 self.UUID = global_config.getRaw('config', 'uuid')36 self.TCV = global_config.getRaw('config', 'tcv')37 # config 的 detail38 self.KEYWORD = global_config.getRaw('detail', 'keyword')39 self.LOCATION_ID = global_config.getRaw('detail', 'location_id')40 self.CHANNEL_ID = global_config.getRaw('detail', 'channel_id')41 self.SEARCH_URL = global_config.getRaw('detail', 'search_url')42 assert self.SEARCH_URL == '' or self.SEARCH_URL.endswith('p'), 'search_url 没有拼接p'43 self.NEED_FIRST = True if global_config.getRaw('detail', 'need_first') == 'True' else False44 try:45 self.NEED_SEARCH_PAGES = int(global_config.getRaw('detail', 'need_pages'))46 except:47 logger.error('need_pages 必须为整数')48 exit()49 # config 的 proxy50 self.USE_PROXY = True if global_config.getRaw('proxy', 'use_proxy') == 'True' else False51 if self.USE_PROXY:52 try:53 self.REPEAT_NUMBER = int(global_config.getRaw('proxy', 'repeat_nub'))54 except:55 logger.error('repeat_nub 必须为整数')56 exit()57 else:58 self.REPEAT_NUMBER = 059 self.HTTP_EXTRACT = True if global_config.getRaw('proxy', 'http_extract') == 'True' else False60 self.HTTP_LINK = global_config.getRaw('proxy', 'http_link')61 self.KEY_EXTRACT = True if global_config.getRaw('proxy', 'key_extract') == 'True' else False62 self.PROXY_HOST = global_config.getRaw('proxy', 'proxy_host').strip()63 self.PROXY_PORT = global_config.getRaw('proxy', 'proxy_port').strip()64 self.KEY_ID = global_config.getRaw('proxy', 'key_id')65 self.KEY_KEY = global_config.getRaw('proxy', 'key_key')66 assert not (self.HTTP_EXTRACT is True and self.KEY_EXTRACT is True), '代理模式不可以全为True'67 # require 的 shop phone68 self.NEED_DETAIL = True if require_config.getRaw('shop_phone', 'need') == 'True' else False69 self.NEED_PHONE_DETAIL = True if require_config.getRaw('shop_phone', 'need_detail') == 'True' else False70 if self.NEED_PHONE_DETAIL:71 logger.warn('开启了电话详情模式,会降低速度并增加反爬概率')72 # require 的 shop review73 self.NEED_REVIEW = True if require_config.getRaw('shop_review', 'need') == 'True' else False74 self.NEED_REVIEW_DETAIL = True if require_config.getRaw('shop_review', 'more_detail') == 'True' else False75 if self.NEED_REVIEW_DETAIL:76 logger.warn('开启了评论详情模式,会降低速度并增加反爬概率')77 try:78 self.NEED_REVIEW_PAGES = int(require_config.getRaw('shop_review', 'need_pages'))79 except:80 logger.error('need_pages 必须为整数')81 exit()82 else:83 self.NEED_REVIEW_PAGES = 0...

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