forked from matheuss/google-translate-token
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
52 lines (48 loc) · 1.94 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import test from 'ava';
import {get as getToken} from './index';
const browser = require('webdriverio').remote({
user: process.env.SAUCE_USERNAME,
key: process.env.SAUCE_ACCESS_KEY,
host: 'localhost',
port: 4445,
desiredCapabilities: {
'browserName': 'chrome',
'tunnel-identifier': process.env.TRAVIS_JOB_NUMBER
}
});
test('check if what we generate equals to what translate.google.com generates', async t => {
try {
const token = await getToken('hello');
const returned = await browser
.init()
.url('http://translate.google.com')
.timeoutsAsyncScript(10000)
.executeAsync((tokenName, callback) => {
setTimeout(function () {
injectAjaxInterceptor(tokenName, callback);
document.getElementById('source').value = 'hello'; // eslint-disable-line no-undef
}, 0);
function injectAjaxInterceptor(tokenName, callback) {
/* eslint-disable no-undef */
XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function (data) {
/* eslint-enable no-undef */
var _this = this;
setTimeout(function () {
if (_this.responseURL.indexOf('single') !== -1) {
var regex = new RegExp('[?&]' + tokenName + '(=([^&#]*)|&|#|$)');
var results = regex.exec(_this.responseURL);
callback(results[2]);
}
}, 5000);
this.reallySend(data);
};
}
}, token.name);
t.is(token.value, returned.value);
} catch (err) {
t.fail(err);
} finally {
browser.end();
}
});