Skip to main content

Examples

Setup​

This page contains complete NodeJS example scripts for running Appium tests on different devices available on the TV Labs platform.

To run these examples, first setup your environment:

mkdir tvlabs-appium-examples && cd tvlabs-appium-examples
npm init -y
npm install --save webdriverio

echo "TVLABS_API_KEY=<tvlabs_api_key>" >> .env
echo "TVLABS_BUILD_ID=$(tvlabs upload -i <path to your application package here>)" >> .env

touch index.js

To run the script after you populate index.js with one of the below examples, run node --env-file=.env index.js.

Roku​

import { remote } from 'webdriverio';
import fs from 'fs';

const capabilities = {
'tvlabs:build': process.env.TVLABS_BUILD_ID,
'tvlabs:constraints': {
platform_key: 'roku',
},
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

async function runTest() {
const driver = await remote(wdOpts);

try {
await driver.executeScript('roku: deviceInfo', []);

await driver.executeScript('roku: pressKey', [{ key: 'Right' }]);

await driver.pause(1000);

await driver.executeScript('roku: pressKey', [{ key: 'Left' }]);

const screenshot = await driver.takeScreenshot();

fs.writeFileSync('./screenshot.png', screenshot, { encoding: 'base64' });
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

WebOS (LG)​

import { remote } from 'webdriverio';

const capabilities = {
'tvlabs:constraints': {
platform_key: 'web_os',
},
'tvlabs:build': process.env.TVLABS_BUILD_ID,
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

async function runTest() {
const driver = await remote(wdOpts);

try {
await driver.executeScript('webos: pressKey', [{ key: 'right' }]);

await driver.pause(1000);

await driver.executeScript('webos: pressKey', [{ key: 'left' }]);
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

Tizen TV (Samsung)​

import { remote } from 'webdriverio';

const capabilities = {
'tvlabs:constraints': {
platform_key: 'tizen',
},
'tvlabs:build': process.env.TVLABS_BUILD_ID,
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

async function runTest() {
const driver = await remote(wdOpts);

try {
await driver.executeScript('tizen: pressKey', [{ key: 'KEY_RIGHT' }]);

await driver.pause(1000);

await driver.executeScript('tizen: pressKey', [{ key: 'KEY_LEFT' }]);
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

tvOS (Apple TV)​

import { remote } from 'webdriverio';

const capabilities = {
'tvlabs:constraints': {
platform_key: 'tv_os',
},
'tvlabs:build': process.env.TVLABS_BUILD_ID,
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

async function runTest() {
const driver = await remote(wdOpts);

const pressKey = async (key) => {
await driver.executeScript('mobile: pressButton', [{ name: key }]);
await driver.pause(500);
};

try {
await pressKey('right');
await pressKey('left');
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

Smartcast (Vizio)​

note

You can find a reference of available commands and key mappings in the SmartCast WebDriver documentation.

import { remote } from 'webdriverio';

const capabilities = {
'tvlabs:constraints': {
platform_key: 'smartcast',
},
'tvlabs:build': process.env.TVLABS_BUILD_ID,
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

async function runTest() {
const driver = await remote(wdOpts);

try {
await driver.executeScript('smartcast: pressKey', [{ key: 'right' }]);

await driver.pause(1000);

await driver.executeScript('smartcast: pressKey', [{ key: 'left' }]);

await driver.pause(1000);

await driver.$('#my-element').getText();
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

Entertainment OS​

note

You can find a reference of available commands and key mappings in the Entertainment OS WebDriver documentation.

import { remote } from 'webdriverio';

const capabilities = {
'tvlabs:constraints': {
platform_key: 'entertainment_os',
},
'appium:appId': process.env.TVLABS_APP_ID,
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

async function runTest() {
const driver = await remote(wdOpts);

try {
await driver.executeScript('entertainment_os: pressKey', [{ key: 'down' }]);

await driver.pause(1000);

await driver.executeScript('entertainment_os: pressKey', [{ key: 'up' }]);

await driver.pause(1000);

await driver.$('#my-element').getText();
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

Android Mobile​

import { remote } from 'webdriverio';

const capabilities = {
'tvlabs:constraints': {
platform_key: 'android_mobile',
},
'tvlabs:build': process.env.TVLABS_BUILD_ID,
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
};

async function runTest() {
const driver = await remote(wdOpts);

try {
// Get device info
await driver.executeScript('mobile: deviceInfo', []);

await driver.pause(1000);

// Press the Right key
await driver.executeScript('mobile: pressKey', [{ keycode: 22 }]);

await driver.pause(1000);

// Press the Left key
await driver.executeScript('mobile: pressKey', [{ keycode: 21 }]);

await driver.pause(1000);
} finally {
// Always clean up
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();

Using @tvlabs/wdio-service​

Beginning with any of the examples above (Roku for this example), run the following command to install the WebdriverIO service:

npm install --save @tvlabs/wdio-service

Then modify the script to use the service:

import { remote } from 'webdriverio';
import TVLabsService from '@tvlabs/wdio-service';
import fs from 'fs';

const capabilities = {
'tvlabs:build': process.env.TVLABS_BUILD_ID,
'tvlabs:constraints': {
platform_key: 'roku',
},
};

const wdOpts = {
hostname: process.env.APPIUM_HOST || 'appium.tvlabs.ai',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
protocol: 'https',
logLevel: 'info',
capabilities,
headers: {
Authorization: `Bearer ${process.env.TVLABS_API_KEY}`,
},
connectionRetryTimeout: 60_000,
};

const serviceOpts = {
apiKey: process.env.TVLABS_API_KEY,
};

async function runTest() {
// Unused here, but usually provided by the WebdriverIO test runner.
const wdioOptions = {};
const specs = [];
const cid = '';

const service = new TVLabsService(serviceOpts, capabilities, wdioOptions);

// This modifies capabilities, adding the TV Labs session ID.
await service.beforeSession(wdOpts, capabilities, [], '');

// Proceed normally!
const driver = await remote(wdOpts);

try {
await driver.executeScript('roku: deviceInfo', []);

await driver.executeScript('roku: pressKey', [{ key: 'Right' }]);

await driver.pause(1000);

await driver.executeScript('roku: pressKey', [{ key: 'Left' }]);

const screenshot = await driver.takeScreenshot();

fs.writeFileSync('./screenshot.png', screenshot, { encoding: 'base64' });
} finally {
await driver.pause(1000);
await driver.deleteSession();
}
}

runTest();