Skip to content

Commit

Permalink
fix: Properly pass this context to getScreenSize helper (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Aug 16, 2023
1 parent 54c923a commit 341c2e4
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import _ from 'lodash';
import { util } from 'appium/support';

async function getScreenSize (log) {
/**
* @typedef {Object} Size
* @property {number} width
* @property {number} height
*/

/**
* @this {import('../driver').WindowsDriver}
* @returns {Promise<Size>}
*/
async function getScreenSize () {
const dimensions = await this.execPowerShell({
command: 'Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Size',
});
log.debug(`Screen size information retrieved: ${dimensions}`);
this.log.debug(`Screen size information retrieved: ${dimensions}`);
const match = /^\s*(True|False)\s+(\d+)\s+(\d+)/m.exec(dimensions);
if (!match) {
throw new Error('Cannot retrieve the screen size. Check the server log for more details');
Expand All @@ -20,6 +30,9 @@ const commands = {};

// The next two commands are required
// for proper `-image` locator functionality
/**
* @returns {Promise<Size>}
*/
commands.getWindowSize = async function getWindowSize () {
const size = await this.winAppDriver.sendCommand('/window/size', 'GET');
if (_.isPlainObject(size)) {
Expand All @@ -28,7 +41,7 @@ commands.getWindowSize = async function getWindowSize () {
// workaround for https://github.com/microsoft/WinAppDriver/issues/1104
this.log.info('Cannot retrieve window size from WinAppDriver');
this.log.info('Falling back to Windows Forms to calculate dimensions');
return await getScreenSize(this.log);
return await getScreenSize.bind(this)();
};

// a workaround for https://github.com/appium/appium/issues/15923
Expand Down

0 comments on commit 341c2e4

Please sign in to comment.