Skip to content

Instantly share code, notes, and snippets.

@trix0
Created January 25, 2018 08:48
Show Gist options
  • Save trix0/8588a7913922d7de62762debb8f71be1 to your computer and use it in GitHub Desktop.
Save trix0/8588a7913922d7de62762debb8f71be1 to your computer and use it in GitHub Desktop.
code
const Swagger = require('swagger-client');
const { exec } = require('child_process');
const wdio = require('webdriverio');
const SWAGGER_URL = 'http://localhost:7100/api/v1/swagger.json'
const AUTH_TOKEN = '26cd01ab067140ec8f6934253c41eceba51ec96c0b1440f1a4fe1c6aa42c7507';
const { performance } = require('perf_hooks');
const fs =require("fs");
const client = new Swagger({
url: SWAGGER_URL
, usePromise: true
, authorizations: {
accessTokenAuth: new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + AUTH_TOKEN, 'header')
}
})
const serial = "cd21ccc5"
client.then((api)=> {
performance.mark('A');
return api.devices.getDeviceBySerial({
serial: serial
, fields: 'serial,present,ready,using,owner'
})
.then(function(res) {
// check if device can be added or not
let device = res.obj.device
if (!device.present || !device.ready || device.using || device.owner) {
throw new Error('Device is not available')
}
return api.user.addUserDevice({
device: {
serial: device.serial
, timeout: 900000
}
})
.then(function(res) {
if (!res.obj.success) {
throw new Error('Could not connect to device')
}
return api.user.remoteConnectUserDeviceBySerial({
serial: device.serial
})
.then(function(res) {
//console.log(res.obj.remoteConnectUrl);
exec('adb connect '+res.obj.remoteConnectUrl+'','',(err, stdout, stderr)=>{
//console.log(stdout);
});
return res.obj.remoteConnectUrl
}).then(async (UDID)=>{
try{
process.on('SIGINT', ()=>{
fnDisconnect(UDID);
});
//await fnCreateServer(UDID);
await fnConnect(UDID);
await console.log("connected");
await performance.mark('B');
performance.measure('A to B', 'A', 'B');
const measure = performance.getEntriesByName('A to B')[0];
console.log(measure.duration/1000);
//await fnDisconnect(UDID);
}
catch(err){
fnDisconnect(UDID).catch(err=>{
throw err;
})
throw err;
}
})
})
})
})
async function fnCreateServer(UDID){
const { stdout, stderr } = await exec('appium --session-override --log-timestamp --log /home/trixo/Code/javaRewrite/log.txt');
if(stderr._hadError){
return Promise.reject(Error("err"));
}
await timeout(2000);
console.log("server Created");
return Promise.resolve();
}
function fnConnect(UDID){
return new Promise((resolve,reject)=>{
const opts = {
port: 4723,
desiredCapabilities: {
platformName: "Android",
deviceName: "Android",
//automationName: "uiautomator2",
udid:UDID, //cd21ccc5 emulator-5554
appPackage:"com.pointvoucher.playlondonpv",
appActivity:"com.unity3d.player.UnityPlayerActivity",
fullReset:false,
noReset:true,
noPermsCheck:true,
launch:true
//autoGrantPermissions:true,
//disableAndroidWatchers:true,
//skipUnlock:true,
//noSign:true,
//gpsEnabled:false,
//ignoreUnimportantViews:true
}
};
const driver = wdio.remote(opts);
driver.init(opts, (error) =>{
if (error) {
reject(new Error('Session did not start properly. Please make sure you sauce credentials are correct'));
}
}).then(()=>{
resolve(driver);
})
})
}
function fnDisconnect(UDID){
client.then(function(api) {
return api.user.getUserDevices({
serial: serial
, fields: 'serial,present,ready,using,owner'
}).then(function(res) {
// check if device can be added or not
var devices = res.obj.devices
var hasDevice = false
devices.forEach(function(device) {
if(device.serial === serial) {
hasDevice = true;
}
})
if (!hasDevice) {
throw new Error('You are not owner')
}
return api.user.deleteUserDeviceBySerial({
serial: serial
}).then(function(res) {
if (!res.obj.success) {
throw new Error('Could not disconnect to device')
}
console.log("Disconnected!")
})
})
})
}
function timeout(delay){
return new Promise((resolve)=>{
setTimeout(resolve,delay)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment