-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayMove.js
59 lines (53 loc) · 2 KB
/
PlayMove.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
53
54
55
56
57
58
59
'use strict';
const {By, Key} = require('selenium-webdriver');
const {readGrid} = require('../helpers');
function PlayMove(direction) {
var self = this;
var key = function() {
switch (direction) {
case "L": return Key.ARROW_LEFT;
case "R": return Key.ARROW_RIGHT;
case "U": return Key.ARROW_UP;
case "D": return Key.ARROW_DOWN;
}
return null;
};
var keyCode = function() {
switch (direction) {
case "L": return 37;
case "R": return 39;
case "U": return 38;
case "D": return 40;
}
return null;
};
var prettyDirection = function() {
switch (direction) {
case "L": return "left";
case "R": return "right";
case "U": return "up";
case "D": return "down";
}
return "unknown";
};
self.check = model => true;
self.run = async function(model, driver) {
var initialUrl = await driver.getCurrentUrl();
// Sending keys directly from Selenium to the 'playground'
// does not seem to work anymore
// The work-around has been to send the keys directly from the browser
// :: await driver.findElement(By.id("playground")).sendKeys(key());
await driver.executeScript(" \
const ke = new KeyboardEvent('keydown', {bubbles: true, cancelable: true, keyCode: " + keyCode() + "}); \
document.getElementById('playground').dispatchEvent(ke); \
");
var updatedUrl = await driver.getCurrentUrl();
if (initialUrl != updatedUrl) {// url has change iff the move was possible
model.play(direction);
}
return model.store(updatedUrl, await readGrid(driver));
};
self.name = "PlayMove(" + prettyDirection() + ")";
self.toString = function() { return self.name; };
}
module.exports = PlayMove;