Skip to content

Commit feb301d

Browse files
committed
Refactoring ++
1 parent 9a6df70 commit feb301d

File tree

9 files changed

+152
-111
lines changed

9 files changed

+152
-111
lines changed

src/tools/appautomate-utils/appium-sdk/config-generator.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
// Configuration utilities for BrowserStack App SDK
2-
import { APP_DEVICE_CONFIGS, DEFAULT_APP_PATH, createStep } from "./index.js";
2+
import {
3+
APP_DEVICE_CONFIGS,
4+
AppSDKSupportedTestingFrameworkEnum,
5+
DEFAULT_APP_PATH,
6+
createStep,
7+
} from "./index.js";
38

49
export function generateAppBrowserStackYMLInstructions(
510
platforms: string[],
611
username: string,
712
accessKey: string,
813
appPath: string = DEFAULT_APP_PATH,
9-
testingFramework?: string,
14+
testingFramework: string,
1015
): string {
16+
if (
17+
testingFramework === AppSDKSupportedTestingFrameworkEnum.nightwatch ||
18+
testingFramework === AppSDKSupportedTestingFrameworkEnum.webdriverio
19+
) {
20+
return "";
21+
}
22+
23+
// Generate platform and device configurations
1124
const platformConfigs = platforms
1225
.map((platform) => {
1326
const devices =
@@ -25,10 +38,10 @@ export function generateAppBrowserStackYMLInstructions(
2538
.filter(Boolean)
2639
.join("\n");
2740

41+
// Construct YAML content
2842
const configContent = `\`\`\`yaml
2943
userName: ${username}
3044
accessKey: ${accessKey}
31-
framework: ${testingFramework}
3245
app: ${appPath}
3346
platforms:
3447
${platformConfigs}
@@ -49,6 +62,7 @@ accessibility: false
4962
- Set \`browserstackLocal: true\` if you need to test with local/staging servers
5063
- Adjust \`parallelsPerPlatform\` based on your subscription limits`;
5164

65+
// Return formatted step for instructions
5266
return createStep(
5367
"Update browserstack.yml file with App Automate configuration:",
5468
`Create or update the browserstack.yml file in your project root with the following content:

src/tools/appautomate-utils/appium-sdk/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ export const SETUP_APP_AUTOMATE_SCHEMA = {
6060
.describe(
6161
"Path to the mobile app file (.apk for Android, .ipa for iOS). Can be a local file path or a BrowserStack app URL (bs://)",
6262
),
63+
project: z
64+
.string()
65+
.optional()
66+
.default("BStack-AppAutomate-Suite")
67+
.describe("Project name for organizing test runs on BrowserStack."),
6368
};

src/tools/appautomate-utils/handler.ts renamed to src/tools/appautomate-utils/appium-sdk/handler.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { z } from "zod";
22
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3-
import { BrowserStackConfig } from "../../lib/types.js";
4-
import { getBrowserStackAuth } from "../../lib/get-auth.js";
3+
import { BrowserStackConfig } from "../../../lib/types.js";
4+
import { getBrowserStackAuth } from "../../../lib/get-auth.js";
55
import {
66
AppSDKSupportedLanguage,
77
AppSDKSupportedTestingFramework,
88
AppSDKInstruction,
99
formatAppInstructionsWithNumbers,
1010
getAppInstructionsForProjectConfiguration,
1111
SETUP_APP_AUTOMATE_SCHEMA,
12-
} from "./appium-sdk/index.js";
12+
} from "./index.js";
1313
import {
1414
getAppSDKPrefixCommand,
1515
generateAppBrowserStackYMLInstructions,
16-
} from "./appium-sdk/index.js";
16+
} from "./index.js";
17+
import { getAppUploadInstruction } from "./utils.js";
18+
import logger from "../../../logger.js";
1719

1820
export async function setupAppAutomateHandler(
1921
rawInput: unknown,
@@ -25,13 +27,24 @@ export async function setupAppAutomateHandler(
2527

2628
const instructions: AppSDKInstruction[] = [];
2729

30+
// Use variables for all major input properties
31+
const testingFramework =
32+
input.detectedTestingFramework as AppSDKSupportedTestingFramework;
33+
const language = input.detectedLanguage as AppSDKSupportedLanguage;
34+
const platforms = (input.desiredPlatforms as string[]) ?? ["android"];
35+
const appPath = input.appPath as string;
36+
const framework = input.detectedFramework as string;
37+
38+
logger.info("Generating SDK setup command...");
39+
logger.debug(`Input: ${JSON.stringify(input)}`);
40+
2841
// Step 1: Generate SDK setup command
2942
const sdkCommand = getAppSDKPrefixCommand(
30-
input.detectedLanguage as AppSDKSupportedLanguage,
31-
input.detectedFramework as string,
43+
language,
44+
testingFramework,
3245
username,
3346
accessKey,
34-
input.appPath as string | undefined,
47+
appPath,
3548
);
3649

3750
if (sdkCommand) {
@@ -40,22 +53,34 @@ export async function setupAppAutomateHandler(
4053

4154
// Step 2: Generate browserstack.yml configuration
4255
const configInstructions = generateAppBrowserStackYMLInstructions(
43-
(input.desiredPlatforms as string[]) ?? ["android"],
56+
platforms,
4457
username,
4558
accessKey,
46-
input.appPath as string | undefined,
47-
input.detectedTestingFramework as AppSDKSupportedTestingFramework,
59+
appPath,
60+
testingFramework,
4861
);
4962

5063
if (configInstructions) {
5164
instructions.push({ content: configInstructions, type: "config" });
5265
}
5366

54-
// Step 3: Generate project configuration and run instructions
67+
// Step 3: Generate app upload instruction
68+
const appUploadInstruction = await getAppUploadInstruction(
69+
appPath,
70+
username,
71+
accessKey,
72+
testingFramework,
73+
);
74+
75+
if (appUploadInstruction) {
76+
instructions.push({ content: appUploadInstruction, type: "setup" });
77+
}
78+
79+
// Step 4: Generate project configuration and run instructions
5580
const projectInstructions = getAppInstructionsForProjectConfiguration(
56-
input.detectedFramework as string,
57-
input.detectedTestingFramework as AppSDKSupportedTestingFramework,
58-
input.detectedLanguage as AppSDKSupportedLanguage,
81+
framework,
82+
testingFramework,
83+
language,
5984
);
6085

6186
if (projectInstructions) {

src/tools/appautomate-utils/appium-sdk/instructions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function getAppInstructionsForProjectConfiguration(
4444

4545
export function getAppSDKPrefixCommand(
4646
language: AppSDKSupportedLanguage,
47-
framework: string,
47+
testingFramework: string,
4848
username: string,
4949
accessKey: string,
5050
appPath?: string,
@@ -53,13 +53,13 @@ export function getAppSDKPrefixCommand(
5353
case "csharp":
5454
return getCSharpSDKCommand(username, accessKey);
5555
case "java":
56-
return getJavaSDKCommand(framework, username, accessKey, appPath);
56+
return getJavaSDKCommand(testingFramework, username, accessKey, appPath);
5757
case "nodejs":
58-
return getNodejsSDKCommand(framework, username, accessKey);
58+
return getNodejsSDKCommand(testingFramework, username, accessKey);
5959
case "python":
60-
return getPythonSDKCommand(framework, username, accessKey);
60+
return getPythonSDKCommand(testingFramework, username, accessKey);
6161
case "ruby":
62-
return getRubySDKCommand(framework, username, accessKey);
62+
return getRubySDKCommand(testingFramework, username, accessKey);
6363
default:
6464
return "";
6565
}

src/tools/appautomate-utils/appium-sdk/languages/java.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Java instructions and commands for App SDK utilities
2-
import logger from "../../../../logger.js";
32
import {
43
createStep,
54
combineInstructions,
@@ -68,19 +67,22 @@ function getMavenCommandForWindows(
6867
);
6968
}
7069

71-
// Generates Maven archetype command for Unix-like platforms (macOS/Linux)
7270
function getMavenCommandForUnix(
73-
username: string,
74-
accessKey: string,
75-
mavenFramework: string,
7671
framework: string,
72+
mavenFramework: string,
7773
): string {
78-
return `mvn archetype:generate -B -DarchetypeGroupId=${MAVEN_ARCHETYPE_GROUP_ID} \\
79-
-DarchetypeArtifactId=${mavenFramework} -DarchetypeVersion=${MAVEN_ARCHETYPE_VERSION} \\
80-
-DgroupId=${MAVEN_ARCHETYPE_GROUP_ID} -DartifactId=${MAVEN_ARCHETYPE_ARTIFACT_ID} -Dversion=${MAVEN_ARCHETYPE_VERSION} \\
81-
-DBROWSERSTACK_USERNAME="${username}" \\
82-
-DBROWSERSTACK_ACCESS_KEY="${accessKey}" \\
83-
-DBROWSERSTACK_FRAMEWORK="${framework}"`;
74+
return (
75+
`mvn archetype:generate -B ` +
76+
`-DarchetypeGroupId="${MAVEN_ARCHETYPE_GROUP_ID}" ` +
77+
`-DarchetypeArtifactId="${mavenFramework}" ` +
78+
`-DarchetypeVersion="${MAVEN_ARCHETYPE_VERSION}" ` +
79+
`-DgroupId="${MAVEN_ARCHETYPE_GROUP_ID}" ` +
80+
`-DartifactId="${MAVEN_ARCHETYPE_ARTIFACT_ID}" ` +
81+
`-Dversion="${MAVEN_ARCHETYPE_VERSION}" ` +
82+
`-DBROWSERSTACK_USERNAME="${process.env.BROWSERSTACK_USERNAME}" ` +
83+
`-DBROWSERSTACK_ACCESS_KEY="${process.env.BROWSERSTACK_ACCESS_KEY}" ` +
84+
`-DBROWSERSTACK_FRAMEWORK="${framework}"`
85+
);
8486
}
8587

8688
export function getJavaSDKCommand(
@@ -89,33 +91,21 @@ export function getJavaSDKCommand(
8991
accessKey: string,
9092
appPath?: string,
9193
): string {
92-
logger.info("Generating Java SDK command");
93-
const { isWindows = false, getPlatformLabel = () => "Unknown" } =
94-
PLATFORM_UTILS || {};
95-
if (!PLATFORM_UTILS) {
96-
console.warn("PLATFORM_UTILS is undefined. Defaulting platform values.");
97-
}
94+
const { isWindows = false, getPlatformLabel } = PLATFORM_UTILS || {};
9895

9996
const mavenFramework = getJavaAppFrameworkForMaven(framework);
10097

10198
let mavenCommand: string;
102-
logger.info(
103-
`Maven command for ${framework} (${getPlatformLabel()}): ${isWindows}`,
104-
);
99+
105100
if (isWindows) {
106101
mavenCommand = getMavenCommandForWindows(framework, mavenFramework);
107102
if (appPath) {
108103
mavenCommand += ` -DBROWSERSTACK_APP="${appPath}"`;
109104
}
110105
} else {
111-
mavenCommand = getMavenCommandForUnix(
112-
username,
113-
accessKey,
114-
mavenFramework,
115-
framework,
116-
);
106+
mavenCommand = getMavenCommandForUnix(framework, mavenFramework);
117107
if (appPath) {
118-
mavenCommand += ` \\\n-DBROWSERSTACK_APP="${appPath}"`;
108+
mavenCommand += ` -DBROWSERSTACK_APP="${appPath}"`;
119109
}
120110
}
121111

@@ -128,7 +118,7 @@ export function getJavaSDKCommand(
128118

129119
const mavenStep = createStep(
130120
"Install BrowserStack SDK using Maven Archetype for App Automate",
131-
`**Maven command for ${framework} (${getPlatformLabel()}):**
121+
`Maven command for ${framework} (${getPlatformLabel()}):
132122
\`\`\`bash
133123
${mavenCommand}
134124
\`\`\`
@@ -137,6 +127,5 @@ Alternative setup for Gradle users:
137127
${GRADLE_APP_SETUP_INSTRUCTIONS}`,
138128
);
139129

140-
logger.info("Java SDK command generated successfully");
141130
return combineInstructions(envStep, mavenStep);
142131
}

0 commit comments

Comments
 (0)