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:
2025-08-18 22:07:37 -04:00
parent 1cf223fd85
commit e93eb5fe84
6 changed files with 35 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
import SupportedBrowser from './SupportedBrowser';
import type {SupportedBrowser} from './SupportedBrowser';
// Source: https://github.com/browserstack/api
@@ -10,16 +10,18 @@ export class BrowserstackApi {
this._authorizationHeader = `Basic ${Buffer.from(`${username}:${accessKey}`).toString('base64')}`;
}
public async getSupportedBrowsers(): Promise<SupportedBrowser[]> {
const endpoint = `${this._baseUrl}/browsers?flat=true`;
public async getListOfSupportedBrowsers(): Promise<SupportedBrowser[]> {
const requestPath = `${this._baseUrl}/browsers?flat=true`;
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) {
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[]>;

View File

@@ -1,4 +1,4 @@
type SupportedBrowser = {
export type SupportedBrowser = {
os: string;
os_version: string;
browser: string;
@@ -6,5 +6,3 @@ type SupportedBrowser = {
browser_version: string | null;
real_mobile: boolean;
};
export default SupportedBrowser;

View File

@@ -37,6 +37,11 @@ export default class CommandLine {
// Convert the string log level back to LoggingLevel enum
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 {
...rawOptions,
logLevel

View File

@@ -1,4 +1,5 @@
import type {CommandLineOptions} from './CommandLine';
import {LoggingLevel} from '../logger/LoggingLevel';
type ApplicationCredentials = {
applicationId: string;
@@ -26,6 +27,7 @@ export default class TestConfiguration {
private readonly _publishers: string[];
private readonly _tests: string[];
private readonly _browserstack: BrowserstackConfiguration;
private readonly _logLevel: LoggingLevel;
constructor(commandLineOptions: CommandLineOptions) {
this._applicationCredentials = {
@@ -43,6 +45,7 @@ export default class TestConfiguration {
this._viewers = commandLineOptions.viewers;
this._publishers = commandLineOptions.publishers;
this._tests = commandLineOptions.tests;
this._logLevel = commandLineOptions.logLevel;
this._browserstack = {
enabled: commandLineOptions.useBrowserstack,
local: commandLineOptions.useBrowserstackLocal,
@@ -74,4 +77,8 @@ export default class TestConfiguration {
get browserstack(): BrowserstackConfiguration {
return this._browserstack;
}
}
get logLevel(): LoggingLevel {
return this._logLevel;
}
}