diff --git a/lib/driver.ts b/lib/driver.ts index 3b577d88b..79f58af59 100644 --- a/lib/driver.ts +++ b/lib/driver.ts @@ -372,6 +372,16 @@ class AndroidUiautomator2Driver }; _.defaults(this.opts, defaultOpts); + this.opts.adbPort = this.opts.adbPort || DEFAULT_ADB_PORT; + // get device udid for this session + const {udid, emPort} = await this.getDeviceInfoFromCaps(); + this.opts.udid = udid; + // @ts-expect-error do not put random stuff on opts + this.opts.emPort = emPort; + // now that we know our java version and device info, we can create our + // ADB instance + this.adb = await this.createADB(); + if (this.isChromeSession) { this.log.info(`We're going to run a Chrome-based session`); const {pkg, activity: defaultActivity} = utils.getChromePkg(this.opts.browserName!); @@ -405,7 +415,6 @@ class AndroidUiautomator2Driver 'without the target application' ); } - this.opts.adbPort = this.opts.adbPort || DEFAULT_ADB_PORT; const result = await this.startUiAutomator2Session(startSessionOpts); @@ -659,16 +668,6 @@ class AndroidUiautomator2Driver async startUiAutomator2Session( caps: Uiautomator2StartSessionOpts ): Promise { - // get device udid for this session - const {udid, emPort} = await this.getDeviceInfoFromCaps(); - this.opts.udid = udid; - // @ts-expect-error do not put random stuff on opts - this.opts.emPort = emPort; - - // now that we know our java version and device info, we can create our - // ADB instance - this.adb = await this.createADB(); - const appInfo = await this.performSessionPreExecSetup(); // set actual device name, udid, platform version, screen size, screen density, model and manufacturer details const sessionInfo: Uiautomator2SessionInfo = { diff --git a/test/unit/driver-specs.js b/test/unit/driver-specs.js index 6cc7c5ca4..0bd6dc2cd 100644 --- a/test/unit/driver-specs.js +++ b/test/unit/driver-specs.js @@ -8,13 +8,24 @@ import {ADB} from 'appium-adb'; chai.should(); chai.use(chaiAsPromised); -let sandbox = sinon.createSandbox(); +const sandbox = sinon.createSandbox(); function defaultStub(driver) { - sinon.stub(driver, 'getDeviceDetails'); + sandbox.stub(driver, 'getDeviceDetails'); + const adb = new ADB(); + sandbox.stub(driver, 'createADB').returns(Promise.resolve(adb)); + sandbox.mock(driver).expects('getDeviceInfoFromCaps').once().returns(Promise.resolve({ + udid: '123', + emPort: false + })); + return adb; } describe('driver.js', function () { + this.afterEach(function() { + sandbox.restore(); + }); + describe('constructor', function () { it('calls BaseDriver constructor with opts', function () { let driver = new AndroidUiautomator2Driver({foo: 'bar'}); @@ -26,7 +37,8 @@ describe('driver.js', function () { describe('createSession', function () { it('should throw an error if app can not be found', async function () { let driver = new AndroidUiautomator2Driver({}, false); - defaultStub(driver); + const adb = defaultStub(driver); + sandbox.stub(adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); await driver .createSession(null, null, { firstMatch: [{}], @@ -39,11 +51,10 @@ describe('driver.js', function () { it('should set sessionId', async function () { let driver = new AndroidUiautomator2Driver({}, false); - defaultStub(driver); - driver.adb = new ADB(); - sinon.mock(driver).expects('checkAppPresent').once().returns(B.resolve()); - sinon.stub(driver.adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); - sinon.mock(driver).expects('startUiAutomator2Session').once().returns(B.resolve()); + const adb = defaultStub(driver); + sandbox.mock(driver).expects('checkAppPresent').once().returns(B.resolve()); + sandbox.stub(adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); + sandbox.mock(driver).expects('startUiAutomator2Session').once().returns(B.resolve()); await driver.createSession(null, null, { firstMatch: [{}], alwaysMatch: { @@ -57,11 +68,10 @@ describe('driver.js', function () { it('should set the default context', async function () { let driver = new AndroidUiautomator2Driver({}, false); - defaultStub(driver); - driver.adb = new ADB(); - sinon.stub(driver.adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); - sinon.mock(driver).expects('checkAppPresent').returns(B.resolve()); - sinon.mock(driver).expects('startUiAutomator2Session').returns(B.resolve()); + const adb = defaultStub(driver); + sandbox.stub(adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); + sandbox.mock(driver).expects('checkAppPresent').returns(B.resolve()); + sandbox.mock(driver).expects('startUiAutomator2Session').returns(B.resolve()); await driver.createSession(null, null, { firstMatch: [{}], alwaysMatch: { @@ -77,8 +87,8 @@ describe('driver.js', function () { let driver = new AndroidUiautomator2Driver({}, false); defaultStub(driver); let app = path.resolve('.'); - sinon.mock(driver).expects('startUiAutomator2Session').returns(B.resolve()); - sinon.mock(driver.helpers).expects('configureApp').returns(app); + sandbox.mock(driver).expects('startUiAutomator2Session').returns(B.resolve()); + sandbox.mock(driver.helpers).expects('configureApp').returns(app); await driver.createSession(null, null, { firstMatch: [{}], @@ -96,9 +106,9 @@ describe('driver.js', function () { let driver = new AndroidUiautomator2Driver({}, false); defaultStub(driver); let app = path.resolve('asdfasdf'); - sinon.mock(driver).expects('checkAppPresent').returns(B.resolve()); - sinon.mock(driver).expects('startUiAutomator2Session').returns(B.resolve()); - sinon.mock(driver.helpers).expects('configureApp').returns(app); + sandbox.mock(driver).expects('checkAppPresent').returns(B.resolve()); + sandbox.mock(driver).expects('startUiAutomator2Session').returns(B.resolve()); + sandbox.mock(driver.helpers).expects('configureApp').returns(app); await driver.createSession(null, null, { firstMatch: [{}], @@ -151,11 +161,10 @@ describe('driver.js', function () { item[0] === 'GET' && item[1].test('/session/xxx/screenshot/'); beforeEach(function () { driver = new AndroidUiautomator2Driver({}, false); - defaultStub(driver); - driver.adb = new ADB(); - sinon.mock(driver).expects('checkAppPresent').once().returns(B.resolve()); - sinon.stub(driver.adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); - sinon.mock(driver).expects('startUiAutomator2Session').once().returns(B.resolve()); + const adb = defaultStub(driver); + sandbox.mock(driver).expects('checkAppPresent').once().returns(B.resolve()); + sandbox.stub(adb, 'getApiLevel').onFirstCall().returns(B.resolve(24)); + sandbox.mock(driver).expects('startUiAutomator2Session').once().returns(B.resolve()); }); describe('on webview mode', function () { @@ -244,7 +253,7 @@ describe('driver.js', function () { it('should trap and proxy to special uia2 server endpoint', async function () { defaultStub(driver); driver.uiautomator2 = {jwproxy: {command: () => {}}}; - let proxySpy = sinon.stub(driver.uiautomator2.jwproxy, 'command'); + let proxySpy = sandbox.stub(driver.uiautomator2.jwproxy, 'command'); await driver.doFindElementOrEls({ strategy: 'xpath', selector: '/*[@firstVisible="true"]', @@ -259,7 +268,7 @@ describe('driver.js', function () { it('should trap and rewrite as uiautomator locator', async function () { defaultStub(driver); driver.uiautomator2 = {jwproxy: {command: () => {}}}; - let proxySpy = sinon.stub(driver.uiautomator2.jwproxy, 'command'); + let proxySpy = sandbox.stub(driver.uiautomator2.jwproxy, 'command'); await driver.doFindElementOrEls({ strategy: 'xpath', selector: '//*[@scrollable="true"]',