|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { AndroidUiautomator2Driver } from 'appium-uiautomator2-driver'; |
| 4 | +import { ADB } from 'appium-adb'; |
| 5 | +import log from '../src/logger.js'; |
| 6 | +import { waitForCondition } from 'asyncbox'; |
| 7 | + |
| 8 | +const START_APP_WAIT_DURATION = 60000; |
| 9 | + |
| 10 | +const edge = { |
| 11 | + pkg: 'com.microsoft.emmx', |
| 12 | + activity: 'com.microsoft.ruby.Main', |
| 13 | +}; |
| 14 | + |
| 15 | +const common = { |
| 16 | + waitDuration: START_APP_WAIT_DURATION, |
| 17 | + optionalIntentArguments: '-d www.appium.io', |
| 18 | +}; |
| 19 | + |
| 20 | +async function skipWelcomeEdge() { |
| 21 | + const adb = await ADB.createADB(); |
| 22 | + await adb.adbExec(['shell', 'pm', 'clear', edge.pkg]); |
| 23 | + const driver = new AndroidUiautomator2Driver(); |
| 24 | + const caps = { |
| 25 | + platformName: "Android", |
| 26 | + "appium:automationName": "UiAutomator2", |
| 27 | + "appium:deviceName": "Android Device", |
| 28 | + "appium:appPackage": edge.pkg, |
| 29 | + "appium:appActivity": edge.activity, |
| 30 | + }; |
| 31 | + |
| 32 | + const findElementWithWaitForCondition = async ( |
| 33 | + strategy, |
| 34 | + selector, |
| 35 | + timeout = 6000 |
| 36 | + ) => { |
| 37 | + try { |
| 38 | + return await waitForCondition( |
| 39 | + async () => { |
| 40 | + try { |
| 41 | + log.info( |
| 42 | + `Attempting to find element (${strategy}: ${selector})` |
| 43 | + ); |
| 44 | + const element = await driver.findElement(strategy, selector); |
| 45 | + log.info( |
| 46 | + `Successfully found element (${strategy}: ${selector})` |
| 47 | + ); |
| 48 | + return element; |
| 49 | + } catch (error) { |
| 50 | + // Return false to continue waiting |
| 51 | + return false; |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + waitMs: timeout, |
| 56 | + intervalMs: 1000, |
| 57 | + } |
| 58 | + ); |
| 59 | + } catch (error) { |
| 60 | + throw new Error( |
| 61 | + `Failed to find element (${strategy}: ${selector}) within ${timeout}ms timeout: ${error.message}` |
| 62 | + ); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + |
| 67 | + try { |
| 68 | + await driver.createSession(null, { |
| 69 | + alwaysMatch: caps, |
| 70 | + firstMatch: [{}], |
| 71 | + }); |
| 72 | + const activity = await driver.getCurrentActivity(); |
| 73 | + log.info(`Activity is ${activity}`); |
| 74 | + if (activity.includes('MicrosoftFirstRunActivity')) { |
| 75 | + // wait for the page to load |
| 76 | + await new Promise(resolve => setTimeout(resolve, 5000)); |
| 77 | + |
| 78 | + const MAX_RETRIES = 3; |
| 79 | + let attempt = 0; |
| 80 | + let clicked = false; |
| 81 | + let found = false; |
| 82 | + var notNowButton; |
| 83 | + |
| 84 | + while (attempt < MAX_RETRIES && !clicked) { |
| 85 | + try{ |
| 86 | + notNowButton = await findElementWithWaitForCondition( |
| 87 | + 'xpath', |
| 88 | + '//android.widget.Button[@text="Not now"]' |
| 89 | + ); |
| 90 | + log.info(`Not Now button is ${JSON.stringify(notNowButton, null, 2)}`); |
| 91 | + found = true; |
| 92 | + } catch(error) { |
| 93 | + log.info(`Not Now button not found, retrying ...`); |
| 94 | + } finally { |
| 95 | + if (found) { |
| 96 | + await driver.click(notNowButton.ELEMENT); |
| 97 | + clicked = true; |
| 98 | + } |
| 99 | + attempt++; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + attempt = 0; |
| 104 | + clicked = false; |
| 105 | + found = false; |
| 106 | + var checkBox; |
| 107 | + |
| 108 | + while (attempt < MAX_RETRIES && !clicked) { |
| 109 | + try{ |
| 110 | + checkBox = await findElementWithWaitForCondition( |
| 111 | + 'xpath', |
| 112 | + '//android.widget.Image' |
| 113 | + ); |
| 114 | + log.info(`Check box is ${JSON.stringify(checkBox, null, 2)}`); |
| 115 | + found = true; |
| 116 | + } catch(error) { |
| 117 | + log.info(`Check box not found, retrying the other xpath...`); |
| 118 | + try { |
| 119 | + checkBox = await findElementWithWaitForCondition( |
| 120 | + 'xpath', |
| 121 | + '//android.widget.CheckBox[@text="Help improve Microsoft products by sending optional diagnostic data about how you use the browser, websites you visit, and crash reports."]' |
| 122 | + ); |
| 123 | + log.info(`Check box is ${JSON.stringify(checkBox, null, 2)}`); |
| 124 | + found = true; |
| 125 | + } catch(error) { |
| 126 | + log.info(`Check box not found, retrying...`); |
| 127 | + } |
| 128 | + } finally { |
| 129 | + if (found) { |
| 130 | + await driver.click(checkBox.ELEMENT); |
| 131 | + clicked = true; |
| 132 | + } |
| 133 | + attempt++; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + attempt = 0; |
| 138 | + clicked = false; |
| 139 | + found = false; |
| 140 | + var confirmButton; |
| 141 | + |
| 142 | + while (attempt < MAX_RETRIES && !clicked) { |
| 143 | + try{ |
| 144 | + confirmButton = await findElementWithWaitForCondition( |
| 145 | + 'xpath', |
| 146 | + '//android.widget.Button[@text="Confirm"]' |
| 147 | + ); |
| 148 | + log.info(`Confirm button is ${JSON.stringify(confirmButton, null, 2)}`); |
| 149 | + found = true; |
| 150 | + } catch(error) { |
| 151 | + log.info(`Confirm button not found, retrying...`); |
| 152 | + } finally { |
| 153 | + if (found) { |
| 154 | + await driver.click(confirmButton.ELEMENT); |
| 155 | + clicked = true; |
| 156 | + } |
| 157 | + attempt++; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } finally { |
| 162 | + await driver.deleteSession(); |
| 163 | + } |
| 164 | + |
| 165 | + try { |
| 166 | + caps["appium:noReset"] = true; |
| 167 | + await driver.createSession(null, { |
| 168 | + alwaysMatch: caps, |
| 169 | + firstMatch: [{}], |
| 170 | + }); |
| 171 | + const activity = await driver.getCurrentActivity(); |
| 172 | + log.info(`Activity is ${activity}`); |
| 173 | + if (activity.includes('GrantPermissionsActivity')) { |
| 174 | + // wait for the page to load |
| 175 | + await new Promise(resolve => setTimeout(resolve, 5000)); |
| 176 | + |
| 177 | + const MAX_RETRIES = 3; |
| 178 | + let attempt = 0; |
| 179 | + let clicked = false; |
| 180 | + let found = false; |
| 181 | + var denyButton; |
| 182 | + |
| 183 | + while (attempt < MAX_RETRIES && !clicked) { |
| 184 | + try{ |
| 185 | + denyButton = await findElementWithWaitForCondition( |
| 186 | + 'id', |
| 187 | + 'com.android.permissioncontroller:id/permission_allow_button' |
| 188 | + ); |
| 189 | + log.info(`Deny button is ${JSON.stringify(denyButton, null, 2)}`); |
| 190 | + found = true; |
| 191 | + } catch(error) { |
| 192 | + log.info(`Deny button not found, retrying ...`); |
| 193 | + } finally { |
| 194 | + if (found) { |
| 195 | + await driver.click(denyButton.ELEMENT); |
| 196 | + clicked = true; |
| 197 | + } |
| 198 | + attempt++; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + } finally { |
| 203 | + await driver.deleteSession(); |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +} |
| 209 | + |
| 210 | +(async () => { |
| 211 | + await skipWelcomeEdge(); |
| 212 | + process.exit(0); |
| 213 | +})(); |
0 commit comments