forked from LambdaTest/codeceptjs-lambdatest-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (78 loc) · 2.75 KB
/
index.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
var updateSessionById = require('./lib/apiClient').updateSessionById
const supportedHelpers = [
'WebDriver',
'Appium',
'Playwright'
];
class LambdaTestHelper extends Helper{
_init(){
this.updateTestName = this.config.updateTestName
if (!this.config.user || !this.config.key) throw new Error('Please provide proper Lambdatest credentials')
this.lambdatestCredentials = {
username: this.config.user,
accessKey: this.config.key,
isApp: this.config?.isApp || false
}
}
async getSessionId (helper, isApp) {
try {
if ( !isApp && helper.WebDriver) {
return helper.WebDriver.browser.sessionId;
}
if ( !isApp && helper.Playwright) {
const { page } = helper.Playwright;
const resp =await JSON.parse(await page.evaluate((_) => {}, `lambdatest_action: ${JSON.stringify({action: 'getTestDetails'})}`));
return resp?.data?.session_id;
}
if (helper.helpers.Appium) {
return helper.helpers.Appium.browser.sessionId;
}
} catch(err){
console.log("Error - ", err)
}
throw new Error(`No matching helper found. Supported helpers: ${supportedHelpers.join('/')}`);
}
getBody(testTitle = null, status = null){
let body = {}
if(status != null) body.status_ind = status
if (this.updateTestName && testTitle != null) body.name = testTitle
return body
}
async updateJob(sessionId, body, lambdaCredentials) {
const sleep = ms => new Promise(r => setTimeout(r, ms));
await sleep(2000)
try{
await updateSessionById(sessionId, body, lambdaCredentials)
}catch (err){
console.log("Update api call error- ", err)
}
}
async _failed(test){
console.log("Test Failed", test.title)
var sessionId = await this.getSessionId(this.helpers,this.lambdatestCredentials.isApp)
console.log("Test ID", sessionId)
if (sessionId && test.title){
var body = this.getBody(null, "failed")
this.updateJob(sessionId, body, this.lambdatestCredentials)
}
}
async _passed(test){
console.log("Test Passed", test.title)
var sessionId = await this.getSessionId(this.helpers,this.lambdatestCredentials.isApp)
console.log("Test ID", sessionId)
if (sessionId && test.title){
var body = this.getBody(null, "passed")
this.updateJob(sessionId, body, this.lambdatestCredentials)
}
}
async _before(test)
{
var sessionId = await this.getSessionId(this.helpers,this.lambdatestCredentials.isApp)
console.log("Test ID", sessionId)
if (sessionId && test.title){
var body = this.getBody(test.title,null)
this.updateJob(sessionId, body, this.lambdatestCredentials)
}
}
}
module.exports = LambdaTestHelper