Support & information center

Local Storage access for testRTC scripting

Local storage is a feature provided by web browsers to allow websites and web applications to store data locally on a user’s device. It’s a form of client-side storage, which means the data is stored on the user’s computer rather than on a remote server.

Usage

Local storage can be used by testRTC to store data in the form of key-value pairs and has some distinct characteristics and uses:

  1. Simple API: Browsers provide a simple JavaScript API for working with local storage. You can use the localStorage object to set and retrieve data.
  1. Persistent Storage: Unlike session storage, which only lasts for the duration of a single browser session, local storage provides persistent storage. This means that data stored in local storage remains available even if the user closes the browser and returns to the website at a later time.
  2. Storage Limit: Local storage has a size limit, which varies depending on the browser but is typically around 5-10 megabytes. This limit is per domain, so different websites cannot access each other’s local storage.

Sample script

The sample script that follows demonstrates how you can set a local storage variable, get a variable, and use that variable later in the script.

client
    .execute(function(data) {
        localStorage.setItem('name', 'Cristiano Ronaldo');
    })

client
    .execute(function(data) {
        return localStorage.getItem('name')
    }, function(result) {
        client
            .rtcSetSessionValue("local_Storage_name", result.value)
    });

client
    .rtcWaitForSessionValue('local_Storage_name', function(value) {
        client
            .rtcInfo('-----rtcInfo---local_Storage_name-------- ', value)
    }, 30000);

Script break down

Let’s break down what each part of the script does step by step:

  1. First Block:
client
    .execute(function(data) {
        localStorage.setItem('name', 'Cristiano Ronaldo');
    })
  • This block uses the ‘client.execute’ function to run a JavaScript function on the web page being tested.
  • Inside the function, it sets an item in the browser’s local storage with the key ‘name’ and the value ‘Cristiano Ronaldo’.
  1. Second Block:
client
    .execute(function(data) {
        return localStorage.getItem('name')
    }, function(result) {
        client
            .rtcSetSessionValue("local_Storage_name", result.value)
    });
  • In this block, another ‘client.execute’ function is used.
  • This time, it retrieves the value associated with the key ‘name’ from the local storage.
  • The retrieved value is then passed to a callback function, and this callback function uses;
    • ‘client.rtcSetSessionValue’ to set a session value with the key
    • ‘local_Storage_name’ to the retrieved value.
  1. Third Block:
client
    .rtcWaitForSessionValue('local_Storage_name', function(value) {
        client
            .rtcInfo('-----rtcInfo---local_Storage_name-------- ', value)
    }, 30000);
  • In the third block, the script waits for a session value with the key ‘local_Storage_name’ to be available.
  • Once the value is available, it logs an information message using ‘client.rtcInfo’, including the value retrieved from the session.

Summary

In summary, this script does the following:

  1. Sets a value in the browser’s local storage with the key ‘name’ and the value ‘Cristiano Ronaldo’.
  2. Retrieves the value associated with the key ‘name’ from local storage, stores it as a session value with the key ‘local_Storage_name’, and logs this value as an information message.
  3. The script waits for the session value with the key ‘local_Storage_name’ to become available, and once available, it logs the value.

Was this article helpful?

Related Articles