Util

It is obtained by introducing the Util object in "leanpro.common", which provides tool functions commonly used in automation scripts.

JavaScript
const { Util } = require('leanpro.common');

It is defined as follows.

JavaScript
Python
class Util {
    static delay(milliseconds: number): Promise<void>;
    static launchProcess(exePath: string, ...args: string[]): ChildProcess; 
    static launchProcess(exePaths: string, args: string[], options: SpawnOptions): ChildProcess;
    static stopProcess(proc: ChildProcess): boolean;
    static takeScreenshot(filePath: string = null, monitor: number = 0): string | void;
    static loadCsvFile(filePath: string): Promise<RowCsv[]>;
    static saveToCsvFile(rows: RowCsv[], filePath: string): boolean;
    static getClipboard(): Promise<string>;
    static setClipboard(text: string): Promise<void>;
    static copyDir(sourceDir, destDir): Promise<void>;
}
class Util():
	def copyDir(source: str, target: str) -> None
	def delay(milliseconds: int) -> None
	def launchProcess(exePath: str, *args: List[str]) -> "ChildProcess"
	def stopProcess(proc: ChildProcess) -> bool

delay(miliseconds): Promise<void>

Wait for a period of time, pass in the parameter specifying the number of milliseconds to delay. Because it is an asynchronous call, remember that await needs to be added in front of it.

  • miliseconds: number type, wait time in milliseconds;
  • return value: asynchronous method that does not return any value;

The following code opens Calculator app, waits for a second till it initializes successfully and starts clicking.

JavaScript
async function run() {
    let calcPath = 'c:/windows/system32/calc.exe';
    await Util.launchProcess(calcPath);
    await Util.delay(1000); //wait for process to initialize
    await model.getButton("Two").click();
}

run();

launchProcess(exePath: string, ...args: string[]): ChildProcess;

Start a process. This is generally used to launch the application under test. The above example shows how to launch a calculator application using the launchProcess() method.

  • exePath: (optional) string type, the path to the executable file, usually ending with .exe.
  • args: (optional) string[] type, the arguments used to start the process, used similarly to child_process.spawn();
  • Return value: object of type ChildProcess, which is usually used in CukeTest to record information about the started process, and later can be used in the stopProcess() method to close the process. More information about the ChildProcess class can be found in child_process class

launchProcess(exePaths: string, args?: string[], options?: SpawnOptions): ChildProcess;

The call to start the process using advanced options, which can be used instead of the above call when the application under test has requirements for environment variables, working directory, and other conditions.

  • exePath: string type, the path to the executable file, usually ending with .exe.
  • args: (optional) string[] type, the arguments used to start the process, used similarly to child_process.spawn();
  • options: (optional) SpawnOptions type, More parameters to control the operation of the process, as an object, you can specify options such as environment variables and working directories in it.
    • cwd: string type, the working directory of the child process, the default value is the running project directory, that is, the value of process.cwd().
    • env: Object type. Environment key-value pairs. Default value: process.env, which is the environment variable when CukeTest is running.
    • detached boolean type. A child process runs independently of its parent process (runs out of control of the parent process).
    • shell boolean or string type. If true, run the command inside a shell. Use '/bin/sh' on Unix and process.env.ComSpec on Windows. A different shell can be specified as a string. Default: false (no shell).
  • Return value: ChildProcess type object, which is usually used to record the information of the started process in CukeTest, and then it can be used to stop the process with the stopProcess() method. For more descriptions about the ChildProcess class, please refer to child_process class

stopProcess(proc: ChildProcess): boolean;

Stop a process. Pass the return value of launchProcess() method to proc to stop the process.

  • proc: ChildProcess type, process information, usually returned by launchProcess() method, cannot directly create an object.
  • Return value: boolean type, execution result. Returns true if stopping the process was successful; otherwise returns false.

JavaScript
async function run() {
    let notepadPath = 'c:/windows/notepad.exe';
    let proc = await Util.launchProcess(notepadPath);
    //do some other operations...
    Util.stopProcess(proc);
}

