Skip to content

Commit

Permalink
Fixes #1631 - Warns user and aborts tests if server isn't right or if…
Browse files Browse the repository at this point in the history
… it's not in test mode.
  • Loading branch information
brizental committed Sep 6, 2017
1 parent 03b0da4 commit 1c8279d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"browser": true
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"module": "git submodule init && git submodule update",
"prestart": "npm run build",
"start": "source env/bin/activate || . env/bin/activate && python run.py",
"start:test": "source env/bin/activate || . env/bin/activate && python run.py -t",
"start:test": "npm run build && source env/bin/activate || . env/bin/activate && python run.py -t",
"virtualenv": "pip install virtualenv && virtualenv env && source env/bin/activate || . env/bin/activate && npm run pip",
"pip": "pip install -r config/requirements.txt",
"config": "cp config/secrets.py.example config/secrets.py",
Expand Down
135 changes: 54 additions & 81 deletions tests/functional/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
/*eslint no-console: ["error", { allow: ["log", "error"] }] */

define(
[
"intern",
"intern!object",
"require",
"intern/dojo/node!leadfoot/helpers/pollUntil"
],
function(intern, registerSuite, require, pollUntil) {
["intern", "intern!object", "require", "intern/dojo/node!http"],
function(intern, registerSuite, require, http) {
"use strict";
var config = intern.config;

Expand Down Expand Up @@ -49,90 +44,68 @@ define(
);
}

// TODO: when https://github.com/mozilla/geckodriver/issues/308 is fixed,
// remove this ugliness.
/*
This method makes a call to our API to check that the server is returning fixture data,
it will also check if there's anything wrong with the server.
*/
function checkServer() {
return new Promise(function(resolve, reject) {
var request = http.get(url("/api/issues/100"), function(response) {
response.on("data", function(data) {
var json = JSON.parse(data);
if (!json.hasOwnProperty("_fixture")) {
reject(
new Error(
`
=======================================================
It seems like you didn't start the server in test mode.
Open another terminal and window type:
\x1b[32m npm run start:test\x1b[0m
or
\x1b[32m python run.py -t\x1b[0m
=======================================================
`
)
);
} else {
resolve("All is well!");
}
});
});

// Handle connection errors.
request.on("error", function() {
reject(
new Error(
`
======================================================
Oops, something went wrong. Try restarting the server.
Open another terminal and window type:
\x1b[32m npm run start:test\x1b[0m
or
\x1b[32m python run.py -t\x1b[0m
======================================================
`
)
);
});
request.end();
});
}

function login(context) {
return openPage(context, url("/login"), "body")
.setFindTimeout(config.wc.pageLoadTimeout)
.getCurrentUrl()
.then(function(url) {
// is this the "normal" login flow?
if (url.includes("return_to")) {
return (
context.remote
.findByCssSelector("#login_field")
.click()
.type(config.wc.user)
.end()
.findByCssSelector("#password")
.click()
.type(config.wc.pw)
.end()
.findByCssSelector("input[type=submit]")
.click()
.end()
// *Sometimes* GitHub can bring up an extra verification
// page if it detects that our test user is requesting
// access too much.
.findByCssSelector(".oauth-review-permissions")
.then(
function() {
// In this case, there's an extra button to click to convince
// GitHub we're totally not a bot. >_>
return context.remote
.sleep(3000)
.findByCssSelector("button.btn-primary")
.click()
.end();
},
function(err) {
// Otherwise, we swallow the NoSuchElement error.
return new Promise(function(resolve) {
if (/NoSuchElement/.test(String(err))) {
resolve(true);
} else {
throw err;
}
});
}
)
.end()
// ...Now make sure the logged-in avatar is shown so we know we're
// back at the home page before we end.
.findByCssSelector(".wc-Navbar-avatar")
.end()
);
}
})
.end();
return openPage(context, url("/login"), "body").end();
}

function logout(context) {
return openPage(context, url("/logout"), "body").clearCookies().end();
}

function visibleByClass(selector) {
var elem;
return pollUntil(
function(selector) {
elem = document.getElementsByClassName(selector);
if (!elem || elem.length === 0) {
return null;
}
elem = elem[0];
return elem.offsetWidth > 0 && elem.offsetHeight > 0 ? true : null;
},
[selector],
10000
);
}

return {
login: login,
logout: logout,
openPage: openPage,
visibleByClass: visibleByClass
checkServer: checkServer,
login: login,
logout: logout
};
}
);
27 changes: 18 additions & 9 deletions tests/intern.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
// Learn more about configuring this file at <https://github.com/theintern/intern/wiki/Configuring-Intern>.
// These default settings work OK for most people. The options that *must* be changed below are the
// packages, suites, excludeInstrumentation, and (if you want functional tests) functionalSuites.
define(["intern"], function(intern, topic) {

define(["intern"], function(intern) {
"use strict";
var args = intern.args;
var siteRoot = args.siteRoot ? args.siteRoot : "http://localhost:5000";

if (topic) {
topic.subscribe("/suite/start", function(suite) {
/* eslint-disable no-console*/
console.log("Running: " + suite.name);
});
}

return {
// Configuration object for webcompat
wc: {
Expand All @@ -25,6 +19,21 @@ define(["intern"], function(intern, topic) {
: 10000
},

setup: function() {
define("FunctionalHelpers", ["tests/functional/lib/helpers"], function(
FunctionalHelpers
) {
return {
checkServer: FunctionalHelpers.checkServer
};
});
var serverPromise;
require(["FunctionalHelpers"], function(FunctionalHelpers) {
serverPromise = FunctionalHelpers.checkServer();
});
return serverPromise;
},

// The port on which the instrumenting proxy will listen
proxyPort: 9090,

Expand All @@ -33,7 +42,7 @@ define(["intern"], function(intern, topic) {
siteRoot: siteRoot,
tunnel: "SeleniumTunnel",
tunnelOptions: {
// this tells SeleniumTunnel to download geckodriver
// this tells SeleniumTunnel to download geckodriver and chromedriver
drivers: ["firefox", "chrome"]
},

Expand Down

0 comments on commit 1c8279d

Please sign in to comment.