-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenshot_worker.ts
96 lines (80 loc) · 3.94 KB
/
screenshot_worker.ts
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
import { BrowserFactory, DEFAULT_CONNECTION_PARAMS, factoryOptions } from "./src/TBBrowserFactory.js";
import {CapabilityFactory} from "./src/TBCapabilityFactory.js";
import {createImageWriter} from "./src/writeImageData.js";
import RabbitMQConsumer from "./src/MessageQueue/RabbitMQConsumer";
import {TestCaseMessage} from "./src/MessageQueue/Messages";
import {serializeTestCase, unserializeTestCase} from "./src/Model/TestCaseSerializer";
import EnvironmentConfig from "./src/EnvironmentConfig";
import { getTestFunction } from "./src/test_functions";
import RabbitMQProducer from "./src/MessageQueue/RabbitMQProducer";
import { TestCase, TestCaseState, TestCaseFailedState } from "./src/Model/TestCase";
import RabbitMQConnection from "./src/MessageQueue/RabbitMQConnection";
import path from "path";
import process from "process";
import {WorkerConfigFactory} from "./src/CommandLine/WorkerConfig";
process.umask(0o002);
const config = new EnvironmentConfig();
const cliConfig = new WorkerConfigFactory();
const connectionOptions = {
...DEFAULT_CONNECTION_PARAMS,
user: config.testingBotApiKey,
key: config.testingBotApiSecret
}
const browserFactory = new BrowserFactory( connectionOptions,
new CapabilityFactory( factoryOptions )
);
const options = cliConfig.getConfig( 'A queue worker that processes screenshot messages', process.cwd() );
const showMessage = options.verbose ? console.log : () => {};
const queueConnection = new RabbitMQConnection( config.queueUrl, "Connection established, hit Ctrl-C to quit worker" );
const consumer = new RabbitMQConsumer( queueConnection );
const producer = new RabbitMQProducer( queueConnection );
// Send a message with the updated test case to the metadata worker.
// The screenshot worker and the test function update the test case state (from "pending" to "running" to "finished"/"failed")
const sendMetadataUpdate = async ( testCase: TestCase, campaignName: string ): Promise<void> =>
producer.sendMetadataUpdate( {
msgType: "update",
testCase: serializeTestCase( testCase ),
campaignName
} );
// Create a function that updates the test state and saves the test state on transitions
// Also logs state description updates and transitions if verbosity is on
const getTestCaseStateChangeFunction = ( campaignName: string ) =>
async ( testCase: TestCase, newState: TestCaseState ): Promise<void> => {
const oldStateName = testCase.getLastStateName();
testCase.updateState( newState );
let msg = `Test case state update: ${testCase.getName()} - ${newState.description}`;
// Write metadata to file on state change
if ( oldStateName !== newState.stateName ) {
msg = `Test case state transition: ${testCase.getName()}- ${oldStateName} to ${newState.stateName} - ${newState.description}`;
await sendMetadataUpdate( testCase, campaignName )
}
showMessage( msg );
}
consumer.consumeScreenshotQueue( async (msgData: TestCaseMessage) => {
const writeImageData = await createImageWriter( path.join( options.outputPath, msgData.trackingName ) );
const testCase = unserializeTestCase( msgData.testCase );
const onTestCaseStateChange = getTestCaseStateChangeFunction( msgData.trackingName )
showMessage( `Creating a browser instance for test ${testCase.getName()}` );
let browser;
try {
browser = await browserFactory.getBrowser(testCase);
} catch ( e ) {
onTestCaseStateChange( testCase, new TestCaseFailedState( 'Failed creating remote browser instance: ' + e.message ) );
showMessage( `Could not create browser instance for test ${testCase.getName()}` );
showMessage( e );
return;
}
showMessage( `Taking screenshot for test ${testCase.getName()}` );
let testFunction;
try {
testFunction = getTestFunction( msgData.testFunction );
} catch ( e ) {
onTestCaseStateChange( testCase, new TestCaseFailedState( e.message ) );
return;
}
try {
await testFunction( browser, testCase, writeImageData, onTestCaseStateChange );
} catch (e) {
onTestCaseStateChange( testCase, new TestCaseFailedState( e.message ) );
}
});