Skip to content

Commit

Permalink
fix: Init adb as the first step upon session creation (appium#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Feb 7, 2024
1 parent 14334c5 commit fbe80e9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
21 changes: 10 additions & 11 deletions lib/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -659,16 +668,6 @@ class AndroidUiautomator2Driver
async startUiAutomator2Session(
caps: Uiautomator2StartSessionOpts
): Promise<Uiautomator2SessionCaps> {
// 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 = {
Expand Down
59 changes: 34 additions & 25 deletions test/unit/driver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand All @@ -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: [{}],
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: [{}],
Expand All @@ -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: [{}],
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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"]',
Expand All @@ -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"]',
Expand Down

0 comments on commit fbe80e9

Please sign in to comment.