tauri-plugin-automation 0.1.4

Tauri plugin for automation via WebDriver
function resolve(id, result) {
  __TAURI_INTERNALS__.invoke("plugin:automation|resolve", {
    id,
    result:
      result instanceof Error
        ? {
            error: result.name,
            message: result.message,
            stacktrace: result.stack,
          }
        : result,
  });
}

function staleElementError() {
  const err = new Error(
    "The element referenced by this selector is no longer attached to the DOM.",
  );
  err.name = "stale element reference";
  return err;
}

/**
 * Look up a live element by selector + position.
 * Throws a "stale element reference" error if the element is absent.
 */
function getElement(selector, position) {
  const element = document.querySelectorAll(selector)[position];
  if (element === undefined) {
    throw staleElementError();
  }
  return element;
}

/**
 * Look up an element from the cache, falling back to a live DOM query.
 * Throws a "stale element reference" error if the element is absent or
 * has been detached from the document since it was cached.
 */
function getCachedElement(selector, position) {
  if (
    __AUTOMATION__.cache[selector] &&
    __AUTOMATION__.cache[selector][position] !== undefined
  ) {
    const cached = __AUTOMATION__.cache[selector][position];
    if (!cached.isConnected) {
      throw staleElementError();
    }
    return cached;
  }
  return getElement(selector, position);
}

Object.defineProperty(window, "__AUTOMATION__", {
  value: {},
});
Object.defineProperty(window.__AUTOMATION__, "resolve", {
  value: resolve,
  writable: false,
});
Object.defineProperty(window.__AUTOMATION__, "cache", {
  value: {},
  writable: false,
});
Object.defineProperty(window.__AUTOMATION__, "getElement", {
  value: getElement,
  writable: false,
});
Object.defineProperty(window.__AUTOMATION__, "getCachedElement", {
  value: getCachedElement,
  writable: false,
});

// Cookie store for WebDriver automation (document.cookie may not work with custom protocols)
Object.defineProperty(window.__AUTOMATION__, "cookies", {
  value: [],
  writable: true,
});

// Alert/confirm/prompt overrides for WebDriver automation
Object.defineProperty(window.__AUTOMATION__, "alert", {
  value: { text: null, type: null, defaultResponse: true, promptText: "" },
  writable: true,
});

const _origAlert = window.alert;
const _origConfirm = window.confirm;
const _origPrompt = window.prompt;

window.alert = function (message) {
  window.__AUTOMATION__.alert = {
    text: String(message),
    type: "alert",
    defaultResponse: true,
    promptText: "",
  };
};

window.confirm = function (message) {
  window.__AUTOMATION__.alert = {
    text: String(message),
    type: "confirm",
    defaultResponse: true,
    promptText: "",
  };
  return window.__AUTOMATION__.alert.defaultResponse;
};

window.prompt = function (message, defaultValue) {
  window.__AUTOMATION__.alert = {
    text: String(message),
    type: "prompt",
    defaultResponse: true,
    promptText: defaultValue !== undefined ? String(defaultValue) : "",
  };
  if (!window.__AUTOMATION__.alert.defaultResponse) {
    return null;
  }
  return window.__AUTOMATION__.alert.promptText;
};