Skip to content

Commit 1134330

Browse files
committed
Playwright sdk and hooks
1 parent 7a75920 commit 1134330

File tree

6 files changed

+208
-0
lines changed

6 files changed

+208
-0
lines changed

hooks/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "lambdatest-playwright-sample",
3+
"scripts": {
4+
"test": "node playwright-single.js",
5+
"test-smartui": "node playwright-smartui.js"
6+
},
7+
"engines": {
8+
"node": ">=12.7.0"
9+
},
10+
"devDependencies": {
11+
"@playwright/test": "^1.35.0",
12+
"playwright": "^1.35.0",
13+
"@cucumber/cucumber": "^7.3.2",
14+
"chai": "^4.3.7"
15+
}
16+
}
17+

hooks/playwright-smartui.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const { chromium } = require('playwright')
2+
const { expect } = require('@playwright/test');
3+
4+
(async () => {
5+
const capabilities = {
6+
'browserName': 'Chrome', // Browsers allowed: `Chrome`, `MicrosoftEdge`, `pw-chromium`, `pw-firefox` and `pw-webkit`
7+
'browserVersion': 'latest',
8+
'LT:Options': {
9+
'platform': 'Windows 10',
10+
'build': 'Playwright SmartUI Build',
11+
'name': 'Playwright SmartUI Test',
12+
'user': process.env.LT_USERNAME || '<USERNAME>',
13+
'accessKey': process.env.LT_ACCESS_KEY || '<ACCESS KEY>',
14+
'network': true,
15+
'video': true,
16+
'console': true,
17+
'smartUIProjectName': 'Playwright-SmartUI-Project4',
18+
// 'smartUIBaseline': false
19+
}
20+
}
21+
22+
const githubURL = process.env.GITHUB_URL
23+
if(githubURL){
24+
capabilities['LT:Options']['github'] = {
25+
url : githubURL
26+
}
27+
}
28+
29+
const browser = await chromium.connect({
30+
wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`
31+
})
32+
33+
const page = await browser.newPage()
34+
35+
await page.goto('https://www.lambdatest.com')
36+
37+
// Add the following command in order to take screenshot in SmartUI
38+
await page.evaluate((_) => {},
39+
`lambdatest_action: ${JSON.stringify({ action: 'smartui.takeScreenshot', arguments: { fullPage: true, screenshotName: 'search-lambdatest' }
40+
})}`) // Add a relevant screenshot name here
41+
42+
43+
try {
44+
expect(title).toEqual('LambdaTest - Search')
45+
// Mark the test as completed or failed
46+
await page.evaluate(_ => {}, `lambdatest_action: ${JSON.stringify({ action: 'setTestStatus', arguments: { status: 'passed', remark: 'Title matched' } })}`)
47+
} catch {
48+
await page.evaluate(_ => {}, `lambdatest_action: ${JSON.stringify({ action: 'setTestStatus', arguments: { status: 'failed', remark: 'Title not matched' } })}`)
49+
}
50+
51+
await page.goto("https://www.lambdatest.com")
52+
53+
await page.evaluate((_) => {},
54+
`lambdatest_action: ${JSON.stringify({ action: 'smartui.takeScreenshot', arguments: { fullPage: true, screenshotName: 'lambdatest-website' }
55+
})}`)
56+
await page.evaluate((_) => {},`lambdatest_action: ${JSON.stringify({ action: 'smartui.takeScreenshot', arguments: { fullPage: false, screenshotName: '<Screenshot Name>'}})}`)
57+
await page.goto("https://www.lambdatest.com/support/api-doc/")
58+
await page.evaluate((_) => {},
59+
`lambdatest_action: ${JSON.stringify({ action: 'smartui.takeScreenshot', arguments: { fullPage: true, screenshotName: 'api-doc' }
60+
})}`)
61+
62+
await browser.close()
63+
})()

sdk/ignorePlaywright.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { chromium } = require("playwright");
2+
const { expect } = require("@playwright/test");
3+
const smartuiSnapshot = require("@lambdatest/playwright-driver");
4+
5+
(async () => {
6+
const capabilities = {
7+
browserName: "Chrome",
8+
browserVersion: "latest",
9+
"LT:Options": {
10+
platform: "Windows 10",
11+
build: "Playwright SmartUI Build",
12+
name: "Playwright SmartUI Test",
13+
user: "jeeveshiitj",
14+
accessKey: "mtPSfImA94UZNUj92fOctXE1VrKR4uDAoYrVU1kKJlqzclR6xU",
15+
network: true,
16+
video: true,
17+
console: true,
18+
},
19+
};
20+
21+
const githubURL = process.env.GITHUB_URL;
22+
if (githubURL) {
23+
capabilities["LT:Options"]["github"] = {
24+
url: githubURL,
25+
};
26+
}
27+
28+
const browser = await chromium.connect({
29+
wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
30+
JSON.stringify(capabilities)
31+
)}`,
32+
});
33+
34+
const page = await browser.newPage();
35+
36+
await page.goto("https://www.lambdatest.com");
37+
38+
// Add the following command in order to take screenshot in SmartUI
39+
await smartuiSnapshot.smartuiSnapshot(page, "LT-Home");
40+
await browser.close();
41+
})();

