- Notifications
You must be signed in to change notification settings - Fork 10
Add smartUISnapshot in JS selenium #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Publish JS packages | ||
| ||
on: | ||
release: | ||
types: [published] | ||
| ||
jobs: | ||
build-and-publish: | ||
runs-on: ubuntu-latest | ||
| ||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v3 | ||
| ||
- name: Set up Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '16' | ||
registry-url: 'https://registry.npmjs.org' | ||
| ||
- name: Install pnpm | ||
run: npm install -g pnpm | ||
| ||
- name: Install dependencies | ||
run: pnpm install | ||
| ||
- name: Publish packages | ||
run: pnpm publish --recursive --access public --no-git-checks | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { isSmartUIRunning, fetchDOMSerializer, postSnapshot } from './src/smartui.js' | ||
import logger from './src/lib/logger.js' | ||
| ||
export default { | ||
logger, | ||
fetchDOMSerializer, | ||
postSnapshot, | ||
isSmartUIRunning | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@lambdatest/sdk-utils", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/LambdaTest/lambdatet-sdk", | ||
"directory": "js/packages/sdk-utils" | ||
}, | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"lambdatest" | ||
], | ||
"author": "LambdaTest <keys@lambdatest.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"axios": "^1.6.2", | ||
"chalk": "^5.3.0", | ||
"winston": "^3.11.0" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import axios from 'axios' | ||
import utils from './utils.js' | ||
| ||
class httpClient { | ||
async request(config) { | ||
return axios.request(config) | ||
.then(resp => { | ||
return { | ||
status: resp.status, | ||
statusMessage: resp.statusMessage, | ||
headers: resp.headers, | ||
body: resp.data | ||
}; | ||
}) | ||
.catch(error => { | ||
if (error.response) { | ||
throw new Error(JSON.stringify(error.response.data)); | ||
} | ||
if (error.request) { | ||
throw new Error(error.toJSON().message); | ||
} | ||
throw error; | ||
}); | ||
} | ||
| ||
isSmartUIRunning() { | ||
return this.request({ | ||
url: `${utils.getSmartUIServerAddress()}/healthcheck`, | ||
method: 'GET', | ||
}) | ||
} | ||
| ||
fetchDOMSerializer() { | ||
return this.request({ | ||
url: `${utils.getSmartUIServerAddress()}/domserializer`, | ||
method: 'GET' | ||
}) | ||
} | ||
| ||
postSnapshot(data) { | ||
return this.request({ | ||
url: `${utils.getSmartUIServerAddress()}/snapshot`, | ||
method: 'POST', | ||
data: data, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
} | ||
}) | ||
} | ||
} | ||
| ||
export default new httpClient(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createLogger, transports, format, config } from 'winston' | ||
import chalk from 'chalk' | ||
| ||
const logLevel = () => { | ||
let debug = (process.env.LT_SDK_DEBUG === 'true') ? 'debug' : undefined; | ||
return debug || process.env.LT_SDK_LOG_LEVEL || 'info' | ||
} | ||
| ||
export default (logContext) => { | ||
return createLogger({ | ||
level: logLevel(), | ||
format: format.combine( | ||
format.timestamp(), | ||
format.printf(({ message, level }) => { | ||
if (typeof message === 'object') { | ||
message = JSON.stringify(message); | ||
} | ||
switch (level) { | ||
case 'debug': | ||
message = chalk.blue(message); | ||
break; | ||
case 'warn': | ||
message = chalk.yellow(message); | ||
break; | ||
case 'error': | ||
message = chalk.red(message); | ||
break; | ||
} | ||
return `[${logContext}] ${message}`; | ||
}) | ||
), | ||
transports: [new transports.Console()] | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import fs from 'fs'; | ||
import { fileURLToPath } from 'url'; | ||
import { dirname, join } from 'path'; | ||
| ||
export function getSmartUIServerAddress() { | ||
return process.env.SMARTUI_SERVER_ADDRESS || 'http://localhost:8080' | ||
} | ||
| ||
export function getPackageName() { | ||
return JSON.parse(fs.readFileSync(join(dirname(fileURLToPath(import.meta.url)), '../../package.json'), 'utf-8')).name | ||
} | ||
| ||
export * as default from './utils.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import client from './lib/httpClient.js' | ||
import logger from './lib/logger.js' | ||
import utils from './lib/utils.js' | ||
const log = logger(utils.getPackageName()) | ||
| ||
export async function isSmartUIRunning() { | ||
try { | ||
await client.isSmartUIRunning(); | ||
return true; | ||
} catch (error) { | ||
log.debug(error); | ||
return false; | ||
} | ||
} | ||
| ||
export async function fetchDOMSerializer() { | ||
try { | ||
return await client.fetchDOMSerializer(); | ||
} catch (error) { | ||
log.debug(error); | ||
throw new Error(`fetch DOMSerializer failed`); | ||
} | ||
} | ||
| ||
export async function postSnapshot(snapshotDOM, snapshotName, testType) { | ||
const data = JSON.stringify({ | ||
snapshot: { | ||
dom: snapshotDOM, | ||
name: snapshotName | ||
}, | ||
testType | ||
}); | ||
| ||
try { | ||
return await client.postSnapshot(data); | ||
} catch (error) { | ||
log.debug(error); | ||
throw new Error(`post snapshot failed`); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { smartuiSnapshot } from './src/smartui.js'; | ||
| ||
export { | ||
smartuiSnapshot | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@lambdatest/selenium-driver", | ||
"version": "1.0.0", | ||
"description": "Selenium driver for all Lambdatest functionalities", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/LambdaTest/lambdatet-sdk", | ||
"directory": "js/packages/selenium-driver" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"type": "module", | ||
"keywords": [ | ||
"lambdatest", | ||
"selenium" | ||
], | ||
"author": "LambdaTest <keys@lambdatest.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@lambdatest/sdk-utils": "workspace:^" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import utils from '@lambdatest/sdk-utils' | ||
import { getPackageName } from './utils.js'; | ||
const pkgName = getPackageName() | ||
| ||
export async function smartuiSnapshot(driver, snapshotName) { | ||
// TODO: check if driver is selenium webdriver object | ||
if (!driver) throw new Error('An instance of the selenium driver object is required.'); | ||
if (!snapshotName) throw new Error('The `snapshotName` argument is required.'); | ||
if (!(await utils.isSmartUIRunning())) throw new Error('SmartUI server is not running.'); | ||
let log = utils.logger(pkgName); | ||
| ||
try { | ||
let resp = await utils.fetchDOMSerializer(); | ||
await driver.executeScript(resp.body.data.dom); | ||
| ||
let { dom } = await driver.executeScript(options => ({ | ||
dom: SmartUIDOM.serialize(options) | ||
}), {}); | ||
| ||
await utils.postSnapshot(dom.html, snapshotName, pkgName); | ||
log.info(`Snapshot captured: ${snapshotName}`); | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import fs from 'fs'; | ||
import { fileURLToPath } from 'url'; | ||
import { dirname, join } from 'path'; | ||
| ||
export function getPackageName() { | ||
return JSON.parse(fs.readFileSync(join(dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf-8')).name | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pinanks why we are not using logger package, or sdk logger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sushobhit-lt there's no need to create a separate logger package, it will be a part of sdk-utils itself