Best Testsigma code snippet using com.testsigma.agent.mobile.android.AndroidDeviceListener.waitForAdbInitialization
Source:AndroidDeviceListener.java
...52 public void initializeNativeBridge() throws NativeBridgeException {53 try {54 this.adBridge = adbBridge.getADBInstance();55 if (!adBridge.hasInitialDeviceList()) {56 waitForAdbInitialization();57 }58 bridgeInitialized = true;59 } catch (Exception e) {60 log.error(e.getMessage(), e);61 throw new NativeBridgeException(e.getMessage(), e);62 }63 }64 public void getInitialDeviceList() {65 try {66 if (agentConfig.getRegistered().equals(Boolean.FALSE)) {67 log.debug("Skipping initial agent devices collection since agent is not registered...");68 return;69 }70 log.debug("Started getting initial agent devices connected...");71 IDevice[] devices = adBridge.getDevices();72 for (IDevice device : devices) {73 if (IDevice.DeviceState.ONLINE.equals(device.getState())) {74 MobileDevice mobileDevice = mobileDeviceMapper.map(device);75 mobileDevice.setIDevice(device);76 populateOtherAttributes(mobileDevice, device);77 this.addDevice(mobileDevice);78 }79 }80 } catch (Exception e) {81 log.error(e.getMessage(), e);82 }83 }84 public void addDeviceListenerCallback() throws TestsigmaException {85 try {86 if (agentConfig.getRegistered().equals(Boolean.FALSE)) {87 log.debug("Skipping agent devices listener callback registration since agent is not registered...");88 return;89 }90 log.debug("Registering agent device listener callbacks...");91 AndroidDebugBridge.addDeviceChangeListener(this);92 } catch (Exception e) {93 log.error(e.getMessage(), e);94 throw new TestsigmaException(e.getMessage(), e);95 }96 }97 public void removeDeviceListenerCallback() throws TestsigmaException {98 try {99 if (agentConfig.getRegistered().equals(Boolean.FALSE)) {100 log.debug("Skipping agent devices listener callback de-registration since agent is not registered...");101 return;102 }103 log.debug("De-Registering agent device listener callbacks...");104 AndroidDebugBridge.removeDeviceChangeListener(this);105 } catch (Exception e) {106 log.error(e.getMessage(), e);107 throw new TestsigmaException(e.getMessage(), e);108 }109 }110 @Override111 public void deviceConnected(IDevice device) {112 log.info("Device connected event received by Listener");113 try {114 if (IDevice.DeviceState.ONLINE.equals(device.getState())) {115 MobileDevice mobileDevice = mobileDeviceMapper.map(device);116 mobileDevice.setIDevice(device);117 populateOtherAttributes(mobileDevice, device);118 this.addDevice(mobileDevice);119 }120 } catch (Exception e) {121 log.error(e.getMessage(), e);122 }123 }124 @Override125 public void deviceDisconnected(IDevice device) {126 log.info("Device disconnected event received by Listener");127 try {128 MobileDevice mobileDevice = mobileDeviceMapper.map(device);129 this.removeDevice(mobileDevice);130 } catch (Exception e) {131 log.error(e.getMessage(), e);132 }133 }134 @Override135 public void deviceChanged(IDevice device, int i) {136 log.info("Device changed event received by Listener");137 try {138 MobileDevice mobileDevice = mobileDeviceMapper.map(device);139 if (IDevice.DeviceState.ONLINE.equals(device.getState())) {140 populateOtherAttributes(mobileDevice, device);141 }142 mobileDevice.setIDevice(device);143 this.updateDevice(mobileDevice);144 } catch (Exception e) {145 log.error(e.getMessage(), e);146 }147 }148 private void populateOtherAttributes(MobileDevice mobileDevice, IDevice device) throws AdbCommandExecutionException {149 mobileDevice.setScreenWidth(commandExecutor.getScreenWidth(device));150 mobileDevice.setScreenHeight(commandExecutor.getScreenHeight(device));151 populateBrowserList(mobileDevice, device);152 mobileDevice.setOsName(MobileOs.ANDROID);153 }154 private void populateBrowserList(MobileDevice mobileDevice, IDevice device) throws AdbCommandExecutionException {155 boolean isChromeInstalled = commandExecutor.isPackageInstalled(device, "com.android.chrome");156 if (isChromeInstalled) {157 List<AgentBrowser> browserList = new ArrayList<>();158 String version = commandExecutor.getChromeVersion(device);159 AgentBrowser browser = new AgentBrowser(OsBrowserType.Chrome, version, 64);160 browserList.add(browser);161 mobileDevice.setBrowserList(browserList);162 }163 }164 private void waitForAdbInitialization() {165 byte retries = 0;166 while (!adBridge.hasInitialDeviceList()) {167 log.debug("Waiting for initial list of device post ADB initialization.....");168 try {169 Thread.sleep(100L);170 retries++;171 } catch (InterruptedException interruptedException) {172 log.warn("Interrupted while waiting for initial device list from ADB");173 }174 if (retries > 40) {175 log.warn("Timed out while waiting for initial device list from ADB");176 }177 }178 }...
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!!