-
Notifications
You must be signed in to change notification settings - Fork 0
/
automation.js
127 lines (118 loc) · 4.83 KB
/
automation.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
const fs = require('fs')
const path = require('path')
const os = require('os')
const { remote, Key } = require('webdriverio')
const downloadsDir = path.join(os.homedir(), 'Downloads')
const downloadHelper = async (downloadTriggerFn, afterDownloadedFn) => {
const getDownloads = () => fs.readdirSync(downloadsDir).filter(f => !(f.endsWith('crdownlaod') || f.endsWith('.part')))
const originalItems = getDownloads()
await downloadTriggerFn()
var newItems = []
while (newItems.length == 0) {
await new Promise(resolve => setTimeout(resolve, 500));
newItems = getDownloads().filter(e => originalItems.indexOf(e) < 0)
}
await afterDownloadedFn(newItems)
}
const automateGenSimple = async (input, filename, dir, inputChanges = []) => {
var browser;
try {
// Skip if okay
const saveDir = dir ?? downloadsDir
const saveName = path.join(saveDir, `${filename}.png`);
const solvedSaveName = path.join(saveDir, `${filename}_solved.png`);
if (fs.existsSync(saveName) && fs.existsSync(solvedSaveName)) {
console.log(`${saveName} and solution already exist, skipping...`)
return
}
// Init
browser = await remote({
capabilities: {
browserName: 'chrome', // You can use other browsers as well
'goog:chromeOptions': {
prefs: {
'profile.default_content_setting_values.automatic_downloads': 1,
},
args: ['--auto-open-devtools-for-tabs'],
},
},
connectionRetryTimeout: 10 * 60 * 1000 // Minutes to ms
})
await browser.url('https://wsg.imranr.dev/')
await browser.waitUntil(async () => {
const elem = await browser.$('#word-list')
return await elem.isExisting()
}, { timeout: 10000, timeoutMsg: 'Element #word-list not found' })
for (var i = 0; i < inputChanges.length; i++) {
try {
const input = (await browser.$(`#${inputChanges[i].id}`))
if (typeof inputChanges[i].value === 'boolean') {
if ((await input.isSelected()) != inputChanges[i]) {
await input.click()
}
} else {
await input.setValue(inputChanges[i].value)
}
} catch (e) {
console.error(e)
}
}
// Generate
await (await browser.$('#word-list')).setValue(input)
await (await browser.$('#generate-btn')).click()
await browser.waitUntil(async () => {
const elem = await browser.$('#word-search-table')
return await elem.isExisting()
}, { timeout: 10000, timeoutMsg: 'Element #word-search-table not found' })
browser.pause(10000)
// Save unsolved
await downloadHelper(
async () => {
await (await browser.$('#save-btn')).click()
await browser.acceptAlert()
},
async (downloadedFiles) => {
fs.renameSync(path.join(downloadsDir, downloadedFiles[0]), saveName)
}
)
// Solve
await (await browser.$('#solve-btn')).click()
// Save solved
await downloadHelper(
async () => {
await (await browser.$('#save-btn')).click()
await browser.acceptAlert()
},
async (downloadedFiles) => {
fs.renameSync(path.join(downloadsDir, downloadedFiles[0]), solvedSaveName)
}
)
} catch (error) {
console.error('An error occurred:', error)
} finally {
if (browser) {
await browser.deleteSession()
}
}
}
const main = async (inputFile, outputDir, inputChanges) => {
const entries = fs.readFileSync(inputFile).toString().split('\n').map(s => s.trim()).filter(s => s.length > 0).map(s => {
var parts = s.split(':').map(p => p.trim())
if (parts.length == 1) {
parts = [parseInt((Math.random() * 10000)).toString(), parts[0]]
} else if (parts.length > 2) {
parts = [parts[0], parts.slice(1).join(':')]
}
return {
filename: parts[0],
inputText: parts[1].split(' ').join('\n')
}
})
for (let i = 0; i < entries.length; i++) {
await automateGenSimple(entries[i].inputText, entries[i].filename, outputDir, inputChanges)
}
}
const inputFile = process.argv[2] || '../word-search-generator/automation-example.txt'
const outputDir = process.argv[2] ? path.resolve(path.dirname(inputFile)) : null
const inputChanges = process.argv[3] ? JSON.parse(process.argv[3]) : []
main(inputFile, outputDir, inputChanges).then(() => console.log('Done!')).catch(e => console.error(e))