Skip to content

Instantly share code, notes, and snippets.

@Zodiase
Last active November 9, 2016 18:48
Show Gist options
  • Save Zodiase/dbdfa729c88f53bf11a89b81b9d2910f to your computer and use it in GitHub Desktop.
Save Zodiase/dbdfa729c88f53bf11a89b81b9d2910f to your computer and use it in GitHub Desktop.
Selenium-Webdriver Example
node_modules
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Selenium-Webdriver Test</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="someElement">Super Secret</div>
</body>
</html>
{
"name": "selenium-webdriver-example",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": ". ./test.sh"
},
"author": "Xingchen Hong",
"license": "Apache-2.0",
"devDependencies": {
"chai": "^3.5.0",
"chromedriver": "^2.25.1",
"express": "^4.14.0",
"mocha": "^3.1.2",
"selenium-webdriver": "^3.0.1"
}
}
var express = require("express");
var app = express();
var port = process.env.PORT || 4000;
app.use(express.static(__dirname));
app.listen(port); //the port you want to use
console.log("Express server running on port: "+port);
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until,
expect = require('chai').expect,
port = process.env.PORT || 4000;
describe('webpage', function () {
var driver;
before(function () {
driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
});
beforeEach(function (done) {
driver.get('http://localhost:' + port + '/').then(done);
});
after(function(done) {
driver.quit().then(done);
});
it('should have title', function (done) {
driver.getTitle().then(function (title) {
expect(title).to.equal('Selenium-Webdriver Test');
done();
});
});
it('should have the secret element', function (done) {
driver.findElement(By.id('someElement')).getText().then(function (text) {
expect(text).to.equal('Super Secret');
done();
});
});
});
#!/bin/bash
# Start the testing.
# Get script directory.
TEST="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT="$TEST/.."
PORT="4000"
# Start the node server in the background and record its PID.
env PORT="$PORT" node "$TEST/server.js" > /dev/null 2>&1 &
printf "\nWaiting for servers to start..."
while true; do
if ! curl --output /dev/null --silent --head --fail "http://localhost:${PORT}"; then
sleep 1;
printf "."
else
printf "Done\n"
break
fi
done
printf "\nRunning tests with WebDriver...\n"
#"$ROOT/node_modules/.bin/wdio" "$TEST/wdio.conf.js"
env PORT="$PORT" mocha "$TEST/test.js"
RESULT=$?
printf "\nKilling background processes..."
kill $(jobs -rp) && wait $(jobs -rp) > /dev/null 2>&1
printf "Done\n"
exit $RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment