Skip to content

Commit

Permalink
[js] //javascript/node/selenium-webdriver now builds + tests with bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyba committed May 8, 2019
1 parent b074e75 commit 44a6545
Show file tree
Hide file tree
Showing 33 changed files with 3,728 additions and 191 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
buck-out
java/client/build
java/server/build
node_modules
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ cpp/iedriver/IEReturnTypes.h
java/client/src/org/openqa/selenium/ie/IeReturnTypes.java
java/server/test/org/openqa/selenium/example
javascript/deps.js
javascript/node/selenium-webdriver/lib/atoms/
javascript/node/selenium-webdriver/node_modules/
javascript/safari-driver/node_modules/
.idea/vcs.xml
Expand Down Expand Up @@ -48,6 +47,7 @@ maven/*.iml
maven/*/*.iml
maven-metadata-.xml
maven-metadata-.xml.sha1
node_modules/
resolver-status.properties
java/client/build/
java/client/log.txt
Expand Down
8 changes: 8 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
filegroup(
name = "license",
srcs = [
"LICENSE",
"NOTICE",
],
visibility = ["//visibility:public"],
)
16 changes: 16 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ dotnet_nuget_new(
version = "3.11.0",
build_file = "//third_party/dotnet/nuget/packages:nunit.bzl"
)

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "4c702ffeeab2d24dd4101601b6d27cf582d2e0d4cdc3abefddd4834664669b6b",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.28.0/rules_nodejs-0.28.0.tar.gz"],
)

