Skip to content

Commit

Permalink
fix: multiple appium session creation (#231)
Browse files Browse the repository at this point in the history
* fix: multiple appium session creation

* chore: added changeset

* fix: ios app restart
  • Loading branch information
pushpam5 authored Nov 25, 2024
1 parent 707bc97 commit 216e9f5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-meals-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"appwright": patch
---

fix: multiple appium session creation
13 changes: 11 additions & 2 deletions src/fixture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { Device } from "../device";
import { createDeviceProvider } from "../providers";
import { WorkerInfoStore } from "./workerInfo";
import { stopAppiumServer } from "../providers/appium";

type TestLevelFixtures = {
/**
Expand Down Expand Up @@ -36,10 +37,12 @@ export const test = base.extend<TestLevelFixtures, WorkerLevelFixtures>({
},
device: async ({ deviceProvider }, use, testInfo) => {
const device = await deviceProvider.getDevice();
const deviceProviderName = (
testInfo.project as FullProject<AppwrightConfig>
).use.device?.provider;
testInfo.annotations.push({
type: "providerName",
description: (testInfo.project as FullProject<AppwrightConfig>).use.device
?.provider,
description: deviceProviderName,
});
testInfo.annotations.push({
type: "sessionId",
Expand All @@ -48,6 +51,12 @@ export const test = base.extend<TestLevelFixtures, WorkerLevelFixtures>({
await deviceProvider.syncTestDetails?.({ name: testInfo.title });
await use(device);
await device.close();
if (
deviceProviderName === "emulator" ||
deviceProviderName === "local-device"
) {
await stopAppiumServer();
}
await deviceProvider.syncTestDetails?.({
name: testInfo.title,
status: testInfo.status,
Expand Down
18 changes: 17 additions & 1 deletion src/providers/appium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ export async function startAppiumServer(
const appiumProcess = spawn("npx", ["appium"], {
stdio: "pipe",
});

appiumProcess.stderr.on("data", async (data: Buffer) => {
console.log(data.toString());
});
appiumProcess.stdout.on("data", async (data: Buffer) => {
const output = data.toString();
console.log(output);

if (output.includes("Error: listen EADDRINUSE")) {
// TODO: Kill the appium server if it is already running
Expand Down Expand Up @@ -86,6 +89,19 @@ export async function startAppiumServer(
});
}

export function stopAppiumServer() {
return new Promise((resolve, reject) => {
exec(`pkill -f appium`, (error, stdout) => {
if (error) {
logger.error(`Error stopping Appium server: ${error.message}`);
reject(error);
}
logger.log("Appium server stopped successfully.");
resolve(stdout);
});
});
}

export function isEmulatorInstalled(platform: Platform): Promise<boolean> {
return new Promise((resolve) => {
if (platform == Platform.ANDROID) {
Expand Down
1 change: 1 addition & 0 deletions src/providers/emulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Follow the steps mentioned in ${androidSimulatorConfigDocLink} to run test on An
"appium:fullReset": true,
"appium:deviceOrientation": this.project.use.device?.orientation,
"appium:settings[snapshotMaxDepth]": 62,
"appium:wdaLaunchTimeout": 300_000,
},
};
}
Expand Down

0 comments on commit 216e9f5

Please sign in to comment.