sdk/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "lambdatest-playwright-sample",
3+
"scripts": {
4+
"test": "node playwright-single.js",
5+
"test-smartui": "node playwright-smartui.js"
6+
},
7+
"engines": {
8+
"node": ">=12.7.0"
9+
},
10+
"devDependencies": {
11+
"@playwright/test": "^1.35.0",
12+
"playwright": "^1.35.0",
13+
"@cucumber/cucumber": "^7.3.2",
14+
"chai": "^4.3.7"
15+
},
16+
"dependencies": {
17+
"@lambdatest/playwright-driver": "^1.0.3",
18+
"@lambdatest/smartui-cli": "^2.0.6"
19+
}
20+
}

sdk/playwrightCloud.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { chromium } = require("playwright");
2+
const { expect } = require("@playwright/test");
3+
const smartuiSnapshot = require("@lambdatest/playwright-driver");
4+
5+
6+
// username: Username can be found at automation dashboard
7+
const USERNAME = process.env.LT_USERNAME || "<USERNAME>";
8+
9+
// AccessKey: AccessKey can be generated from automation dashboard or profile section
10+
const KEY = process.env.LT_ACCESS_KEY || "<ACCESS_KEY>";
11+
(async () => {
12+
const capabilities = {
13+
browserName: "Chrome",
14+
browserVersion: "latest",
15+
"LT:Options": {
16+
platform: "Windows 10",
17+
build: "Playwright SmartUI Build",
18+
name: "Playwright SmartUI Test",
19+
user: USERNAME,
20+
accessKey: KEY,
21+
network: true,
22+
video: true,
23+
console: true,
24+
},
25+
};
26+
27+
const githubURL = process.env.GITHUB_URL;
28+
if (githubURL) {
29+
capabilities["LT:Options"]["github"] = {
30+
url: githubURL,
31+
};
32+
}
33+
34+
const browser = await chromium.connect({
35+
wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
36+
JSON.stringify(capabilities)
37+
)}`,
38+
});
39+
40+
const page = await browser.newPage();
41+
42+
await page.goto("https://www.lambdatest.com");
43+
44+
// Add the following command in order to take screenshot in SmartUI
45+
await smartuiSnapshot.smartuiSnapshot(page, "LT-Home");
46+
await browser.close();
47+
})();

sdk/playwrightLocal.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { chromium } = require("playwright");
2+
const smartuiSnapshot = require("@lambdatest/playwright-driver");
3+
4+
(async () => {
5+
// Launch a local browser instance
6+
const browser = await chromium.launch({
7+
headless: false, // Set to false to see the browser UI
8+
});
9+
10+
const page = await browser.newPage();
11+
12+
// Navigate to the desired URL
13+
await page.goto("https://www.lambdatest.com");
14+
15+
// Use smartuiSnapshot to take a visual snapshot locally
16+
await smartuiSnapshot.smartuiSnapshot(page, "LT-Home");
17+
18+
// Close the browser
19+
await browser.close();
20+
})();

0 commit comments

Comments
 (0)