Best Python code snippet using Airtest
HardwareCommands.py
Source:HardwareCommands.py  
...95        #factor of 4 in frame interval is because the plc clock runs at 4 khz, so 4 clock96        #ticks is 1 ms97        for exception_count in range(self.exception_limit):98            try:99                self.core.wait_for_device(self.plc_name)100                self.core.wait_for_device(self.tiger_name)101                trigger_pulse_width = 80102                frame_interval = np.ceil((step_size / z_scan_speed) * 4)103                104                self.set_property(self.plc_name, self.prop_position, self.addr_stage_ttl)105                self.set_property(self.plc_name, self.prop_cell_type, self.val_input)106                self.set_property(self.plc_name, self.prop_position, self.addr_delay_1)107                self.set_property(self.plc_name, self.prop_cell_type, self.val_delay)108                self.set_property(self.plc_name, self.prop_cell_config, 0)109                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_stage_ttl)110                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)111                self.set_property(self.plc_name, self.prop_position, self.addr_or)112                self.set_property(self.plc_name, self.prop_cell_type, self.val_or)113                self.set_property(self.plc_name, self.prop_cell_config, 0)114                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_delay_1)115                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_delay_2)116                self.set_property(self.plc_name, self.prop_position, self.addr_and)117                self.set_property(self.plc_name, self.prop_cell_type, self.val_and)118                self.set_property(self.plc_name, self.prop_cell_config, 0)119                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_or)120                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_stage_ttl)121                self.set_property(self.plc_name, self.prop_position, self.addr_delay_2)122                self.set_property(self.plc_name, self.prop_cell_type, self.val_delay)123                self.set_property(self.plc_name, self.prop_cell_config, frame_interval)124                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_and)125                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)126                self.set_property(self.plc_name, self.prop_position, self.addr_one_shot)127                self.set_property(self.plc_name, self.prop_cell_type, self.val_one_shot)128                self.set_property(self.plc_name, self.prop_cell_config, trigger_pulse_width)129                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_delay_2)130                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)131                self.set_property(self.plc_name, self.prop_position, self.addr_bnc_1)132                self.set_property(self.plc_name, self.prop_cell_type, self.val_output)133                self.set_property(self.plc_name, self.prop_cell_config, self.addr_one_shot)134                self.set_property(self.plc_name, self.prop_cell_input_1, 0)135                self.set_property(self.plc_name, self.prop_cell_input_2, 0)136                #self.set_property(self.plc_name, self.prop_position, self.addr_bnc_3)137                #self.set_property(self.plc_name, self.prop_cell_type, self.val_output)138                #self.set_property(self.plc_name, self.prop_cell_config, self.addr_stage_ttl)139                #self.set_property(self.plc_name, self.prop_cell_input_1, 0)140                #self.set_property(self.plc_name, self.prop_cell_input_2, 0)141                #self.set_property(self.plc_name, self.prop_position, self.addr_bnc_4)142                #self.set_property(self.plc_name, self.prop_cell_type, self.val_output)143                #self.set_property(self.plc_name, self.prop_cell_config, self.addr_one_shot)144                #self.set_property(self.plc_name, self.prop_cell_input_1, 0)145                #self.set_property(self.plc_name, self.prop_cell_input_2, 0)146            except Exception as error:147                logging.exception(error)148                exception = "exception raised during initialize_plc_for_scan(), retry attempt " + str(exception_count + 1)149                logging.exception(exception)150                self.studio.logs().log_message(exception)151            else:152                return153        exception = "HardwareCommands.py initialize_plc_for_scan() failed"154        logging.exception(exception)155        self.studio.logs().log_message(exception)156        return157    def set_plc_frame_interval(self, step_size, z_scan_speed):158        for exception_count in range (self.exception_limit):159            try:160                self.core.wait_for_device(self.plc_name)161                self.core.wait_for_device(self.tiger_name)162                frame_interval = np.ceil((step_size / z_scan_speed) * 4)163                self.set_property(self.plc_name, self.prop_position, self.addr_delay_2)164                self.set_property(self.plc_name, self.prop_cell_type, self.val_delay)165                self.set_property(self.plc_name, self.prop_cell_config, frame_interval)166                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_and)167                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)168            except Exception as error:169                logging.exception(error)170                exception = "exception raised during set_plc_frame_interval(), retry attempt " + str(exception_count + 1)171                logging.exception(exception)172                self.studio.logs().log_message(exception)173            else:174                return175        exception = "HardwareCommands.py set_plc_frame_interval() failed"176        logging.exception(exception)177        self.studio.logs().log_message(exception)178        return179    def initialize_plc_for_continuous_lsrm(self, framerate):180        # Same as the last PLC function except it pulses on its own.181        for exception_count in range (self.exception_limit):182            try:183                self.core.wait_for_device(self.plc_name)184                self.core.wait_for_device(self.tiger_name)185                trigger_pulse_width = 4186                frame_interval = np.ceil(1.0 / framerate * 1000 * 4)187                self.set_property(self.plc_name, self.prop_position, self.addr_delay_1)188                self.set_property(self.plc_name, self.prop_cell_type, self.val_delay)189                self.set_property(self.plc_name, self.prop_cell_config, 0)190                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_constant)191                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)192                self.set_property(self.plc_name, self.prop_position, self.addr_or)193                self.set_property(self.plc_name, self.prop_cell_type, self.val_or)194                self.set_property(self.plc_name, self.prop_cell_config, 0)195                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_delay_1)196                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_delay_2)197                self.set_property(self.plc_name, self.prop_position, self.addr_and)198                self.set_property(self.plc_name, self.prop_cell_type, self.val_and)199                self.set_property(self.plc_name, self.prop_cell_config, 0)200                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_or)201                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_constant)202                self.set_property(self.plc_name, self.prop_position, self.addr_delay_2)203                self.set_property(self.plc_name, self.prop_cell_type, self.val_delay)204                self.set_property(self.plc_name, self.prop_cell_config, frame_interval)205                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_and)206                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)207                self.set_property(self.plc_name, self.prop_position, self.addr_one_shot)208                self.set_property(self.plc_name, self.prop_cell_type, self.val_one_shot)209                self.set_property(self.plc_name, self.prop_cell_config, trigger_pulse_width)210                self.set_property(self.plc_name, self.prop_cell_input_1, self.addr_delay_2)211                self.set_property(self.plc_name, self.prop_cell_input_2, self.addr_clk)212                self.set_property(self.plc_name, self.prop_position, self.addr_bnc_1)213                self.set_property(self.plc_name, self.prop_cell_type, self.val_output)214                self.set_property(self.plc_name, self.prop_cell_config, self.addr_one_shot)215                self.set_property(self.plc_name, self.prop_cell_input_1, 0)216                self.set_property(self.plc_name, self.prop_cell_input_2, 0)217                self.set_property(self.plc_name, self.prop_position, self.addr_constant)218                self.set_property(self.plc_name, self.prop_cell_type, self.val_constant)219                self.set_property(self.plc_name, self.prop_cell_config, 1)220            except Exception as error:221                logging.exception(error)222                exception = "exception raised during initialize_plc_for_continuous_lsrm(), retry attempt " + str(exception_count + 1)223                logging.exception(exception)224                self.studio.logs().log_message(exception)225            else:226                return227                228        exception = "HardwareCommands.py initialize_plc_for_continuous_lsrm() failed"229        logging.exception(exception)230        self.studio.logs().log_message(exception)231        return232    def set_dslm_camera_properties(self, z_scan_speed):233        #Sets camera properties for a DSLM zstack234        for exception_count in range (self.exception_limit):235            try:236                self.core.wait_for_device(self.cam_name)237                self.core.stop_sequence_acquisition()238                self.studio.live().set_live_mode_on(False)239                exposure = 20240                self.core.set_exposure(exposure)241                self.set_property(self.cam_name, self.trigger_mode_prop, self.scan_trigger_mode)242                self.set_property(self.cam_name, self.acquire_mode_prop, self.scan_acquire_mode)243            except Exception as error:244                logging.exception(error)245                exception = "exception raised during set_dslm_camera_properties(), retry attempt " + str(exception_count + 1)246                logging.exception(exception)247                self.studio.logs().log_message(exception)248            else:249                return250        exception = "HardwareCommands.py set_dslm_camera_properties() failed"251        logging.exception(exception)252        self.studio.logs().log_message(exception)253        return254    def set_default_camera_properties(self, exposure):255        #Sets camera properties to default.256        for exception_count in range (self.exception_limit):257            try:258                self.core.wait_for_device(self.cam_name)259                self.core.stop_sequence_acquisition()260                self.studio.live().set_live_mode_on(False)261                self.set_property(self.cam_name, self.trigger_mode_prop, self.default_trigger_mode)262                self.set_property(self.cam_name, self.acquire_mode_prop, self.default_acquire_mode)263                self.core.set_exposure(exposure)264            except Exception as error:265                logging.exception(error)266                exception = "exception raised during set_default_camera_properties(), retry attempt " + str(exception_count + 1)267                logging.exception(exception)268                self.studio.logs().log_message(exception)269            else:270                return271                272        exception = "HardwareCommands.py set_default_camera_properties() failed"273        logging.exception(exception)274        self.studio.logs().log_message(exception)275        return276    def set_x_stage_speed(self, speed):277        #Sets x-stage speed278        for exception_count in range (self.exception_limit):279            try:280                self.core.wait_for_device(self.zy_stage_name)281                self.core.wait_for_device(self.x_stage_name)282                self.set_property(self.x_stage_name, self.x_speed_property, speed)283            except Exception as error:284                logging.exception(error)285                exception = "exception raised during set_x_stage_speed(), retry attempt " + str(exception_count + 1)286                logging.exception(exception)287                self.studio.logs().log_message(exception)288            else:289                return290            291        exception = "HardwareCommands.py set_x_stage_speed() failed"292        logging.exception(exception)293        self.studio.logs().log_message(exception)294        return295    def set_zy_stage_speed(self, speed):296        #Sets zy-stage speed. Used to switch between z-stage speed when297        #moving to new position and the speed used during scans.298        for exception_count in range (self.exception_limit):299            try:300                self.core.wait_for_device(self.zy_stage_name)301                self.core.wait_for_device(self.x_stage_name)302                self.set_property(self.zy_stage_name, self.z_speed_property, speed)303            except Exception as error:304                logging.exception(error)305                exception = "exception raised during set_zy_stage_speed(), retry attempt " + str(exception_count + 1)306                logging.exception(exception)307                self.studio.logs().log_message(exception)308            else:309                return310        exception = "HardwareCommands.py set_zy_stage_speed() failed"311        logging.exception(exception)312        self.studio.logs().log_message(exception)313        return314    def scan_setup(self, start_z, end_z):315        """316        SCAN is a module on the ASI stage. Please read the ASI manual for more details317        The '2' in all of the commands is the address of the Z Stage card as opposed to the318        XY Stage, which is '1'. An ASI stage scan is achieved by doing the following:319        1. Scan properties are set as "2 SCAN Y=0 Z=0 F=0". This is simply320           to tell the stage what axis will be scaning.321        2. Positions are set with SCANR X=[StartPosition] Y=[EndPosition]322           where positions are in units of mm. SCANR means raster scan.323        3. "2 SCAN" is sent. When the stage reaches the first position, the TTL 324           port goes high. This is what triggers the PLC to pulse. Once it reaches325           the end, TTL goes low and the stage resets to the start position.326        """327        for exception_count in range (self.exception_limit):328            try:329                self.core.wait_for_device(self.zy_stage_name)330                self.core.wait_for_device(self.x_stage_name)331                start_z = np.round(start_z) / 1000.332                end_z = np.round(end_z) / 1000.333                scan_r_properties = "SCANR X=" + str(start_z) + " Y=" + str(end_z)334                self.set_property(self.zy_stage_name, self.serial, self.scan_properties)335                self.set_property(self.zy_stage_name, self.serial, scan_r_properties)336            except Exception as error:337                logging.exception(error)338                exception = "exception raised during scan_setup(), retry attempt " + str(exception_count + 1)339                logging.exception(exception)340                self.studio.logs().log_message(exception)341            else:342                return343        exception = "HardwareCommands.py scan_setup() failed"344        logging.exception(exception)345        self.studio.logs().log_message(exception)346        return347    def scan_start(self):348        #Start scan with properties set in scan_setup.349        for exception_count in range (self.exception_limit):350            try:351                self.core.wait_for_device(self.cam_name)352                self.core.wait_for_device(self.plc_name)353                self.core.wait_for_device(self.tiger_name)354                self.core.wait_for_device(self.zy_stage_name)355                self.core.wait_for_device(self.x_stage_name)356                self.set_property(self.zy_stage_name, self.serial, self.scan_start_command)357                358            except Exception as error:359                logging.exception(error)360                exception = "exception raised during scan_start(), retry attempt " + str(exception_count + 1)361                logging.exception(exception)362                self.studio.logs().log_message(exception)363            else:364                return365        exception = "HardwareCommands.py scan_start() failed"366        logging.exception(exception)367        self.studio.logs().log_message(exception)368        return369    370    def move_stage(self, x_position, y_position, z_position):371        for exception_count in range (self.exception_limit):372            try:373                self.core.wait_for_device(self.zy_stage_name)374                self.core.wait_for_device(self.x_stage_name)375                self.set_zy_stage_speed(self.zy_stage_speed)376                self.set_x_stage_speed(self.x_stage_speed)377       378                #Reason for this is to ensure capillaries dpn't hit the objective. These conditions379                #should be changed to match the geometry of the holder.380                current_x_position = self.get_x_position()381                if current_x_position > x_position:382                    self.core.set_xy_position(self.zy_stage_name, z_position, y_position)383                    self.core.wait_for_device(self.zy_stage_name)384                    self.core.set_position(self.x_stage_name, x_position)385                    self.core.wait_for_device(self.x_stage_name)386                else:387                    self.core.set_position(self.x_stage_name, x_position)388                    self.core.wait_for_device(self.x_stage_name)389                    self.core.set_xy_position(self.zy_stage_name, z_position, y_position)390                    self.core.wait_for_device(self.zy_stage_name)391            except Exception as error:392                logging.exception(error)393                exception = "exception raised during move_stage(), retry attempt " + str(exception_count + 1)394                logging.exception(exception)395                self.studio.logs().log_message(exception)396            else:397                return398        exception = "HardwareCommands.py move_stage() failed"399        logging.exception(exception)400        self.studio.logs().log_message(exception)401        return402    def get_x_position(self):403        #Gets current x-position from xy-stage404        for exception_count in range (self.exception_limit):405            try:406                self.core.wait_for_device(self.zy_stage_name)407                self.core.wait_for_device(self.x_stage_name)408                x_pos =  int(np.round(self.core.get_position(self.x_stage_name)))409            except Exception as error:410                logging.exception(error)411                exception = "exception raised during get_x_position(), retry attempt " + str(exception_count + 1)412                logging.exception(exception)413                self.studio.logs().log_message(exception)414            else:415                return x_pos416        exception = "HardwareCommands.py get_x_position() failed"417        logging.exception(exception)418        self.studio.logs().log_message(exception)419        return 0420    def get_y_position(self):421        #Gets current y-position from xy-stage422        for exception_count in range (self.exception_limit):423            try:424                self.core.wait_for_device(self.zy_stage_name)425                self.core.wait_for_device(self.x_stage_name)426                y_pos =  int(np.round(self.core.get_y_position(self.zy_stage_name)))427            except Exception as error:428                logging.exception(error)429                exception = "exception raised during get_y_position(), retry attempt " + str(exception_count + 1)430                logging.exception(exception)431                self.studio.logs().log_message(exception)432            else:433                return y_pos434        exception = "HardwareCommands.py get_y_position() failed"435        logging.exception(exception)436        self.studio.logs().log_message(exception)437        return 0438    def get_z_position(self):439        #Gets current y-position from z-stage440        for exception_count in range (self.exception_limit):441            try:442                self.core.wait_for_device(self.zy_stage_name)443                self.core.wait_for_device(self.x_stage_name)444                z_pos =  int(np.round(self.core.get_x_position(self.zy_stage_name)))445            except Exception as error:446                logging.exception(error)447                exception = "exception raised during get_z_position(), retry attempt " + str(exception_count + 1)448                logging.exception(exception)449                self.studio.logs().log_message(exception)450            else:451                return z_pos452        exception = "HardwareCommands.py get_z_position() failed"453        logging.exception(exception)454        self.studio.logs().log_message(exception)455        return 0456    def reset_joystick(self):457        #The joystick tends to bug out after the SCAN command. This resets458        #the joystick so that it works correctly. See the ASI documentation 459        #for more details.460        for exception_count in range (self.exception_limit):461            try:462                self.core.wait_for_device(self.zy_stage_name)463                self.core.wait_for_device(self.x_stage_name)464                self.set_property(self.zy_stage_name, self.serial, "J X+ Y+ Z+")465                self.set_property(self.zy_stage_name, self.serial, "J X=4 Y=3 Z=2")466                self.set_property(self.zy_stage_name, self.serial, "JSSPD Z=5")467            except Exception as error:468                logging.exception(error)469                exception = "exception raised during reset_joystick(), retry attempt " + str(exception_count + 1)470                logging.exception(exception)471                self.studio.logs().log_message(exception)472            else:473                return474        exception = "HardwareCommands.py reset_joystick() failed"475        logging.exception(exception)476        self.studio.logs().log_message(exception)477        return478    def reset_stage(self):479        for exception_count in range (self.exception_limit):480            try:481                self.core.wait_for_device(self.zy_stage_name)482                self.core.wait_for_device(self.x_stage_name)483                self.set_property(self.zy_stage_name, self.serial, "RESET")484            except Exception as error:485                logging.exception(error)486                exception = "exception raised during reset_stage(), retry attempt " + str(exception_count + 1)487                logging.exception(exception)488                self.studio.logs().log_message(exception)489            else:490                return491        exception = "HardwareCommands.py reset_stage() failed"492        logging.exception(exception)493        self.studio.logs().log_message(exception)494        return495    def set_ensync_position(self):496        for exception_count in range (self.exception_limit):497            try:498                self.core.wait_for_device(self.zy_stage_name)499                self.core.wait_for_device(self.x_stage_name)500                self.set_property(self.zy_stage_name, self.serial, "ES X=400 Y=400 Z=400")501            except Exception as error:502                logging.exception(error)503                exception = "exception raised during set_ensync_position(), retry attempt " + str(exception_count + 1)504                logging.exception(exception)505                self.studio.logs().log_message(exception)506            else:507                return508        exception = "HardwareCommands.py set_ensync_position() failed"509        logging.exception(exception)510        self.studio.logs().log_message(exception)...hardware_UsbMount.py
Source:hardware_UsbMount.py  
...26        @param mount_cycles: how many times to mount/unount Default: 10.27        @param filter_dict: storage dictionary filter.28               Default: match any device connected on the USB bus.29        """30        # wait_for_device() returns (device_dictionary,31        # time_spent_looking_for_it), and only the dictionary is relevant for32        # this test33        storage = self.wait_for_device(filter_dict, cycles=1,34                                       mount_volume=True)[0]35        if not os.path.ismount(storage['mountpoint']):36            raise error.TestFail('filesystem %s mount failed' % filter_dict)37        storage_filter = {'fs_uuid': storage['fs_uuid']}38        # We cannot use autotemp.tempfile since we should close the descriptors39        # everytime the storage device is un-mounted.40        self._tmpfile = os.path.join(storage['mountpoint'],41                                     'tempfile_usb_mount.tmp')42        while mount_cycles:43            mount_cycles -= 144            # Create a 1MiB random file and checksum it.45            storage_mod.create_file(self._tmpfile, 1*1024*1024)46            chksum = storage_mod.checksum_file(self._tmpfile)47            logging.debug('storage to umount %s', storage)48            # Umount the volume.49            self.scanner.umount_volume(storage_dict=storage)50            storage = self.wait_for_device(storage_filter,51                                           mount_volume=False)[0]52            if os.path.ismount(storage['mountpoint']):53                raise error.TestFail('filesystem %s unmount failed ' %54                                     storage_filter)55            # Mount the volume back.56            self.scanner.mount_volume(storage_dict=storage)57            storage =  self.wait_for_device(storage_filter,58                                            mount_volume=False)[0]59            if not os.path.ismount(storage['mountpoint']):60                raise error.TestFail('filesystem %s mount failed' %61                                     storage_filter)62            # Check that the created file exists and has the same content.63            if not os.path.isfile(self._tmpfile):64                raise error.TestFail('%s: file not present after remounting' %65                                     self._tmpfile)66            if chksum != storage_mod.checksum_file(self._tmpfile):67                raise error.TestFail('%s: file content changed after '68                                     'remounting' % self._tmpfile)69        # Mount it, suspend and verify that after suspend-to-ram the70        # device is still mounted71        self.scanner.mount_volume(storage_dict=storage)72        storage = self.wait_for_device(storage_filter, mount_volume=False)[0]73        if not os.path.ismount(storage['mountpoint']):74            raise error.TestFail('filesystem %s mount failed ' % storage)75        sys_power.do_suspend(self.SECS_TO_SUSPEND)76        # mount_volume=False because we don't want the method to mount if77        # unmonted: we need to check its actual status right after suspend78        storage = self.wait_for_device(storage_filter, mount_volume=False)[0]79        if not os.path.ismount(storage['mountpoint']):80            raise error.TestFail('filesystem %s not mounted after suspend' %81                                 storage_filter)82        if not os.path.isfile(self._tmpfile):83            raise error.TestFail('%s: file not present anymore after '84                                 'remounting' % self._tmpfile)85        if chksum != storage_mod.checksum_file(self._tmpfile):86            raise error.TestFail('%s: file content changed after remounting' %...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
