Skip to content

Commit

Permalink
added changes for merge test js
Browse files Browse the repository at this point in the history
  • Loading branch information
HRanjan-11 committed Aug 12, 2024
1 parent ca01428 commit 8ddd8f9
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 140 deletions.
39 changes: 0 additions & 39 deletions playwright-test-js-merge-test/lambdatest-desktop-setup.js

This file was deleted.

46 changes: 0 additions & 46 deletions playwright-test-js-merge-test/lambdatest-mobile-setup.js

This file was deleted.

100 changes: 100 additions & 0 deletions playwright-test-js-merge-test/lambdatest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Add the file in your test suite to run tests on LambdaTest.
* Import `test` object from this file in the tests.
*/
const path = require('path')
const { chromium, _android } = require('playwright')
const cp = require('child_process');
const playwrightClientVersion = cp.execSync('npx playwright --version').toString().trim().split(' ')[1];

const capabilities = {
'browserName': 'Chrome', // Browsers allowed: `Chrome`, `MicrosoftEdge`, `pw-chromium`, `pw-firefox` and `pw-webkit`
'browserVersion': 'latest',
'LT:Options': {
'platform': 'Windows 10',
'build': 'Playwright JS Build',
'name': 'Playwright Test',
'user': process.env.LT_USERNAME,
'accessKey': process.env.LT_ACCESS_KEY,
'network': true,
'video': true,
'console': true,
'tunnel': false, // Add tunnel configuration if testing locally hosted webpage
'tunnelName': '', // Optional
'geoLocation': '', // country code can be fetched from https://www.lambdatest.com/capabilities-generator/
'playwrightClientVersion': playwrightClientVersion
}
}
// Patching the capabilities dynamically according to the project name.
const modifyCapabilities = (configName, testName) => {
let config = configName.split('@lambdatest')[0]

// Check if its an android test or a desktop test
if (configName.match(/android/)) {
let [deviceName, platformVersion, platform] = config.split(':')
capabilities['LT:Options']['deviceName'] = deviceName
capabilities['LT:Options']['platformVersion'] = platformVersion
capabilities['LT:Options']['platformName'] = platform
capabilities['LT:Options']['name'] = testName
capabilities['LT:Options']['build'] = 'Playwright JS Android Build'
capabilities['LT:Options']['isRealMobile'] = true

} else {
// Desktop test
let [browserName, browserVersion, platform] = config.split(':')
capabilities.browserName = browserName ? browserName : capabilities.browserName
capabilities.browserVersion = browserVersion ? browserVersion : capabilities.browserVersion
capabilities['LT:Options']['platform'] = platform ? platform : capabilities['LT:Options']['platform']
capabilities['LT:Options']['name'] = testName
}
}
async function setupBrowser(testInfo) {

let fileName = testInfo.file.split(path.sep).pop()

let device, context, browser, ltPage;
if (testInfo.project.name.match(/lambdatest/)) {
modifyCapabilities(testInfo.project.name, `${testInfo.title} - ${fileName}`)


// Check if its a desktop or an android test
if (testInfo.project.name.match(/android/)) {
// Android test
device = await _android.connect(`wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`);

await device.shell("am force-stop com.android.chrome");
context = await device.launchBrowser();
context.setDefaultTimeout(120000)
ltPage = await context.newPage(testInfo.project.use);

} else {
// Desktop test
browser = await chromium.connect(`wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`)

ltPage = await browser.newPage(testInfo.project.use)
}


}

return {ltPage,device,context,browser};
}

async function tearDown(ltPage,context,browser,device,testInfo) {
const testStatus = {
action: 'setTestStatus',
arguments: {
status: testInfo.status,
remark: testInfo.error?.stack || testInfo.error?.message,
}
}
await ltPage.evaluate(() => {},
`lambdatest_action: ${JSON.stringify(testStatus)}`)

await ltPage.close();
await context?.close();
await browser?.close()
await device?.close();
}

module.exports = { setupBrowser,tearDown };
6 changes: 6 additions & 0 deletions playwright-test-js-merge-test/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const config = {
use: {
viewport: { width: 1280, height: 720 }
}
},
{
name: 'chrome:latest:MacOs Ventura@lambdatest',
use: {
viewport: { width: 1280, height: 720 }
}
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions playwright-test-js-merge-test/playwrightAndroid.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const config = {
{
name: '.*:.*:android@lambdatest',
use: {}
},
{
name: 'Pixel 6:.*:android@lambdatest',
use: {}
}
]
}
Expand Down
40 changes: 12 additions & 28 deletions playwright-test-js-merge-test/tests/MobileGroup.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { test, expect } = require('@playwright/test');
const { setupBrowser } = require('../lambdatest-mobile-setup')
const { chromium, _android } = require('playwright');
const { setupBrowser,tearDown } = require('../lambdatest-setup')

