-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
103 lines (88 loc) · 2.07 KB
/
Copy pathtest.js
File metadata and controls
103 lines (88 loc) · 2.07 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var childProcess = require('child_process')
var http = require('./http').sync
var path = require('path')
var url = require('url')
class Driver {
/**
* Create a new driver instance & start it
*/
static start (driverBin, port) {
var driver = new Driver('http://localhost:' + port)
driver.start(driverBin, port)
return driver
}
constructor (host) {
this.host = host
}
/**
* Start the driver as a child process
*/
start (driverBin, port) {
this.process = childProcess.spawn(driverBin, ['--port=' + port])
}
/**
* HTTP request to create a new session, then return a new session instance with created obj
*/
createSession () {
return new Session(
this,
this.request(
'/session',
'POST',
{
// Chromedriver needs this, can be empty though:
desiredCapabilities: {}
}
)
)
}
/**
* Make a request to the driver endpoint, given a path
*/
request (path, method, data) {
var reqUrl = url.resolve(this.host, path)
return http(reqUrl, method, data)
}
}
class Session {
/**
* Create a new drver & session & return it
*/
static start (driver, port = 4444) {
// TODO: Keep map of signatures -> Driver instances
if (!Session.driver) {
Session.driver = Driver.start(driver, port)
}
return Session.driver.createSession()
}
constructor (driver, session) {
this.driver = driver
if (!session.sessionId) {
throw 'No session Id:\n' + JSON.stringify(session)
}
this.path = path.join('session', session.sessionId)
}
/**
* Make a driver request to this session or one of its child endpoints
*/
request (part, method, data) {
return this.driver.request(path.join(this.path, part), method, data)
}
/**
* Set the page URL
*/
go (url) {
this.request('url', 'POST', { url })
}
/**
* End the session (&process)
*/
close () {
this.request('', 'DELETE')
// TODO: Only kill if there are no more sessions
this.driver.process.kill()
}
}
module.exports = {
Session, Driver
}