-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (131 loc) · 4.62 KB
/
app.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Functions for automating Chrome browser tasks using the Chrome DevTools Protocol.
* @module main
*/
/**
* Get the version of the installed Chrome browser.
*
* @async
* @function getChromeVersion
* @returns {Promise<string>} The version of the installed Chrome browser.
* @throws {Error} If an error occurs while getting the Chrome version.
*/
/**
* Download the ChromeDriver binary corresponding to the specified version.
*
* @async
* @function downLoadDriver
* @param {string} version - The version of the ChromeDriver binary to download.
* @returns {Promise<Buffer>} The downloaded ChromeDriver binary data.
* @throws {Error} If an error occurs during the download.
*/
/**
* Save the downloaded ChromeDriver binary data to a local file.
*
* @async
* @function saveZipFile
* @param {Buffer} data - The downloaded ChromeDriver binary data.
* @returns {Promise<string>} The path to the saved zip file.
* @throws {Error} If an error occurs while saving the zip file.
*/
/**
* Unzip a file to the specified directory.
*
* @async
* @function unzipFile
* @param {string} zipFilePath - The path to the zip file.
* @param {string} outputDir - The directory to unzip the file to.
* @returns {Promise<void>} A Promise that resolves when the file is successfully unzipped.
* @throws {Error} If an error occurs during the unzip process.
*/
/**
* Start the ChromeDriver server.
*
* @async
* @function start
* @param {string} driverPath - The path to the ChromeDriver binary.
* @returns {Promise<void>} A Promise that resolves when the ChromeDriver server is started.
* @throws {Error} If an error occurs while starting the ChromeDriver server.
*/
/**
* Create a new Chrome DevTools session.
*
* @async
* @function createSession
* @param {string} endpoint - The Chrome DevTools Protocol endpoint (e.g., 'http://localhost:9515').
* @returns {Promise<string>} The session ID for the newly created session.
* @throws {Error} If an error occurs during session creation.
*/
/**
* Navigate to the specified URL in the active Chrome session.
*
* @async
* @function navigateToUrl
* @param {string} sessionId - The ID of the Chrome DevTools session.
* @param {string} url - The URL to navigate to.
* @returns {Promise<void>} A Promise that resolves when the navigation is complete.
* @throws {Error} If an error occurs during navigation.
*/
/**
* Turn on full-screen mode in the active Chrome session.
*
* @async
* @function turnFullScreen
* @param {string} sessionId - The ID of the Chrome DevTools session.
* @returns {Promise<void>} A Promise that resolves when full-screen mode is activated.
* @throws {Error} If an error occurs while turning on full-screen mode.
*/
/**
* Take a screenshot in the active Chrome session and save it to a file.
*
* @async
* @function takeScreenShot
* @param {string} sessionId - The ID of the Chrome DevTools session.
* @param {string} fileName - The name to save the screenshot file as.
* @returns {Promise<string>} The path to the saved screenshot file.
* @throws {Error} If an error occurs during screenshot capture.
*/
/**
* Get information about the currently focused element in the active Chrome session.
*
* @async
* @function getActiveElem
* @param {string} sessionId - The ID of the Chrome DevTools session.
* @returns {Promise<Object>} Information about the active element.
* @throws {Error} If an error occurs while retrieving information about the active element.
*/
/**
* Close the specified Chrome DevTools session.
*
* @async
* @function closeSession
* @param {string} sessionId - The ID of the Chrome DevTools session.
* @returns {Promise<void>} A Promise that resolves when the session is closed.
* @throws {Error} If an error occurs during session closure.
*/
/**
* Immediately execute the main script to automate Chrome browser tasks.
*
* @async
* @function
*/
(async () => {
try {
// Example usage of the functions defined above
const version = await getChromeVersion();
console.log(version);
const data = await downLoadDriver(version);
const savedData = await saveZipFile(data);
const decompress = await unzipFile('chromedriver.zip', 'dist');
const run = await start('./dist/chromedriver');
const id = await createSession('http://localhost:9515');
const fc = await turnFullScreen(id);
const navigate = await navigateToUrl(id, 'https://www.w3schools.com/');
const getImage = await takeScreenShot(id, 'w3c');
const elem = await getActiveElem(id);
console.log(await elem);
const exit = await closeSession(id);
} catch (e) {
console.log(e);
}
})();