Skip to content
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

Release v1.0.2 #19

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/sdk-utils/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isSmartUIRunning, fetchDOMSerializer, postSnapshot } from './src/smartui.js'
import logger from './src/lib/logger.js'
const { isSmartUIRunning, fetchDOMSerializer, postSnapshot } = require('./src/smartui');
const logger = require('./src/lib/logger');

export default {
module.exports = {
logger,
fetchDOMSerializer,
postSnapshot,
5 changes: 2 additions & 3 deletions packages/sdk-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "@lambdatest/sdk-utils",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/LambdaTest/lambdatest-js-sdk",
"directory": "packages/sdk-utils"
},
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@@ -19,7 +18,7 @@
"license": "MIT",
"dependencies": {
"axios": "^1.6.2",
"chalk": "^5.3.0",
"chalk": "4",
"winston": "^3.11.0"
}
}
12 changes: 5 additions & 7 deletions packages/sdk-utils/src/lib/httpClient.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios'
import utils from './utils.js'
const axios = require('axios');
const utils = require('./utils');

class httpClient {
module.exports = new class httpClient {
async request(config) {
return axios.request(config)
.then(resp => {
@@ -14,7 +14,7 @@ class httpClient {
})
.catch(error => {
if (error.response) {
throw new Error(JSON.stringify(error.response.data));
throw new Error(error.response.data.error.message);
}
if (error.request) {
throw new Error(error.toJSON().message);
@@ -47,6 +47,4 @@ class httpClient {
}
})
}
}

export default new httpClient();
};
10 changes: 5 additions & 5 deletions packages/sdk-utils/src/lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createLogger, transports, format, config } from 'winston'
import chalk from 'chalk'
const { createLogger, transports, format } = require('winston');
const chalk = require('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) => {
module.exports = function logger(logContext) {
return createLogger({
level: logLevel(),
format: format.combine(
@@ -20,10 +20,10 @@ export default (logContext) => {
message = chalk.blue(message);
break;
case 'warn':
message = chalk.yellow(message);
message = chalk.yellow(`Warning: ${message}`);
break;
case 'error':
message = chalk.red(message);
message = chalk.red(`Error: ${message}`);
break;
}
return `[${logContext}] ${message}`;
14 changes: 4 additions & 10 deletions packages/sdk-utils/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

export function getSmartUIServerAddress() {
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';
module.exports = {
getSmartUIServerAddress
};
29 changes: 16 additions & 13 deletions packages/sdk-utils/src/smartui.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import client from './lib/httpClient.js'
import logger from './lib/logger.js'
import utils from './lib/utils.js'
const log = logger(utils.getPackageName())
const client = require('./lib/httpClient');
const logger = require('./lib/logger');
const utils = require('./lib/utils');
const log = logger(require('../package.json').name);

export async function isSmartUIRunning() {
async function isSmartUIRunning() {
try {
await client.isSmartUIRunning();
return true;
@@ -13,28 +13,31 @@ export async function isSmartUIRunning() {
}
}

export async function fetchDOMSerializer() {
async function fetchDOMSerializer() {
try {
return await client.fetchDOMSerializer();
} catch (error) {
log.debug(error);
throw new Error(`fetch DOMSerializer failed`);
throw new Error(`fetch DOMSerializer failed; ${error.message}`);
}
}

export async function postSnapshot(snapshotDOM, snapshotName, testType) {
async function postSnapshot(snapshot, testType) {
const data = JSON.stringify({
snapshot: {
dom: snapshotDOM,
name: snapshotName
},
snapshot,
testType
});

try {
return await client.postSnapshot(data);
} catch (error) {
log.debug(error);
throw new Error(`post snapshot failed`);
throw new Error(`post snapshot failed; ${error.message}`);
}
}

module.exports = {
isSmartUIRunning,
fetchDOMSerializer,
postSnapshot
}
6 changes: 3 additions & 3 deletions packages/selenium-driver/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { smartuiSnapshot } from './src/smartui.js';
const { smartuiSnapshot } = require('./src/smartui');

export {
module.exports = {
smartuiSnapshot
};
}
3 changes: 1 addition & 2 deletions packages/selenium-driver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lambdatest/selenium-driver",
"version": "1.0.1",
"version": "1.0.2",
"description": "Selenium driver for all Lambdatest functionalities",
"main": "index.js",
"repository": {
@@ -11,7 +11,6 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"keywords": [
"lambdatest",
"selenium"
23 changes: 14 additions & 9 deletions packages/selenium-driver/src/smartui.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import utils from '@lambdatest/sdk-utils'
import { getPackageName } from './utils.js';
const pkgName = getPackageName()
const utils = require('@lambdatest/sdk-utils');
const pkgName = require('../package.json').name;

export async function smartuiSnapshot(driver, snapshotName) {
async function smartuiSnapshot(driver, name, options = {}) {
// 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 (!name) throw new Error('The `name` 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)
let { dom, url } = await driver.executeScript(options => ({
dom: SmartUIDOM.serialize(options),
url: document.URL
}), {});

await utils.postSnapshot(dom.html, snapshotName, pkgName);
log.info(`Snapshot captured: ${snapshotName}`);
let { body } = await utils.postSnapshot({url, name, dom, options}, pkgName);
log.info(`Snapshot captured: ${name}`);
if (body && body.data && body.data.warnings?.length !== 0) body.data.warnings.map(e => log.warn(e));
} catch (error) {
throw new Error(error);
}
}

module.exports = {
smartuiSnapshot
}
7 changes: 0 additions & 7 deletions packages/selenium-driver/src/utils.js

This file was deleted.

39 changes: 34 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.