Skip to content

Latest commit

 

History

History
109 lines (101 loc) · 3.23 KB

File metadata and controls

109 lines (101 loc) · 3.23 KB

PerfectoRestClientDemo

NodeJS Demo Client for Perfecto REST API.

Get started with Perfecto RESTful API, read more at our Community.

Install & dependencies:

  • Run npm install command within the project's directory.

Running tests:

  • The tests example here uses Jasmine testing framework.
  • Configure your Perfecto lab User, password and host at ClientConf.js.
  • Run the tests using npm test command.

Useage:

  • Require Perfecto library:
var Perfecto = require('../lib/Perfecto').Perfecto;
  • Creating a new client:
var client = new Perfecto.PerfectoClient('https' , 'MyHost.perfectomobile.com', MyUser, MyPassword);
var client = new Perfecto.PerfectoClient(Conf.protocol ,Conf.host, Conf.user, Conf.pass);
  • Starting a new execution, open device and browser navigation example:
client.startNewExecution()
    .then((execution_Id)=>{
        return client.openDevice(deviceId);
    })
    .then((ans)=>{
        // ans = REST API response, JSON format
        return client.browserOpen();
    })
    .then((ans)=>{
        return client.browserGoTo('https://google.com');
    })
    .then((ans)=>{
        return client.browserClose();
    })
    .then((ans)=>{
        return client.closeDevice();
    })
    .then((ans)=>{
        return client.endExecution();
    })
    .catch((err)=>{
        logger.log(err);
    });
  • .then(ans) .... , the ans object contains the previous block response (JSON format).
 .then((ans)=>{
     console.log(ans); // print the respone's body.
     //... do something
 }

More Functionalities:

  • Devices and Cradle Commands: The following devices and cradles commands are supported:
    Device info, device list, cradle info and cradle list.
    For example - getting devices list:
    After starting a new execution:
.then((ans)=>{
    return client.getDevicesList();
})
.then((ans)=>{
    console.log(ans); // Print list of the device in your cloud.
});

Implementing a new command:

Consider the following function in the file PerfectoClient.js:

/**
 * Execute http get command
 * 
 * ### args ###
 * command = command name
 * subcommand = subcommand name
 * params = array with the following form ['param=ParamValue','param1=ParamValue' ....]
 * 
 * ### return ###
 * Promise
 */
this.executeCommand = (command, subcommand, params)=>{
    let req_url = this.base_url + '/' + this.executionId + '?' + 'operation=command' +
        '&user='+ this.user +'&password=' + this.password + '&command=' + 
        command + '&subcommand=' + subcommand;
   .
   .
   .
   // JavaScript code ...
}

Use the executeCommand method to add your new command, for example:

this.myNewCommand = (Param)=>{
    let params = ['ParamName=' + Param];
    return this.executeCommand('MyNewCommandName', 'MyNewCommandSubCommand', params);
}

Make sure you add the command within the PerfectoClient object scope.