Enhance CommandLine and TestConfiguration for improved option validation and logging
* Added validation for required command line options in the `CommandLine` class to ensure necessary parameters are provided. * Updated `TestConfiguration` to store and expose the logging level from command line options. * Refactored `BrowserstackApi` to improve method naming and added content type headers for API requests. * Changed `SupportedBrowser` type to be exported for better accessibility. * Updated `Page` and `SubscribingPage` classes to use a consistent browser import and improved constructor parameters.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import SupportedBrowser from './SupportedBrowser';
|
import type {SupportedBrowser} from './SupportedBrowser';
|
||||||
|
|
||||||
// Source: https://github.com/browserstack/api
|
// Source: https://github.com/browserstack/api
|
||||||
|
|
||||||
@@ -10,16 +10,18 @@ export class BrowserstackApi {
|
|||||||
this._authorizationHeader = `Basic ${Buffer.from(`${username}:${accessKey}`).toString('base64')}`;
|
this._authorizationHeader = `Basic ${Buffer.from(`${username}:${accessKey}`).toString('base64')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getSupportedBrowsers(): Promise<SupportedBrowser[]> {
|
public async getListOfSupportedBrowsers(): Promise<SupportedBrowser[]> {
|
||||||
const endpoint = `${this._baseUrl}/browsers?flat=true`;
|
const requestPath = `${this._baseUrl}/browsers?flat=true`;
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: this._authorizationHeader
|
Authorization: this._authorizationHeader,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Accept: 'application/json'
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await fetch(endpoint, {headers});
|
const response = await fetch(requestPath, {headers});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to fetch BrowserStack supported browsers due to [ ${response.statusText}]`);
|
throw new Error(`Failed to fetch BrowserStack supported browsers due to [${response.statusText}]`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json() as Promise<SupportedBrowser[]>;
|
return response.json() as Promise<SupportedBrowser[]>;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
type SupportedBrowser = {
|
export type SupportedBrowser = {
|
||||||
os: string;
|
os: string;
|
||||||
os_version: string;
|
os_version: string;
|
||||||
browser: string;
|
browser: string;
|
||||||
@@ -6,5 +6,3 @@ type SupportedBrowser = {
|
|||||||
browser_version: string | null;
|
browser_version: string | null;
|
||||||
real_mobile: boolean;
|
real_mobile: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SupportedBrowser;
|
|
||||||
|
|||||||
@@ -37,6 +37,11 @@ export default class CommandLine {
|
|||||||
// Convert the string log level back to LoggingLevel enum
|
// Convert the string log level back to LoggingLevel enum
|
||||||
const logLevel = rawOptions.logLevel ? LoggingLevelMapping.convertLoggingLevelTypeToLoggingLevel(rawOptions.logLevel) : defaultLogLevel;
|
const logLevel = rawOptions.logLevel ? LoggingLevelMapping.convertLoggingLevelTypeToLoggingLevel(rawOptions.logLevel) : defaultLogLevel;
|
||||||
|
|
||||||
|
// Validate required options are present
|
||||||
|
if (!rawOptions.applicationId || !rawOptions.secret || !rawOptions.pcastUri || !rawOptions.channelUri) {
|
||||||
|
throw new Error('Missing required command line options: applicationId, secret, pcastUri, and channelUri are required');
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...rawOptions,
|
...rawOptions,
|
||||||
logLevel
|
logLevel
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type {CommandLineOptions} from './CommandLine';
|
import type {CommandLineOptions} from './CommandLine';
|
||||||
|
import {LoggingLevel} from '../logger/LoggingLevel';
|
||||||
|
|
||||||
type ApplicationCredentials = {
|
type ApplicationCredentials = {
|
||||||
applicationId: string;
|
applicationId: string;
|
||||||
@@ -26,6 +27,7 @@ export default class TestConfiguration {
|
|||||||
private readonly _publishers: string[];
|
private readonly _publishers: string[];
|
||||||
private readonly _tests: string[];
|
private readonly _tests: string[];
|
||||||
private readonly _browserstack: BrowserstackConfiguration;
|
private readonly _browserstack: BrowserstackConfiguration;
|
||||||
|
private readonly _logLevel: LoggingLevel;
|
||||||
|
|
||||||
constructor(commandLineOptions: CommandLineOptions) {
|
constructor(commandLineOptions: CommandLineOptions) {
|
||||||
this._applicationCredentials = {
|
this._applicationCredentials = {
|
||||||
@@ -43,6 +45,7 @@ export default class TestConfiguration {
|
|||||||
this._viewers = commandLineOptions.viewers;
|
this._viewers = commandLineOptions.viewers;
|
||||||
this._publishers = commandLineOptions.publishers;
|
this._publishers = commandLineOptions.publishers;
|
||||||
this._tests = commandLineOptions.tests;
|
this._tests = commandLineOptions.tests;
|
||||||
|
this._logLevel = commandLineOptions.logLevel;
|
||||||
this._browserstack = {
|
this._browserstack = {
|
||||||
enabled: commandLineOptions.useBrowserstack,
|
enabled: commandLineOptions.useBrowserstack,
|
||||||
local: commandLineOptions.useBrowserstackLocal,
|
local: commandLineOptions.useBrowserstackLocal,
|
||||||
@@ -74,4 +77,8 @@ export default class TestConfiguration {
|
|||||||
get browserstack(): BrowserstackConfiguration {
|
get browserstack(): BrowserstackConfiguration {
|
||||||
return this._browserstack;
|
return this._browserstack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get logLevel(): LoggingLevel {
|
||||||
|
return this._logLevel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,36 +1,33 @@
|
|||||||
import {browser} from '@wdio/globals';
|
import {browser as wdio} from '@wdio/globals';
|
||||||
|
|
||||||
export type PageOptions = {
|
export type PageOptions = {
|
||||||
browser?: typeof browser; // MultiRemote usecase
|
browser?: typeof wdio; // MultiRemote usecase
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PageOpenOptions = {
|
export type PageOpenOptions = {
|
||||||
queryParameters?: Record<string, string | number>;
|
queryParameters?: Record<string, string | number>;
|
||||||
isNewTabRequest?: boolean;
|
isNewTabRequest?: boolean;
|
||||||
endpoint?: string;
|
|
||||||
requestPath?: string;
|
requestPath?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class Page {
|
export default class Page {
|
||||||
private readonly _baseUrl: string;
|
private readonly _baseUrl: string;
|
||||||
|
private readonly _browser: typeof wdio;
|
||||||
|
|
||||||
constructor(baseUrl: string) {
|
constructor(baseUrl: string, browser: typeof wdio) {
|
||||||
this._baseUrl = baseUrl;
|
this._baseUrl = baseUrl;
|
||||||
|
this._browser = browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async open(options: PageOpenOptions = {}): Promise<void> {
|
public async open(options: PageOpenOptions = {}): Promise<void> {
|
||||||
const {queryParameters, isNewTabRequest, endpoint, requestPath} = options;
|
const {queryParameters, isNewTabRequest, requestPath} = options;
|
||||||
|
|
||||||
// Build the URL path properly
|
|
||||||
let pageUrl = this._baseUrl;
|
let pageUrl = this._baseUrl;
|
||||||
if (endpoint) {
|
|
||||||
pageUrl += `/${endpoint}`;
|
|
||||||
}
|
|
||||||
if (requestPath) {
|
if (requestPath) {
|
||||||
pageUrl += `/${requestPath}`;
|
pageUrl += `/${requestPath}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add query parameters if they exist
|
|
||||||
if (queryParameters && Object.keys(queryParameters).length > 0) {
|
if (queryParameters && Object.keys(queryParameters).length > 0) {
|
||||||
const queryString = Object.entries(queryParameters)
|
const queryString = Object.entries(queryParameters)
|
||||||
.map(([queryParameterName, queryParameterValue]) => `${queryParameterName}=${queryParameterValue}`)
|
.map(([queryParameterName, queryParameterValue]) => `${queryParameterName}=${queryParameterValue}`)
|
||||||
@@ -39,9 +36,9 @@ export default class Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isNewTabRequest) {
|
if (isNewTabRequest) {
|
||||||
await (browser as any).newWindow(pageUrl);
|
await (this._browser as any).newWindow(pageUrl);
|
||||||
} else {
|
} else {
|
||||||
await (browser as any).url(pageUrl);
|
await (this._browser as any).url(pageUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import {$} from '@wdio/globals';
|
import {browser as wdio, $} from '@wdio/globals';
|
||||||
import Page, {PageOpenOptions} from './Page';
|
import Page, {PageOpenOptions} from './Page';
|
||||||
|
|
||||||
export class SubscribingPage extends Page {
|
export class SubscribingPage extends Page {
|
||||||
constructor(baseUri: string) {
|
constructor(baseUri: string, browser: typeof wdio) {
|
||||||
super(baseUri);
|
super(baseUri, browser);
|
||||||
}
|
}
|
||||||
|
|
||||||
get videoElement() {
|
get videoElement() {
|
||||||
|
|||||||
Reference in New Issue
Block a user