<commit_before>/* globals describe, it, before, after */

const assert = require("assert");
const firefox = require("selenium-webdriver/firefox");
const webdriver = require("selenium-webdriver");
//const FxRunnerUtils = require("fx-runner/lib/utils");
//const Fs = require("fs-promise");
const path = require("path");

const SHOOTER_BUTTON_ID = "action-button--jid1-neeaf3sahdkhpajetpack-pageshot-shooter";

function addAddonToDriver(driver, location) {
  return driver.executeAsyncScript(`
const FileUtils = Components.utils.import('resource://gre/modules/FileUtils.jsm').FileUtils;
const { console } = Components.utils.import("resource://gre/modules/Console.jsm", {});
const { AddonManager } = Components.utils.import("resource://gre/modules/AddonManager.jsm", {});
const callback = arguments[arguments.length - 1];

class AddonListener {
  onInstallEnded(install, addon) {
    callback([addon.id, 0]);
  }

  onInstallFailed(install) {
    callback([null, install.error]);
  }

  onInstalled(addon) {
    AddonManager.removeAddonListener(this);
    callback([addon.id, 0]);
  }
}

AddonManager.addAddonListener(new AddonListener());
AddonManager.installTemporaryAddon(new FileUtils.File(arguments[0]))
  .catch((error) => {
    callback([null, error]);
  });

`, location).then(([result, err]) => {
    if (!result[0] && result[1]) {
      throw new Error(`Failed to install add-on: ${result[1]}`);
    }
    return driver;
  });
}

function promiseDriver() {
  const profile = new firefox.Profile();
  const options = new firefox.Options();
  options.setProfile(profile);

  const builder = new webdriver.Builder()
    .forBrowser("firefox")
    .setFirefoxOptions(options);

  const driver = builder.build();

  driver.setContext(firefox.Context.CHROME);
  let fileLocation = path.join(process.cwd(), "build", "mozilla-pageshot.xpi");
  addAddonToDriver(driver, fileLocation);
}

function getElementById(driver, id) {
  driver.setContext(firefox.Context.CHROME);
  return driver.wait(
    webdriver.until.elementLocated(
      webdriver.By.id(id)), 1000);
}

describe("Test Page Shot", function () {
  this.timeout(10000);
  let driver;

  before(function() {
    return promiseDriver().then((theDriver) => {
      driver = theDriver;
    });
  });

  after(function() {
    return driver.quit();
  });

  it("should find the add-on button", function() {
    return getElementById(driver, SHOOTER_BUTTON_ID)
      .then((button) => button.getAttribute("label"))
      .then((label) => assert.equal(label, "Make shot"));
  });
});<commit_msg>Switch the rest of the code to be synchronous<commit_after>/* globals describe, it, before, after */

const assert = require("assert");
const firefox = require("selenium-webdriver/firefox");
const webdriver = require("selenium-webdriver");
//const FxRunnerUtils = require("fx-runner/lib/utils");
//const Fs = require("fs-promise");
const path = require("path");

const SHOOTER_BUTTON_ID = "action-button--jid1-neeaf3sahdkhpajetpack-pageshot-shooter";

function addAddonToDriver(driver, location) {
  return driver.executeAsyncScript(`
const FileUtils = Components.utils.import('resource://gre/modules/FileUtils.jsm').FileUtils;
const { console } = Components.utils.import("resource://gre/modules/Console.jsm", {});
const { AddonManager } = Components.utils.import("resource://gre/modules/AddonManager.jsm", {});
const callback = arguments[arguments.length - 1];

class AddonListener {
  onInstallEnded(install, addon) {
    callback([addon.id, 0]);
  }

  onInstallFailed(install) {
    callback([null, install.error]);
  }

  onInstalled(addon) {
    AddonManager.removeAddonListener(this);
    callback([addon.id, 0]);
  }
}

AddonManager.addAddonListener(new AddonListener());
AddonManager.installTemporaryAddon(new FileUtils.File(arguments[0]))
  .catch((error) => {
    callback([null, error]);
  });

`, location).then(([result, err]) => {
    if (!result[0] && result[1]) {
      throw new Error(`Failed to install add-on: ${result[1]}`);
    }
    return driver;
  });
}

function getDriver() {
  const profile = new firefox.Profile();
  const options = new firefox.Options();
  options.setProfile(profile);

  const builder = new webdriver.Builder()
    .forBrowser("firefox")
    .setFirefoxOptions(options);

  const driver = builder.build();

  driver.setContext(firefox.Context.CHROME);
  let fileLocation = path.join(process.cwd(), "build", "mozilla-pageshot.xpi");
  addAddonToDriver(driver, fileLocation);
  return driver;
}

function getElementById(driver, id) {
  driver.setContext(firefox.Context.CHROME);
  return driver.wait(
    webdriver.until.elementLocated(
      webdriver.By.id(id)), 1000);
}

describe("Test Page Shot", function () {
  this.timeout(10000);
  let driver;

  before(function() {
    driver = getDriver();
  });

  after(function() {
    return driver.quit();
  });

  it("should find the add-on button", function() {
    return getElementById(driver, SHOOTER_BUTTON_ID)
      .then((button) => button.getAttribute("label"))
      .then((label) => assert.equal(label, "Make shot"));
  });
});
{'fraction_non_alphanumeric': 0.1624015748031496, 'fraction_numerical': 0.007086614173228346, 'mean_word_length': 4.601984564498347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6132517', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1607, 'n_words': 300}