Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Highlight elements that user searched for #344

Merged
merged 6 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 32 additions & 38 deletions app/main/appium-method-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,40 @@ export default class AppiumMethodHandler {
return {variableName, variableType, strategy, selector, elements};
}

async executeElementCommand (elementId, methodName, args = [], skipScreenshot = false) {
const cachedEl = this.elementCache[elementId];
async _execute ({elementId, methodName, args, skipScreenshotAndSource}) {
let cachedEl;
let res = {};

// Give the cached element a variable name (el1, el2, el3,...) the first time it's used
if (!cachedEl.variableName && cachedEl.variableType === 'string') {
cachedEl.variableName = `el${this.elVariableCounter++}`;
if (elementId) {
// Give the cached element a variable name (el1, el2, el3,...) the first time it's used
cachedEl = this.elementCache[elementId];
if (!cachedEl.variableName && cachedEl.variableType === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use _.isString

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variableType is a hard-coded string that identifies what type the element is. It's used by the RecordedActions component which displays recorded code and it uses the variableType to determine if it should be el.text("String") or el.text(123) etc...

cachedEl.variableName = `el${this.elVariableCounter++}`;
}
res = await cachedEl.el[methodName].apply(cachedEl.el, args);
} else {
// Specially handle the tap and swipe method
if (methodName === 'tap') {
res = await (new wd.TouchAction(this.driver))
.tap({x: args[0], y: args[1]})
.perform();
} else if (methodName === 'swipe') {
const [startX, startY, endX, endY] = args;
res = await (new wd.TouchAction(this.driver))
.press({x: startX, y: startY})
.moveTo({x: endX, y: endY})
.release()
.perform();
} else if (methodName !== 'source' && methodName !== 'screenshot') {
res = await this.driver[methodName].apply(this.driver, args);
}
}
const res = await cachedEl.el[methodName].apply(cachedEl.el, args);

// Give the source/screenshot time to change before taking the screenshot
await Bluebird.delay(500);

let sourceAndScreenshot;
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume an empty array should be assigned to this variable by default since we apply ellipsis operator to it later

Copy link
Contributor Author

@dpgraham dpgraham Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you pass undefined or null into an object via ellipsis (spread) operator, it has no effect.

if (!skipScreenshot) {
if (!skipScreenshotAndSource) {
sourceAndScreenshot = await this._getSourceAndScreenshot();
}

Expand All @@ -82,38 +102,12 @@ export default class AppiumMethodHandler {
};
}

async executeMethod (methodName, args = [], skipScreenshot = false) {
let res = {};

// Specially handle the tap and swipe method
if (methodName === 'tap') {
res = await (new wd.TouchAction(this.driver))
.tap({x: args[0], y: args[1]})
.perform();
} else if (methodName === 'swipe') {
const [startX, startY, endX, endY] = args;
res = await (new wd.TouchAction(this.driver))
.press({x: startX, y: startY})
.moveTo({x: endX, y: endY})
.release()
.perform();
} else if (methodName !== 'source' && methodName !== 'screenshot') {
res = await this.driver[methodName].apply(this.driver, args);
}

// Give the source/screenshot time to change before taking the screenshot
await Bluebird.delay(500);

let sourceAndScreenshot;

if (!skipScreenshot) {
sourceAndScreenshot = await this._getSourceAndScreenshot();
}
async executeElementCommand (elementId, methodName, args = [], skipScreenshotAndSource = false) {
return await this._execute({elementId, methodName, args, skipScreenshotAndSource});
}

return {
...sourceAndScreenshot,
res,
};
async executeMethod (methodName, args = [], skipScreenshotAndSource = false) {
return await this._execute({methodName, args, skipScreenshotAndSource});
}

async _getSourceAndScreenshot () {
Expand Down
8 changes: 4 additions & 4 deletions app/main/appium.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function connectClientMethodListener () {
fetchArray = false, // Optional. Are we fetching an array of elements or just one?
elementId, // Optional. Element being operated on
args = [], // Optional. Arguments passed to method
skipScreenshot = false, // Optional. Do we want the updated source and screenshot?
skipScreenshotAndSource = false, // Optional. Do we want the updated source and screenshot?
} = data;

let renderer = evt.sender;
Expand All @@ -323,15 +323,15 @@ function connectClientMethodListener () {
if (methodName) {
if (elementId) {
console.log(`Handling client method request with method '${methodName}', args ${JSON.stringify(args)} and elementId ${elementId}`);
res = await methodHandler.executeElementCommand(elementId, methodName, args, skipScreenshot);
res = await methodHandler.executeElementCommand(elementId, methodName, args, skipScreenshotAndSource);
} else {
console.log(`Handling client method request with method '${methodName}' and args ${JSON.stringify(args)}`);
res = await methodHandler.executeMethod(methodName, args, skipScreenshot);
res = await methodHandler.executeMethod(methodName, args, skipScreenshotAndSource);
}
} else if (strategy && selector) {
if (fetchArray) {
console.log(`Fetching elements with selector '${selector}' and strategy ${strategy}`);
res = await methodHandler.fetchElements(strategy, selector, skipScreenshot);
res = await methodHandler.fetchElements(strategy, selector, skipScreenshotAndSource);
} else {
console.log(`Fetching an element with selector '${selector}' and strategy ${strategy}`);
res = await methodHandler.fetchElement(strategy, selector);
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/actions/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ export function setLocatorTestElement (elementId) {
if (elementId) {
try {
const [location, size] = await(B.all([
callClientMethod({methodName: 'getLocation', args: [elementId], skipScreenshot: true, skipRecord: true}),
callClientMethod({methodName: 'getSize', args: [elementId], skipScreenshot: true, skipRecord: true}),
callClientMethod({methodName: 'getLocation', args: [elementId], skipScreenshotAndSource: true, skipRecord: true}),
callClientMethod({methodName: 'getSize', args: [elementId], skipScreenshotAndSource: true, skipRecord: true}),
]));
dispatch({type: SET_SEARCHED_FOR_ELEMENT_BOUNDS, location: location.res, size: size.res});
} catch (ign) { }
Expand Down