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

Commit

Permalink
Use multiple selector strategies if first one doesn't work
Browse files Browse the repository at this point in the history
* Don't refresh page if we couldn't find element, instead just try the next locator strategy
* If nothing is found, show a notification recommending refreshing instead of just refreshing
* The previous refreshing feature was a mistake, because if the element can never be found it will ALWAYS refresh
  • Loading branch information
dpgraham committed Oct 24, 2017
1 parent 0d49422 commit a141faf
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions app/renderer/actions/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,27 @@ export function bindAppium () {


// A debounced function that calls findElement and gets info about the element
const findElement = _.debounce(async function (strategy, selector, dispatch, getState, path) {
// Get the information about the element
let {elementId, variableName, variableType} = await callClientMethod({
strategy,
selector,
});

// If the element is stale, unselect and reload the source
if (!elementId) {
dispatch({type: UNSELECT_ELEMENT});
return await applyClientMethod({methodName: 'source'})(dispatch);
}
const findElement = _.debounce(async function (strategyMap, dispatch, getState, path) {
for (let [strategy, selector] of strategyMap) {
// Get the information about the element
let {elementId, variableName, variableType} = await callClientMethod({
strategy,
selector,
});

// Set the elementId, variableName and variableType for the selected element
// (check first that the selectedElementPath didn't change, to avoid race conditions)
if (getState().inspector.selectedElementPath === path) {
dispatch({type: SET_SELECTED_ELEMENT_ID, elementId, variableName, variableType});
// Set the elementId, variableName and variableType for the selected element
// (check first that the selectedElementPath didn't change, to avoid race conditions)
if (elementId && getState().inspector.selectedElementPath === path) {
return dispatch({type: SET_SELECTED_ELEMENT_ID, elementId, variableName, variableType});
}
}

// If we couldn't find the element, show a notification
notification.error({
message: 'Error',
description: 'Could not find element. Try refreshing page',
duration: 0,
});
}, 1000);

export function selectElement (path) {
Expand All @@ -150,10 +153,10 @@ export function selectElement (path) {

// Find the optimal selection strategy. If none found, fall back to XPath.
const strategyMap = _.toPairs(getLocators(selectedElementAttributes, sourceXML));
const [optimalStrategy, optimalSelector] = strategyMap.length > 0 ? strategyMap[strategyMap.length - 1] : ['xpath', selectedElementXPath];
strategyMap.push(['xpath', selectedElementXPath]);

// Debounce find element so that if another element is selected shortly after, cancel the previous search
await findElement(optimalStrategy, optimalSelector, dispatch, getState, path);
await findElement(strategyMap, dispatch, getState, path);
};
}

Expand Down

0 comments on commit a141faf

Please sign in to comment.