Skip to content

Commit fa3d0b5

Browse files
committed
feat: implement uploading new logs to Sauce with all test statuses
1 parent eb8dc10 commit fa3d0b5

File tree

5 files changed

+123
-24
lines changed

5 files changed

+123
-24
lines changed

package-lock.json

Lines changed: 78 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
],
3838
"author": "Vojta Jina <[email protected]>",
3939
"dependencies": {
40+
"fs-extra": "^9.0.1",
4041
"global-agent": "^2.1.8",
41-
"saucelabs": "^4.3.0",
42+
"saucelabs": "^4.5.1",
4243
"webdriverio": "^6.1.9"
4344
},
4445
"license": "MIT",

src/browser-info.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import {SauceLabsOptions} from 'saucelabs'
22
import {BrowserObject} from "webdriverio";
33

44
type SauceBaseOption = Pick<SauceLabsOptions, 'headless' | 'region'>
5+
interface Results {
6+
status: string;
7+
message: string;
8+
screenshot: string;
9+
}
510

611
/**
712
* This interface describes a browser that has been launched with Saucelabs. This is helpful
@@ -17,8 +22,8 @@ export interface SaucelabsBrowser extends SauceBaseOption {
1722
/** Saucelabs access key that has been used to launch this browser. */
1823
accessKey: string;
1924

20-
/** Saucelabs driver instance to communicate with this browser. */
21-
driver: BrowserObject
25+
/** All test results will be stored here **/
26+
results: Results[];
2227
}
2328

2429
/** Type that describes the BrowserMap injection token. */

src/launcher/launcher.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {remote, BrowserObject} from 'webdriverio';
2+
import SauceLabs from 'saucelabs';
3+
import {mkdirSync, removeSync, writeFileSync} from 'fs-extra';
24
import {processConfig} from "../process-config";
35
import {BrowserMap} from "../browser-info";
46

@@ -73,7 +75,7 @@ export function SaucelabsLauncher(args,
7375
accessKey: seleniumCapabilities.key,
7476
region: seleniumCapabilities.region,
7577
headless: seleniumCapabilities.headless,
76-
driver,
78+
results:[],
7779
});
7880

7981
await driver.url(pageUrl);
@@ -88,7 +90,30 @@ export function SaucelabsLauncher(args,
8890
this.on('kill', async (done: () => void) => {
8991
try {
9092
const driver = connectedDrivers.get(this.id);
93+
const {sessionId} = driver;
9194
await driver.deleteSession();
95+
96+
const browserData = browserMap.get(this.id);
97+
const api = new SauceLabs({
98+
user: browserData.username,
99+
key: browserData.accessKey,
100+
region: browserData.region,
101+
});
102+
103+
// Wait until the vm is destroyed and the assets are stored
104+
await new Promise(resolve => setTimeout(() => resolve(), 5000));
105+
// Create a tmp dir
106+
mkdirSync(sessionId);
107+
writeFileSync(`${sessionId}/log.json`, JSON.stringify(browserData.results, null, 2));
108+
// Update the log assets
109+
// @ts-ignore
110+
await api.uploadJobAssets(
111+
sessionId,
112+
{ files: [`${sessionId}/log.json`] },
113+
);
114+
// remove the temporary folder
115+
removeSync(sessionId);
116+
92117
} catch (e) {
93118
// We need to ignore the exception here because we want to make sure that Karma is still
94119
// able to retry connecting if Saucelabs itself terminated the session (and not Karma)

src/reporter/reporter.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ export function SaucelabsReporter(logger, browserMap: BrowserMap) {
2727
// This fires when a single test is executed and will update the run in sauce labs with an annotation
2828
// of the test including the status of the test
2929
this.onSpecComplete = function(browser, result) {
30-
const driver = browserMap.get(browser.id).driver
3130
const status = result.success ? '✅' : '❌'
3231

33-
pendingUpdates.push(driver.execute(`sauce:context=${status}: ${result.fullName}`))
32+
browserMap.get(browser.id).results.push({
33+
status: 'info',
34+
message: `${status} ${result.fullName}`,
35+
screenshot: null
36+
})
3437

3538
if(!result.success && result.log.length > 0){
36-
pendingUpdates.push(driver.execute(`sauce:context=${result.log[0]}`))
39+
browserMap.get(browser.id).results.push({
40+
status: 'info',
41+
message: `${result.log[0]}`,
42+
screenshot: null
43+
})
3744
}
3845
}
3946

0 commit comments

Comments
 (0)