Support & information center

Using an external Database Firebase

The complete source for this sample script can be downloaded from the bottom of this page. 

Save the json file to your local computer and import it to testRTC’s console.

There are cases where there is a need to store and retrieve information from an external database. For example:

  • Use an external configuration – an external system will configure one or more parameters stored in a DB that the test script will retrieve and use
  • Keep a state between test or monitor executions. For example, if we wish to perform a specific activity in every 5th test execution

In order to use an external DB, we will use the rtcActivateWebhook() command.

In the following code snippet we will use Firebase, a real-time cloud database (acquired by Google).

Relevant Firebase documentation can be found in https://firebase.google.com/docs/database/rest/save-data

API and code snippets

Retrieve a value – Firebase API

https://[Account].firebaseio.com/[value_name].json

Retrieve a value – in web browser

Retrieve a value from Firebase – testRTC code snippet

 .rtcActivateWebhook("https://[Account].firebaseio.com/variable.json", function(answer) { }Code language: JavaScript (javascript)

Update value in Firebase – Firebase API

POST /[value_name].json HTTP/1.1
Host: [Account].firebaseio.com
Content-Type: text/xml
X-HTTP-Method-Override: PATCH
Cache-Control: no-cache
{ [value_content] }

Update value in Firebase – testRTC code snippet

var options = {
        url: 'https://[Account].firebaseio.com/variable.json,
        headers: {
            'X-HTTP-Method-Override': 'PATCH',
            'Cache-Control': 'no-cache',
            'Content-Type': 'text/xml'
        }
    };
    client.rtcActivateWebhook(headers, {value: parseInt(result.value)}, function(answer) {
        console.log('Store response: ' + JSON.stringify(answer));
    })Code language: PHP (php)

Sample script

Script flow

  1. Read current time in seconds from 1970 (called ‘Epoch Unix Timestamp’) from web site http://www.unixtimestamp.com/ (or http://www.epochconverter.com/) – see attached screen shot
  2. Store (‘Current Epoch Unix Timestamp’ + 20) in Firebase in [Firebase_value]
  3. Sleep 10 seconds
  4. Read [Firebase_value] and ‘Current Epoch Unix Timestamp’
  5. Ensure that [Firebase_value] < ‘Current Epoch Unix Timestamp’
  6. Sleep 20 seconds
  7. Read [Firebase_value] and ‘Current Epoch Unix Timestamp’

Test script source code

var agentName = process.env.RTC_AGENT_NAME;
var agentType = Number(process.env.RTC_IN_SESSION_ID); 
var assert = require('assert');
var jsonName= 'url8.json';

client
    .rtcSetTestExpectation("video.in == 0")
    .rtcSetTestExpectation("audio.in == 0")
    .rtcSetTestExpectation("video.out == 0")
    .rtcSetTestExpectation("audio.out == 0")
    .rtcInfo("testRTC start - agent name: %s agent type: %d", agentName, agentType)

    //Read current time in seconds from 1970 (called ‘Epoch Unix Timestamp’) from web site http://www.epochconverter.com/ )
    .url(process.env.RTC_SERVICE_URL)
    .waitForElementVisible('#ecclock', 35000)
    .getText("#ecclock", function(result) {
    this.assert.equal(typeof result, "object");
    this.assert.equal(result.status, 0);
    client.rtcInfo('Current time:' + result);

    //Store (‘Current Epoch Unix Timestamp’ + 20) in Firebase in [Firebase_value]
    var options = {
        url: 'https://testrtc-8d868.firebaseio.com/'+jsonName,
        headers: {
            'X-HTTP-Method-Override': 'PATCH',
            'Cache-Control': 'no-cache',
            'Content-Type': 'text/xml' 
        }
    };
    client.rtcActivateWebhook(options, {value: parseInt(result.value)+20}, function(answer) {
        client.rtcInfo('Stored expected time (compared = current + 20 seconds):' + JSON.stringify(answer));
    })

    //sleep for 10 seconds and read saved data from firebase
   .rtcActivateWebhook("https://testrtc-8d868.firebaseio.com/"+jsonName, function(answer) {
        var test = answer.split(":");
        var test1 = test[1].split("}");
        client.rtcInfo('Read expected time (should be the stored expected time):' + JSON.stringify(test1[0]));
        client.pause(10000);

        //After 10 seconds, read current time again from web site
        client.getText("#ecclock", function(result) {
            this.assert.equal(typeof result, "object");
            this.assert.equal(result.status, 0);
            client.rtcInfo('After 10 seconds sleep, current time:' + result.value);

            //Ensure that [Firebase_value] > ‘Current Epoch Unix Timestamp’
            if (parseInt(test1[0]) > parseInt(result.value))
            {
                client.rtcInfo('[Stored time value] > [Current time] - as expected');
                assert.equal(1, 1);
            }
            else
            {  
                client.rtcInfo('[Stored time value] <= [Current time] - NOT as expected');
                assert.equal(1, 2);
            }
        });
        
        //Sleep 20 seconds
        client.pause(20000);
        //Read [Firebase_value] and ‘Current Epoch Unix Timestamp’
        client.getText("#ecclock", function(result) {
            this.assert.equal(typeof result, "object");
            this.assert.equal(result.status, 0);
            client.rtcInfo('After 30 seconds sleep, current time:' + result.value);
            
            //client.pause(5000);
            //Ensure that [Firebase_value] < ‘Current Epoch Unix Timestamp’
            if (parseInt(test1[0]) > parseInt(result.value))
            {  
                client.rtcInfo('[Stored time value] > [Current time] - NOT as expected');
                assert.equal(1, 2);
            }
            else
            {  
                client.rtcInfo('[Stored time value] <= [Current time] - as expected');
                assert.equal(1, 1);
            }
        });
    });
});Code language: JavaScript (javascript)

Expected log messages

Was this article helpful?

Related Articles