Skip to content

Sample script changes for supporting playwright iOS #39

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

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ local.log
local.log
package-lock.json
local.log
.obs_test_details-*
/log
/test-results
92 changes: 69 additions & 23 deletions fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports.BS_LOCAL_ARGS = {
};

// Patching the capabilities dynamically according to the project name.
const patchMobileCaps = (name, title) => {
const patchAndroidCaps = (name, title) => {
let combination = name.split(/@browserstack/)[0];
let [browerCaps, osCaps] = combination.split(/:/);
let [browser, deviceName] = browerCaps.split(/@/);
Expand Down Expand Up @@ -60,6 +60,20 @@ const patchCaps = (name, title) => {
caps.name = title;
};

const patchIosCaps = (name, title) => {
let combination = name.split(/@browserstack/)[0];
let [browerCaps, osCaps] = combination.split(/:/);
let [browser, deviceName] = browerCaps.split(/@/);
let osCapsSplit = osCaps.split(/ /);
let os = osCapsSplit.shift();
let osVersion = osCapsSplit.join(" ");
caps.browser = browser ? browser : "safari";
caps.deviceName = deviceName ? deviceName : "Iphone 16";
caps.osVersion = osVersion ? osVersion : "18";
caps.name = title;
caps.realMobile = "true";
};

const isHash = (entity) =>
Boolean(entity && typeof entity === "object" && !Array.isArray(entity));
const nestedKeyValue = (hash, keys) =>
Expand All @@ -82,9 +96,10 @@ exports.test = base.test.extend({
page: async ({ page, playwright }, use, testInfo) => {
if (testInfo.project.name.match(/browserstack/)) {
let vBrowser, vContext, vDevice;
const isMobile = testInfo.project.name.match(/browserstack-mobile/);
if (isMobile) {
patchMobileCaps(
const isAndroid = testInfo.project.name.match(/browserstack-android/);
const isIOS = testInfo.project.name.match(/browserstack-ios/);
if (isAndroid) {
patchAndroidCaps(
testInfo.project.name,
`${testInfo.file} - ${testInfo.title}`
);
Expand All @@ -95,6 +110,18 @@ exports.test = base.test.extend({
);
await vDevice.shell("am force-stop com.android.chrome");
vContext = await vDevice.launchBrowser();
} else if(isIOS) {
patchIosCaps(
testInfo.project.name,
`${testInfo.file} - ${testInfo.title}`
);

vBrowser = await playwright.webkit.connect({
wsEndpoint:
`wss://cdp.browserstack.com/playwright?caps=` +
`${encodeURIComponent(JSON.stringify(caps))}`,
});
vContext = await vBrowser.newContext(testInfo.project.use);
} else {
patchCaps(testInfo.project.name, `${testInfo.title}`);
delete caps.osVersion;
Expand All @@ -112,7 +139,7 @@ exports.test = base.test.extend({

await vPage.close();

if (isMobile) {
if (isAndroid) {
await vDevice.close();
} else {
await vBrowser.close();
Expand All @@ -123,31 +150,50 @@ exports.test = base.test.extend({
},

beforeEach: [
async ({ page }, use) => {
await page
.context()
.tracing.start({ screenshots: true, snapshots: true, sources: true });
await use();
async ({ page }, use, testInfo) => {
const isIOS = testInfo.project.name.match(/browserstack-ios/);
if(isIOS) {
await page.context();
await use();
} else {
await page
.context()
.tracing.start({ screenshots: true, snapshots: true, sources: true });
await use();
}
},
{ auto: true },
],

afterEach: [
async ({ page }, use, testInfo) => {
const isIOS = testInfo.project.name.match(/browserstack-ios/);
await use();
if (testInfo.status == "failed") {
await page
.context()
.tracing.stop({ path: `${testInfo.outputDir}/trace.zip` });
await page.screenshot({ path: `${testInfo.outputDir}/screenshot.png` });
await testInfo.attach("screenshot", {
path: `${testInfo.outputDir}/screenshot.png`,
contentType: "image/png",
});
await testInfo.attach("trace", {
path: `${testInfo.outputDir}/trace.zip`,
contentType: "application/zip",
});

if(isIOS) {
if (testInfo.status == "failed") {
await page.context();
await page.screenshot({ path: `${testInfo.outputDir}/screenshot.png` });
await testInfo.attach("screenshot", {
path: `${testInfo.outputDir}/screenshot.png`,
contentType: "image/png",
});
}
} else {
if (testInfo.status == "failed") {
await page
.context()
.tracing.stop({ path: `${testInfo.outputDir}/trace.zip` });
await page.screenshot({ path: `${testInfo.outputDir}/screenshot.png` });
await testInfo.attach("screenshot", {
path: `${testInfo.outputDir}/screenshot.png`,
contentType: "image/png",
});
await testInfo.attach("trace", {
path: `${testInfo.outputDir}/trace.zip`,
contentType: "application/zip",
});
}
}
},
{ auto: true },
Expand Down
Loading