run();

NOTE: Some apps are multi-process. The interface window is opened by the main process. Stopping the main process in this case does not close the application interface. This is the case with the Calculator app in Windows 10.

copyDir(sourceDir: string, destDir: string): Promise<void>

Copies the contents of the target folder into the specified directory, or creates the target directory if it does not exist.

  • sourceDir: string type, which is the path of the folder to be copied.
  • destDir: string type, is the path to copy to the destination folder.

JavaScript
const { Util } = require('leanpro.common');
(async () => {
    const sourceDir = './model_files';
    const destDir = './test/model_copy_files';
    await Util.copyDir(sourceDir, destDir)
})();

takeScreenshot(filePath: string = null, monitor: number = 0): string

Capture the entire screen image, save it in png format, and return the base64 encoding of the screenshot, which can be directly used as the report attachment of the running report.

  • filePath is the file path and should end with .png suffix. If filePath is null, return the base64 encoding of the image.
  • monitor is the number of the captured screen, 0 is all monitors, 1 is the first, and the default is 0.
  • Return value: string type, the base64 encoding of the screenshot.

For the old screen capture method, it is recommended to use capture() and captureToFile() methods of Screen object, see the introduction Screen Automation Object;

loadCsvFile(filePath: string): Promise<RowCsv[]>

  • filepath: string type, the path of the CSV file to be read;
  • Return value: 'RowCsv' type, an array of json objects, the key of each object is the column name, and the value is the data.

For example a data.csv file with the following content:

first_name,last_name,company_name,state,zip
James,Butt,"Benton, John B Jr",LA,70116
Josephine,Darakjy,"Chanay, Jeffrey A Esq",MI,48116
Art,Venere,"Chemel, James L Cpa",NJ,8014

Run the following code to load the csv file and return the json data:

JavaScript
(async function() {
    let data = await Util.loadCsvFile('C:\\temp\\data.csv');
    console.log(data);
})();

It will return the following json data:

[
{ "first_name": "James",
  "last_name": "Butt",
  "company_name": "Benton, John B Jr",
  "state": "LA",
  "zip": "70116" },
{ "first_name": "Josephine",
  "last_name": "Darakjy",
  "company_name": "Chanay, Jeffrey A Esq",
  "state": "MI",
  "zip": "48116" },
{ "first_name": "Art",
  "last_name": "Venere",
  "company_name": "Chemel, James L Cpa",
  "state": "NJ",
  "zip": "8014" } 
]

saveToCsvFile(rows: RowCsv[], filePath: string): boolean;

After getting the data in json format, you can use the saveToCsvFile(rows, filePath) function to save the data as a csv file.

JavaScript
Util.saveToCsvFile(rows: RowCsv[], filePath: string): boolean;

  • The parameter rows is the row data, its key is the column name, and its value is the element in the cell;
  • The parameter filePath is the saved file path;

For example, we need to get the data from the data.csv file read in the previous step and save them in the data_bak.csv file in the script directory. The code is as follows:

JavaScript
(async function() {
    let data = await Util.loadCsvFile('C:\\temp\\data.csv');
    // console.log(data);
    Util.saveToCsvFile(data, "./data_bak.csv");
})();

After run, data_bak.csv file is generated in the root directory. When open it, you can notice that the content is the same as the content of the data.csv file.

Clipboard Operation

Below are two methods - reading and setting the value of the clipboard.

getClipboard(): Promise<string>

Get the value currently in the clipboard.

  • Return value: string type, asynchronously returns the text in the clipboard.

setClipboard(text: string): Promise<void>

Modifies the value currently in the clipboard.

  • text: string type, the text string to be written to the clipboard;
  • Return value: string type, asynchronously returns the text in the clipboard.

E.g.

JavaScript
(async function() {

    await Util.setClipboard('(🦄)');
    let text = await Util.getClipboard();

    console.log(text);
})();

results matching ""

    No results matching ""