Util
It is obtained by introducing the Util object in "leanpro.common", which provides tool functions commonly used in automation scripts.
const { Util } = require('leanpro.common');It is defined as follows.
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) -> booldelay(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: numbertype, 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.
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();def run():
    calcPath = 'c:/windows/system32/calc.exe'
    Util.launchProcess(calcPath)
    Util.delay(1000) #/wait for process to initialize
    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) stringtype, the path to the executable file, usually ending with.exe.
- args: (optional) string[]type, the arguments used to start the process, used similarly tochild_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 thestopProcess()method to close the process. More information about theChildProcessclass 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: stringtype, the path to the executable file, usually ending with.exe.
- args: (optional) string[]type, the arguments used to start the process, used similarly tochild_process.spawn();
- options: (optional) SpawnOptionstype, 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:- stringtype, the working directory of the child process, the default value is the running project directory, that is, the value of- process.cwd().
- env:- Objecttype. Environment key-value pairs. Default value:- process.env, which is the environment variable when CukeTest is running.
- detached- booleantype. A child process runs independently of its parent process (runs out of control of the parent process).
- shell- booleanor- stringtype. If- true, run the command inside a- shell. Use- '/bin/sh'on Unix and- process.env.ComSpecon Windows. A different shell can be specified as a string. Default: false (no shell).
 
- Return value: ChildProcesstype 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 thestopProcess()method. For more descriptions about theChildProcessclass, 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: ChildProcesstype, process information, usually returned bylaunchProcess()method, cannot directly create an object.
- Return value: booleantype, execution result. Returnstrueif stopping the process was successful; otherwise returnsfalse.
async function run() {
    let notepadPath = 'c:/windows/notepad.exe';
    let proc = await Util.launchProcess(notepadPath);
    //do some other operations...
    Util.stopProcess(proc);
}
run();def run():
    notepadPath = 'c:/windows/notepad.exe'
    proc = Util.launchProcess(notepadPath)
    # 在这里添加您的代码来执行其他操作
    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:- stringtype, which is the path of the folder to be copied.
- destDir:- stringtype, is the path to copy to the destination folder.
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.
- filePathis the file path and should end with- .pngsuffix. If- filePathis null, return the base64 encoding of the image.
- monitoris the number of the captured screen, 0 is all monitors, 1 is the first, and the default is 0.
- Return value: stringtype, thebase64encoding of the screenshot.
For the old screen capture method, it is recommended to use
capture()andcaptureToFile()methods ofScreenobject, see the introduction Screen Automation Object;
loadCsvFile(filePath: string): Promise<RowCsv[]>
- filepath: stringtype, the path of the CSV file to be read;
- Return value: 'RowCsv' type, an array of json objects, the keyof each object is the column name, and thevalueis 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:
(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.
Util.saveToCsvFile(rows: RowCsv[], filePath: string): boolean;- The parameter rowsis the row data, its key is the column name, and its value is the element in the cell;
- The parameter filePathis 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:
(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: stringtype, asynchronously returns the text in the clipboard.
setClipboard(text: string): Promise<void>
Modifies the value currently in the clipboard.
- text: stringtype, the text string to be written to the clipboard;
- Return value: stringtype, asynchronously returns the text in the clipboard.
E.g.
(async function() {
    await Util.setClipboard('(🦄)');
    let text = await Util.getClipboard();
    console.log(text);
})();