load("@build_bazel_rules_nodejs//:defs.bzl", "npm_install")
npm_install(
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
install_bazel_dependencies()
1 change: 1 addition & 0 deletions common/src/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ filegroup(
]),
visibility = [
"//java/client/test/org/openqa/selenium/environment:__pkg__",
"//javascript/node/selenium-webdriver:__pkg__",
"//dotnet/test/common:__pkg__"
],
)
1 change: 1 addition & 0 deletions javascript/atoms/fragments/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ closure_fragment(
visibility = [
"//dotnet/src/webdriver:__pkg__",
"//java/client/src/org/openqa/selenium/remote:__pkg__",
"//javascript/node/selenium-webdriver/lib/atoms:__pkg__",
"//py:__pkg__",
],
deps = [
Expand Down
88 changes: 88 additions & 0 deletions javascript/node/selenium-webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary", "npm_package")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")

SRC_FILES = [
"CHANGES.md",
"README.md",
"package.json",
] + glob([
"*.js",
"example/*.js",
"http/*.js",
"io/*.js",
"lib/*.js",
"net/*.js",
"remote/*.js",
"testing/*.js",
])

npm_package(
name = "selenium-webdriver",
srcs = SRC_FILES,
deps = [
":license",
"//javascript/node/selenium-webdriver/lib/atoms:get_attribute",
"//javascript/node/selenium-webdriver/lib/atoms:is_displayed",
],
)

TEST_FILES = glob(["test/**/*_test.js"])

TEST_DATA = SRC_FILES + glob(
[
"lib/test/**",
"test/**",
],
exclude = TEST_FILES,
)

nodejs_binary(
name = "fileserver",
data = TEST_DATA + [
"//common/src/web",
"@npm//express",
"@npm//multer",
"@npm//serve-index",
"@npm//tmp",
"@npm//jszip",
"@npm//rimraf",
"//javascript/node/selenium-webdriver/lib/atoms:get_attribute",
"//javascript/node/selenium-webdriver/lib/atoms:is_displayed",
],
entry_point = "selenium/javascript/node/selenium-webdriver/lib/test/fileserver.js",
)

jasmine_node_test(
name = "tests",
local = True,
srcs = TEST_FILES,
bootstrap = ["selenium/javascript/node/selenium-webdriver/tools/init_jasmine.js"],
data = TEST_DATA + [
"tools/init_jasmine.js",
"//common/src/web",
"@npm//express",
"@npm//multer",
"@npm//serve-index",
"@npm//jszip",
"@npm//rimraf",
"@npm//tmp",
"@npm//sinon",
"@npm//xml2js",
],
deps = [
"//javascript/node/selenium-webdriver/lib/atoms:get_attribute",
"//javascript/node/selenium-webdriver/lib/atoms:is_displayed",
"@npm//jasmine",
],
)

# npm_package does not pick up filegroup sources.
genrule(
name = "license",
srcs = ["//:license"],
outs = [
"LICENSE",
"NOTICE",
],
cmd = "cp $(locations //:license) $(@D)",
)
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ suite(function(env) {
//
// This example is always configured to skip Chrome.
ignore(env.browsers(Browser.CHROME)).it('demo 2', async function() {
await driver.get('http://www.google.com/ncr');
let url = await driver.getCurrentUrl();
assert.equal(url, 'https://www.google.com/');
await driver.get('https://www.google.com/ncr');
await driver.wait(until.urlIs('https://www.google.com/'), 1500);
});

after(() => driver && driver.quit());
Expand Down
29 changes: 29 additions & 0 deletions javascript/node/selenium-webdriver/lib/atoms/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")

nodejs_binary(
name = "make_atoms_module",
data = ["make-atoms-module.js"],
entry_point = "selenium/javascript/node/selenium-webdriver/lib/atoms/make-atoms-module",
)

# genrules used to copy closure_js_binary outputs to locations
# that will be picked up by the npm_package rule.

genrule(
name = "is_displayed",
srcs = ["//javascript/atoms/fragments:is-displayed.js"],
outs = ["is-displayed.js"],
tools = [":make_atoms_module"],
cmd = "$(location :make_atoms_module) $< $@",
visibility = ["//javascript/node/selenium-webdriver:__pkg__"],
)

genrule(
name = "get_attribute",
srcs = ["//javascript/webdriver/atoms:get-attribute.js"],
outs = ["get-attribute.js"],
tools = [":make_atoms_module"],
cmd = "$(location :make_atoms_module) $< $@",
visibility = ["//javascript/node/selenium-webdriver:__pkg__"],
)

12 changes: 12 additions & 0 deletions javascript/node/selenium-webdriver/lib/atoms/make-atoms-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require('fs');
const path = require('path');

if (process.argv.length < 3) {
process.stderr.write(`Usage: node ${path.basename(__filename)} <src file> <dst file>\n`);
process.exit(-1);
}

const buffer = fs.readFileSync(process.argv[2]);
fs.writeFileSync(process.argv[3], `// GENERATED CODE - DO NOT EDIT
module.exports = ${buffer.toString('utf8').trim()};
`);
34 changes: 0 additions & 34 deletions javascript/node/selenium-webdriver/lib/devmode.js

This file was deleted.

34 changes: 25 additions & 9 deletions javascript/node/selenium-webdriver/lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,34 @@ const promise = require('./promise');
const {Session} = require('./session');
const {WebElement} = require('./webdriver');

const {getAttribute, isDisplayed} = /** @suppress {undefinedVars|uselessCode} */(function() {
const getAttribute = requireAtom(
'./atoms/get-attribute.js',
'//javascript/node/selenium-webdriver/lib/atoms:get-attribute.js');
const isDisplayed = requireAtom(
'./atoms/is-displayed.js',
'//javascript/node/selenium-webdriver/lib/atoms:is-displayed.js');

/**
* @param {string} module
* @param {string} bazelTarget
* @return {!Function}
*/
function requireAtom(module, bazelTarget) {
try {
return {
getAttribute: require('./atoms/get-attribute.js'),
isDisplayed: require('./atoms/is-displayed.js')
};
return require(module);
} catch (ex) {
throw Error(
'Failed to import atoms modules. If running in devmode, you need to run'
+ ' `./go node:atoms` from the project root: ' + ex);
try {
const file = bazelTarget.slice(2).replace(':', '/');
return require(`../../../../bazel-genfiles/${file}`);
} catch (ex2) {
console.log(ex2);
throw Error(
`Failed to import atoms module ${module}. If running in dev mode, you`
+ ` need to run \`bazel build ${bazelTarget}\` from the project`
+ `root: ${ex}`);
}
}
})();
}


/**
Expand Down
21 changes: 13 additions & 8 deletions javascript/node/selenium-webdriver/lib/test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@

'use strict';

const spawn = require('child_process').spawn,
fs = require('fs'),
path = require('path');
const fs = require('fs');
const path = require('path');
const {spawn} = require('child_process');

const isDevMode = require('../devmode');

var projectRoot = path.normalize(path.join(__dirname, '../../../../..'));
const PROJECT_ROOT = path.normalize(path.join(__dirname, '../../../../..'));
const WORKSPACE_FILE = path.join(PROJECT_ROOT, 'WORKSPACE');

function isDevMode() {
return fs.existsSync(WORKSPACE_FILE)
}


function checkIsDevMode() {
if (!isDevMode) {
if (!isDevMode()) {
throw Error('Cannot execute build; not running in dev mode');
}
}
Expand Down Expand Up @@ -128,6 +132,8 @@ Build.prototype.go = function() {

// PUBLIC API

exports.isDevMode = isDevMode;


/**
* Creates a build of the listed targets.
Expand All @@ -146,6 +152,5 @@ exports.of = function(var_args) {
* @throws {Error} If not running in dev mode.
*/
exports.projectRoot = function() {
checkIsDevMode();
return projectRoot;
return PROJECT_ROOT;
};
22 changes: 17 additions & 5 deletions javascript/node/selenium-webdriver/lib/test/fileserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ const express = require('express');
const multer = require('multer');
const serveIndex = require('serve-index');

const isDevMode = require('../devmode');
const {isDevMode} = require('./build');
const resources = require('./resources');
const {Server} = require('./httpserver');

const WEB_ROOT = '/common';
const DATA_ROOT = '/data';
const JS_ROOT = '/javascript';

const baseDirectory = resources.locate(isDevMode ? 'common/src/web' : '.');
const baseDirectory = resources.locate('common/src/web');
const dataDirectory = path.join(__dirname, 'data');
const jsDirectory = resources.locate(isDevMode ? 'javascript' : '..');
const jsDirectory = resources.locate('javascript');

const Pages = (function() {
var pages = {};
Expand Down Expand Up @@ -135,7 +135,7 @@ app.get('/', sendIndex)
.get(Path.REDIRECT, redirectToResultPage)
.get(Path.SLEEP, sendDelayedResponse)

if (isDevMode) {
if (isDevMode()) {
var closureDir = resources.locate('third_party/closure/goog');
app.use('/third_party/closure/goog',
serveIndex(closureDir), express.static(closureDir));
Expand Down Expand Up @@ -212,6 +212,18 @@ function handleUpload(request, response) {


function sendEcho(request, response) {
if (request.query['html']) {
const html = request.query['html'];
if (html) {
response.writeHead(200, {
'Content-Length': Buffer.byteLength(html, 'utf8'),
'Content-Type': 'text/html; charset=utf-8'
});
response.end(html);
return;
}
}

var body = [
'<!DOCTYPE html>',
'<title>Echo</title>',
Expand Down Expand Up @@ -255,7 +267,7 @@ function sendIndex(request, response) {
var data = ['<!DOCTYPE html><h1>/</h1><hr/><ul>',
createListEntry('common'),
createListEntry('data')];
if (isDevMode) {
if (isDevMode()) {
data.push(createListEntry('javascript'));
}
data.push('</ul>');
Expand Down

0 comments on commit 44a6545

Please sign in to comment.