// test.describe.configure({ mode: 'parallel' });

Expand All @@ -9,36 +8,22 @@ test.describe('Group 1', () => {
let context;
let page;
let device;
let testInfoGlobal;

test.beforeAll(async () => {
test.beforeAll(async ({}, testInfo) => {
const {ltPage,ltDevice,ltContext,ltBrowser} = await setupBrowser(testInfo);
page = ltPage;
device = ltDevice;
context = ltContext;
browser = ltBrowser;
});

page = await setupBrowser();
});

test.afterAll(async () => {

if (testInfoGlobal) {
const testStatus = {
action: 'setTestStatus',
arguments: {
status: testInfoGlobal.status,
remark: testInfoGlobal.error?.stack || testInfoGlobal.error?.message,
}
}
await page.evaluate(() => {},
`lambdatest_action: ${JSON.stringify(testStatus)}`)
}
test.afterAll(async ({}, testInfo) => {

await page?.close();
await context?.close();
await browser?.close();
await device?.close();
await tearDown(page,context,browser,device,testInfo);
});


test('test 1 Search LambdaTest on DuckDuckGo', async ({},testInfo) => {
testInfoGlobal = testInfo;
test('test 1 Search LambdaTest on DuckDuckGo', async () => {
await page.goto('https://duckduckgo.com')
let element = await page.locator("[name=\"q\"]");
await element.click();
Expand All @@ -52,8 +37,7 @@ test.describe('Group 1', () => {
expect(title).toEqual(expect.stringContaining('LambdaTest'))
})

test('test 2 Search LambdaTest on DuckDuckGo', async ({},testInfo) => {
testInfoGlobal = testInfo;
test('test 2 Search LambdaTest on DuckDuckGo', async () => {
await page.goto('https://duckduckgo.com')
let element = await page.locator("[name=\"q\"]");
await element.click();
Expand Down
39 changes: 12 additions & 27 deletions playwright-test-js-merge-test/tests/desktopGroup.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { test, expect } = require('@playwright/test');
const { chromium } = require('playwright');
const { setupBrowser } = require('../lambdatest-desktop-setup')
const { setupBrowser,tearDown } = require('../lambdatest-setup')

// test.describe.configure({ mode: 'parallel' });

Expand All @@ -9,35 +8,22 @@ test.describe('Group 1', () => {
let context;
let page;
let device;
let testInfoGlobal;

test.beforeAll(async () => {

page = await setupBrowser();
});
test.beforeAll(async ({}, testInfo) => {
const {ltPage,ltDevice,ltContext,ltBrowser} = await setupBrowser(testInfo);
page = ltPage;
device = ltDevice;
context = ltContext;
browser = ltBrowser;
});

test.afterAll(async () => {
if (testInfoGlobal) {
const testStatus = {
action: 'setTestStatus',
arguments: {
status: testInfoGlobal.status,
remark: testInfoGlobal.error?.stack || testInfoGlobal.error?.message,
}
}
await page.evaluate(() => {},
`lambdatest_action: ${JSON.stringify(testStatus)}`)
}
test.afterAll(async ({}, testInfo) => {

await page?.close();
await context?.close();
await browser?.close();
await device?.close();
await tearDown(page,context,browser,device,testInfo);
});


test('test 1 Search LambdaTest on DuckDuckGo', async ({},testInfo) => {
testInfoGlobal = testInfo;
test('test 1 Search LambdaTest on DuckDuckGo', async () => {
await page.goto('https://duckduckgo.com')
let element = await page.locator("[name=\"q\"]");
await element.click();
Expand All @@ -51,8 +37,7 @@ test.describe('Group 1', () => {
expect(title).toEqual(expect.stringContaining('LambdaTest'))
})

test('test 2 Search LambdaTest on DuckDuckGo', async ({},testInfo) => {
testInfoGlobal = testInfo;
test('test 2 Search LambdaTest on DuckDuckGo', async () => {
await page.goto('https://duckduckgo.com')
let element = await page.locator("[name=\"q\"]");
await element.click();
Expand Down

0 comments on commit 8ddd8f9

Please sign in to comment.