Skip to content

Instantly share code, notes, and snippets.

@latin-panda
Created April 10, 2024 12:25
Show Gist options
  • Save latin-panda/9faca345602e917862e521e58229a290 to your computer and use it in GitHub Desktop.
Save latin-panda/9faca345602e917862e521e58229a290 to your computer and use it in GitHub Desktop.
This is a draft of what a settings file would look like to make apdex e2e generic.
{
"instanceURL": "https://...",
"users": [
{
"type": "offline",
"role": "chw",
"username": "abc",
"password": "abc-pass"
}
],
"pages": {
"contactList": {
"selector": "//*[@text=\"People\"]"
},
"contactDetails": [
{
"id": "chw-area",
"contactType": "area",
"selector": "//*[contains(@text, \"CHW AREA\")]"
},
{
"id": "household-1",
"contactType": "household",
"selector": "//*[contains(@text, \"Mutui Household\")]"
},
{
"id": "patient-1",
"contactType": "patient",
"selector": "//*[contains(@text, \"Robert Mutui\")]"
}
],
"reportList": {
"selector": "//*[@text=\"Reports\"]"
},
"reportDetails": [
{
"form": "",
"selector": "//*[@text=\"Tasks\"]"
},
{
"form": "",
"selector": "//*[@text=\"Tasks\"]"
}
],
"taskList": {
"selector": "//*[@text=\"Tasks\"]"
},
"messageList": {
"selector": "//*[@text=\"Messages\"]"
},
"targets": {
"selector": "//*[@text=\"Analytics\"]"
}
},
"contactForms": [
{
"form": "create-household",
"selector": "//*[contains(@text, \"CHW AREA\")]",
"pages": [
[
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
},
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
}
],
[
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
},
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
}
]
]
}
],
"reportForms": [
{
"form": "pregnancy",
"selector": "//*[contains(@text, \"CHW AREA\")]",
"pages": [
[
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
},
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
}
],
[
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
},
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
}
]
]
}
],
"taskForms": [
{
"form": "pregnancy",
"selector": "//*[contains(@text, \"CHW AREA\")]",
"pages": [
[
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
},
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
}
],
[
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
},
{
"selector": "//*[contains(@text, \"Robert Mutui\")]",
"value": "",
"fieldType": ""
}
]
]
}
]
}
@latin-panda
Copy link
Author

latin-panda commented Apr 10, 2024

Service to load and access this settings file. The settings file is passed as parameter when running the command line, for example: npm run performance-e2e *path_to_your_settings_file*

import { resolve, extname } from 'node:path';
import { existsSync, readFileSync } from 'node:fs';

export class SettingsService {
  static #SUPPORTED_INPUT_FILE = '.json';
  static #settings;

  constructor() {}

  static #setSettings(settings) {
    this.settings = settings;
  }

  static #getInputFilePath() {
    const args = process.argv.slice(2);
    if (!args?.length) {
      throw new Error(
        'No path to the settings file provided. Expected: npm run performance-e2e *path_to_your_settings_file*'
      );
    }

    const path = resolve(args[0]);
    if (extname(path) !== this.#SUPPORTED_INPUT_FILE) {
      throw new Error(
        'The settings file is not a JSON file. Retry using a file with extension ending in .json'
      );
    }

    if (!existsSync(path)) {
      throw new Error('The settings file does not exist in the specified location. Verify the path is correct.');
    }

    return path;
  };

  static async loadSettingsFile() {
    const binary = readFileSync(this.#getInputFilePath());
    this.#setSettings(JSON.parse(binary));
  };

  static getInstanceURL(type) {
    return this.settings?.instanceURL;
  }

  static getUserByType(type) {
    return this.settings?.users?.find(user => user.type === type);
  }

  static getContactList(pageName) {
    return this.settings?.pages?.contactList;
  }

  static getContactDetails(contactType) {
    return this.settings?.pages?.contactDetails?.find(item => item.contactType === contactType );
  }

  static getReportList(pageName) {
    return this.settings?.pages?.reportList;
  }

  static getReportDetails(formId) {
    return this.settings?.pages?.reportDetails?.find(item => item.form === formId );
  }

  static getTaskList(pageName) {
    return this.settings?.pages?.taskList;
  }

  static getMessageList(pageName) {
    return this.settings?.pages?.messageList;
  }

  static getTargets(pageName) {
    return this.settings?.pages?.targets;
  }

  static getContactForms(formId) {
    return this.settings?.contactForms?.find(item => item.form === formId );
  }

  static getReportForm(formId) {
    return this.settings?.reportForms?.find(item => item.form === formId );
  }

  static getTaskForms(formId) {
    return this.settings?.taskForms?.find(item => item.form === formId );
  }
}

@latin-panda
Copy link
Author

latin-panda commented Apr 10, 2024

Usage of Settings Service

import { SettingsService } from './settings.service.js';

(async function() {
  try {
    await SettingsService.loadSettingsFile();
    console.log('ContactList - Tab:', SettingsService.getContactList());
    console.log('CHW Area:', SettingsService.getContactDetails('area'));
    console.log('contactForms:', SettingsService.getContactForms('create-household'));
    console.log('users:', SettingsService.getUserByType('offline'));
    console.log('instanceURL:', SettingsService.getInstanceURL());
  } catch (error) {
    console.error('ERROR: ', error.message || error);
    process.exit(1);
  }
})();

Output:

> e2e_experiment@1.0.0 do
> node . ./settings.json

ContactList - Tab: { selector: '//*[@text="People"]' }
CHW Area: {
  id: 'chw-area',
  contactType: 'area',
  selector: '//*[contains(@text, "CHW AREA")]'
}
contactForms: {
  form: 'create-household',
  selector: '//*[contains(@text, "CHW AREA")]',
  pages: [ [ [Object], [Object] ], [ [Object], [Object] ] ]
}
users: { type: 'offline', role: 'chw', username: 'abc', password: 'abc-pass' }
instanceURL: https://...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment