Skip to content

Commit

Permalink
Add save to unavailable server test. (mozilla-services#4027)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenba committed Feb 25, 2018
1 parent 63bf01d commit e44cbc5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
32 changes: 32 additions & 0 deletions bin/test-helpers/make_addon_with_unavailable_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# This simple script creates an addon with an unavailable backend. At least one
# of the Selenium tests rely on it. It also (re)creates the addon that might've
# been replaced during the process.

set -e

SCRIPT_PATH=${0%/*}
cd "$SCRIPT_PATH/../../"

if [ -n "$SCREENSHOTS_BACKEND" ]
then
PREVIOUS_BACKEND=$SCREENSHOTS_BACKEND
fi

export SCREENSHOTS_BACKEND=${BAD_SCREENSHOTS_BACKEND:-'http://localhost:49152'}
echo "Setting backend to ${SCREENSHOTS_BACKEND}"

make bootstrap_zip >/dev/null
mv build/screenshots-bootstrap.zip build/screenshots-bootstrap-server-down.zip
echo "Created build/screenshots-bootstrap-server-down.zip"

if [ -n "$PREVIOUS_BACKEND" ]
then
export SCREENSHOTS_BACKEND=$PREVIOUS_BACKEND
else
unset SCREENSHOTS_BACKEND
fi

make bootstrap_zip

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
"postlint": "nsp check -o summary",
"nsp": "nsp check",
"posttest": "npm run lint",
"test": "make bootstrap_zip && mocha test/ test/server/unit/*",
"test:selenium": "make bootstrap_zip && mocha test/test.js",
"test": "bin/test-helpers/make_addon_with_unavailable_server && mocha test/ test/server/unit/*",
"test:selenium": "bin/test-helpers/make_addon_with_unavailable_server && mocha test/test.js",
"test:server": "make .venv && cd test/server && ../../.venv/bin/pytest -m 'not slow'",
"make_versions_exact": "node bin/build-scripts/make_versions_exact.js",
"update_outdated": "david update && npm run make_versions_exact && npm update && npm outdated",
Expand Down
62 changes: 35 additions & 27 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PRESELECTION_IFRAME_ID = "firefox-screenshots-preselection-iframe";
const SELECTION_IFRAME_ID = "firefox-screenshots-selection-iframe";
const PREVIEW_IFRAME_ID = "firefox-screenshots-preview-iframe";
const backend = "http://localhost:10080";
const addonFileLocation = path.join(process.cwd(), "build", "screenshots-bootstrap.zip");

function getDriver() {
const channel = process.env.FIREFOX_CHANNEL || "NIGHTLY";
Expand All @@ -50,16 +51,14 @@ function getDriver() {
.setFirefoxOptions(options)
.build();

const fileLocation = path.join(process.cwd(), "build", "screenshots-bootstrap.zip");
driver.installAddon(fileLocation);
driver.installAddon(addonFileLocation);

return driver;
}

function getChromeElement(driver, selector) {
driver.setContext(firefox.Context.CHROME);
return driver.wait(
webdriver.until.elementLocated(selector), 1000);
return driver.setContext(firefox.Context.CHROME)
.then(() => driver.wait(until.elementLocated(selector)));
}

/** Calls finallyCallback() after the promise completes, successfully or not,
Expand Down Expand Up @@ -122,6 +121,16 @@ function skipOnboarding(driver) {
});
}

function startAutoSelectionShot(driver) {
return driver.get(backend)
.then(() => startScreenshots(driver))
.then(() => driver.wait(until.ableToSwitchToFrame(By.id(PRESELECTION_IFRAME_ID))))
.then(() => driver.wait(until.elementLocated(By.css(".visible"))))
.then(() => driver.actions().move({x: 100, y: 100}).click().perform())
.then(() => driver.switchTo().defaultContent())
.then(() => driver.wait(until.ableToSwitchToFrame(By.id(SELECTION_IFRAME_ID))));
}

/** Waits when ready, calls creator to create the shot, waits for the resulting
promise to complete, then watches for the
new tab to open and load, and returns the shot URL. The new tab
Expand Down Expand Up @@ -244,28 +253,8 @@ describe("Test Screenshots", function() {
});

it("should take an auto selection shot", function(done) {
driver.get(backend).then(() => {
return startScreenshots(driver);
}).then(() => {
return driver.wait(
until.ableToSwitchToFrame(By.id(PRESELECTION_IFRAME_ID))
);
}).then(() => {
return driver.wait(
until.elementLocated(By.css(".visible"))
);
}).then(() => {
return driver.actions().move({x: 100, y: 100}).click().perform();
}).then(() => {
return driver.switchTo().defaultContent();
}).then(() => {
return driver.wait(
until.ableToSwitchToFrame(By.id(SELECTION_IFRAME_ID))
);
}).then(() => {
return driver.wait(
until.elementLocated(By.css(".highlight-button-save"))
);
startAutoSelectionShot(driver).then(() => {
return driver.wait(until.elementLocated(By.css(".highlight-button-save")));
}).then((saveButton) => {
return expectCreatedShot(driver, () => {
saveButton.click();
Expand Down Expand Up @@ -321,4 +310,23 @@ describe("Test Screenshots", function() {
}).catch(done);
});

it("should remain on original tab with UI overlay on save failure", function(done) {
const badAddon = path.join(process.cwd(), "build", "screenshots-bootstrap-server-down.zip");
driver.installAddon(badAddon)
.then(() => startAutoSelectionShot(driver))
.then(() => driver.wait(until.elementLocated(By.css(".highlight-button-save"))))
.then((saveButton) => saveButton.click())
.then(() => driver.getAllWindowHandles())
.then((tabs) => driver.switchTo().window(tabs[tabs.length - 1]))
.then(() => driver.switchTo().defaultContent())
.then(() => driver.findElement(By.id(PRESELECTION_IFRAME_ID)))
.then(selectionFrame => assert(selectionFrame.isDisplayed()))
.then(() => driver.getCurrentUrl())
.then((url) => {
assert.equal(url, `${backend}/`, `Navigated away from ${backend}`);

// Doing this in case there are tests after this one.
driver.installAddon(addonFileLocation).then(() => done()).catch(done);
}).catch(done);
});
});

0 comments on commit e44cbc5

Please # to comment.