Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions .github/workflows/release-js.yaml
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 }}
1 change: 1 addition & 0 deletions js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
9 changes: 9 additions & 0 deletions js/packages/sdk-utils/index.js
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
}
25 changes: 25 additions & 0 deletions js/packages/sdk-utils/package.json
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"
}
}
52 changes: 52 additions & 0 deletions js/packages/sdk-utils/src/lib/httpClient.js
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();
34 changes: 34 additions & 0 deletions js/packages/sdk-utils/src/lib/logger.js
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'
Copy link
Collaborator

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

Copy link
Contributor Author

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


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()]
});
};
13 changes: 13 additions & 0 deletions js/packages/sdk-utils/src/lib/utils.js
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';
40 changes: 40 additions & 0 deletions js/packages/sdk-utils/src/smartui.js
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`);
}
}
5 changes: 5 additions & 0 deletions js/packages/selenium-driver/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { smartuiSnapshot } from './src/smartui.js';

export {
smartuiSnapshot
};
24 changes: 24 additions & 0 deletions js/packages/selenium-driver/package.json
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:^"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

workspace will work at market place as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

}
}
25 changes: 25 additions & 0 deletions js/packages/selenium-driver/src/smartui.js
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);
}
}
7 changes: 7 additions & 0 deletions js/packages/selenium-driver/src/utils.js
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
}
Loading