Skip to content
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

export platform specific objects through proxy #374

Merged
merged 19 commits into from
Jan 26, 2018
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
17 changes: 14 additions & 3 deletions detox/local-cli/detox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const cp = require('child_process');
program
.option('-o, --runner-config [config]',
`Test runner config file, defaults to e2e/mocha.opts for mocha and e2e/config.json' for jest`)
.option('-s, --specs [relativePath]',
`Root of test folder`)
.option('-l, --loglevel [value]',
'info, debug, verbose, silly, wss')
.option('-c, --configuration [device configuration]',
Expand All @@ -26,14 +28,23 @@ program

const config = require(path.join(process.cwd(), 'package.json')).detox;

const testFolder = config.specs || 'e2e';
const runner = config['test-runner'] || 'mocha';
const runnerConfig = program.runnerConfig || config['runner-config'] || getDefaultRunnerConfig();
const testFolder = getConfigFor('specs', 'e2e');
const runner = getConfigFor('testRunner', 'mocha');
const runnerConfig = getConfigFor('runnerConfig', getDefaultRunnerConfig());

if (typeof program.debugSynchronization === "boolean") {
program.debugSynchronization = 3000;
}

function getConfigFor(key, defaults) {
const keyKebabCase = camelToKebabCase(key);
return program[key] || config[key] || config[keyKebabCase] || defaults;
}

function camelToKebabCase(string) {
return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}

switch (runner) {
case 'mocha':
runMocha();
Expand Down
74 changes: 20 additions & 54 deletions detox/src/Detox.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ const DEVICE_CLASSES = {
};

class Detox {
constructor(userConfig) {
if (!userConfig) {
throw new Error(`No configuration was passed to detox, make sure you pass a config when calling 'detox.init(config)'`);
}

this.userConfig = userConfig;
constructor({deviceConfig, session}) {
this.deviceConfig = deviceConfig;
this.userSession = deviceConfig.session || session;
this.client = null;
this.device = null;
this._currentTestNumber = 0;
Expand All @@ -43,34 +40,32 @@ class Detox {
}
}

async init(params = {launchApp: true}) {
if (!(this.userConfig.configurations && _.size(this.userConfig.configurations) >= 1)) {
throw new Error(`No configured devices`);
}

const deviceConfig = await this._getDeviceConfig();
if (!deviceConfig.type) {
configuration.throwOnEmptyType();
}

const [sessionConfig, shouldStartServer] = await this._chooseSession(deviceConfig);
async init(userParams) {
const sessionConfig = await this._getSessionConfig();
const defaultParams = {launchApp: true, initGlobals: true};
const params = Object.assign(defaultParams, userParams || {});

if (shouldStartServer) {
if (!this.userSession) {
this.server = new DetoxServer(new URL(sessionConfig.server).port);
}

this.client = new Client(sessionConfig);
await this.client.connect();

const deviceClass = DEVICE_CLASSES[deviceConfig.type];
const deviceClass = DEVICE_CLASSES[this.deviceConfig.type];

if (!deviceClass) {
throw new Error(`'${deviceConfig.type}' is not supported`);
throw new Error(`'${this.deviceConfig.type}' is not supported`);
}

const deviceDriver = new deviceClass(this.client);
this.device = new Device(deviceConfig, sessionConfig, deviceDriver);
this.device = new Device(this.deviceConfig, sessionConfig, deviceDriver);
await this.device.prepare(params);
global.device = this.device;

if (params.initGlobals) {
deviceDriver.exportGlobals();
global.device = this.device;
}
}

async cleanup() {
Expand Down Expand Up @@ -105,41 +100,12 @@ class Detox {
}
}

async _chooseSession(deviceConfig) {
let session = deviceConfig.session;
let shouldStartServer = false;

if (!session) {
session = this.userConfig.session;
}

if (!session) {
session = await configuration.defaultSession();
shouldStartServer = true;
}
async _getSessionConfig() {
const session = this.userSession || await configuration.defaultSession();

configuration.validateSession(session);

return [session, shouldStartServer];
}

async _getDeviceConfig() {
const configurationName = argparse.getArgValue('configuration');
const configurations = this.userConfig.configurations;

let deviceConfig;
if (!configurationName && _.size(configurations) === 1) {
deviceConfig = _.values(configurations)[0];
} else {
deviceConfig = configurations[configurationName];
}

if (!deviceConfig) {
throw new Error(`Cannot determine which configuration to use. use --configuration to choose one of the following:
${Object.keys(configurations)}`);
}

return deviceConfig;
return session;
}
}

Expand Down
Loading