Skip to content

Commit

Permalink
Merge pull request #115 from LambdaTest/develop
Browse files Browse the repository at this point in the history
Added android test cases
  • Loading branch information
ankur-lt authored Dec 19, 2023
2 parents 6686291 + 1a30230 commit 7ac1770
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ jobs:
- name: Execute Playwright Test JS
run: npm run test

- name: Execute Playwright Test JS on Android
run: npm run testAndroid

playwright-test-ts:
runs-on: ubuntu-latest
defaults:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ dist

# IDE files
.idea/
.vscode

**/target/
**/.DS_Store
Expand Down
51 changes: 40 additions & 11 deletions playwright-test-js/lambdatest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
const base = require('@playwright/test')
const path = require('path')
const { chromium } = require('playwright')
const { chromium, _android } = require('playwright')
const cp = require('child_process');
const playwrightClientVersion = cp.execSync('npx playwright --version').toString().trim().split(' ')[1];

Expand All @@ -31,11 +31,27 @@ const capabilities = {
// Patching the capabilities dynamically according to the project name.
const modifyCapabilities = (configName, testName) => {
let config = configName.split('@lambdatest')[0]
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

// 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

delete capabilities.browserName;
delete capabilities.browserVersion;
} 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
}
}

exports.test = base.test.extend({
Expand All @@ -44,12 +60,22 @@ exports.test = base.test.extend({
let fileName = testInfo.file.split(path.sep).pop()
if (testInfo.project.name.match(/lambdatest/)) {
modifyCapabilities(testInfo.project.name, `${testInfo.title} - ${fileName}`)
let device, context, browser, ltPage;

const browser = await chromium.connect({
wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`
})
// 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();
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)
}

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

const testStatus = {
Expand All @@ -61,8 +87,11 @@ exports.test = base.test.extend({
}
await ltPage.evaluate(() => {},
`lambdatest_action: ${JSON.stringify(testStatus)}`)

await ltPage.close()
await browser.close()
await context?.close();
await browser?.close()
await device?.close();
} else {
// Run tests in local in case of local config provided
await use(page)
Expand Down
3 changes: 2 additions & 1 deletion playwright-test-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"description": "Sample project to run playwright tests using playwright runner on LambdaTest platform",
"scripts": {
"test": "npx playwright test --config=./playwright.config.js"
"test": "npx playwright test --config=./playwright.config.js",
"testAndroid": "npx playwright test --config=./playwrightAndroid.config.js"
},
"devDependencies": {
"@playwright/test": "^1.35.0",
Expand Down
5 changes: 3 additions & 2 deletions playwright-test-js/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { devices } = require('@playwright/test')
const config = {
testDir: 'tests',
testMatch: '**/*.spec.js',
timeout: 600000,
timeout: 60000,
workers: 4,
use: {},
projects: [
// -- LambdaTest Config --
Expand All @@ -24,7 +25,7 @@ const config = {
}
},
{
name: 'MicrosoftEdge:latest:MacOS Ventura@lambdatest',
name: 'MicrosoftEdge:109:MacOS Ventura@lambdatest',
use: {
...devices['iPhone 12 Pro Max']
}
Expand Down
56 changes: 56 additions & 0 deletions playwright-test-js/playwrightAndroid.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { devices } = require('@playwright/test')

// Playwright config to run tests on LambdaTest platform and local
const config = {
testDir: 'tests',
testMatch: '**/*.spec.js',
timeout: 60000,
workers: 4,
use: {},
projects: [
// -- LambdaTest Config --
// name in the format: deviceName:platformVersion:platformName@lambdatest
// Use additional configuration options provided by Playwright if required: https://playwright.dev/docs/api/class-testconfig
{
name: 'Pixel 5:12:android@lambdatest',
use: {}
},
{
// Regex device name
name: 'Galaxy*:12:android@lambdatest',
use: {}
},

// Config for running tests in local
// {
// name: "chrome",
// use: {
// browserName: "chromium",
// channel: "chrome",
// },
// },
// {
// name: "safari",
// use: {
// browserName: "webkit",
// viewport: { width: 1200, height: 750 },
// },
// },
// {
// name: "firefox",
// use: {
// browserName: "firefox",
// viewport: { width: 800, height: 600 },
// },
// },
// // Test in mobile viewport.
// {
// name: "chrome@pixel5",
// use: {
// ...devices['iPhone 12 Pro Max'],
// }
// },
]
}

module.exports = config
6 changes: 3 additions & 3 deletions playwright-test-js/tests/multipleBrowserContexts.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { test } = require('../lambdatest-setup')
const { expect } = require('@playwright/test')

test.describe('Browse LambdaTest in different browser contexts', () => {
test.describe.skip('Browse LambdaTest in different browser contexts', () => {
test('Search LambdaTest & LambdaTest Blog on DuckDuckGo', async ({ page }, testInfo) => {
await page.goto('https://duckduckgo.com')
let element = await page.locator("[name=\"q\"]");
Expand All @@ -10,8 +10,8 @@ test.describe('Browse LambdaTest in different browser contexts', () => {
await element.press("Enter");
const title = await page.title()

// Create new browserContext
const newPage = await page.context().browser().newPage(testInfo.project.use)
// Create new browserContext in the same window
const newPage = await page.context().newPage(testInfo.project.use)
await newPage.goto('https://duckduckgo.com')
const searchElement = await newPage.locator("[name=\"q\"]");
await searchElement.click();
Expand Down

0 comments on commit 7ac1770

Please sign in to comment.