From 476ac44572d602dce14cb3351be7c2dc8a19a57d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=98=84=EC=84=B1=ED=95=84?= Date: Tue, 9 Jan 2024 22:21:59 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EC=8B=A4=ED=96=89=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/js/APIScenario.js | 1 + .../static/js/APIScenarioExecutor.js | 84 ++++++++++++++++++- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/main/resources/static/js/APIScenario.js b/src/main/resources/static/js/APIScenario.js index 9c9037b..16e5fed 100644 --- a/src/main/resources/static/js/APIScenario.js +++ b/src/main/resources/static/js/APIScenario.js @@ -277,6 +277,7 @@ class APIScenario { stopScenario() { if (this.executor) this.executor.reset(); + this.outputEditor.setValue(''); } addStart() { diff --git a/src/main/resources/static/js/APIScenarioExecutor.js b/src/main/resources/static/js/APIScenarioExecutor.js index 4e55105..753e08e 100644 --- a/src/main/resources/static/js/APIScenarioExecutor.js +++ b/src/main/resources/static/js/APIScenarioExecutor.js @@ -33,7 +33,7 @@ class APIScenarioExecutor { } // Method to execute the current node's API request if it's an 'api' node - executeCurrentNode() { + executeCurrentNode2() { console.log(this.currentNodeId) let allNodes = document.querySelector('#drawflow').querySelectorAll('.drawflow-node'); @@ -89,6 +89,69 @@ class APIScenarioExecutor { } } + executeCurrentNode() { + return new Promise((resolve, reject) => { + console.log(this.currentNodeId) + + let allNodes = document.querySelector('#drawflow').querySelectorAll('.drawflow-node'); + allNodes.forEach(node => { + node.classList.remove('current-execution'); + }); + + if (!this.currentNodeId) { + console.log('No current node to execute.'); + return; + } + + const currentNode = this.graph[this.currentNodeId]; + + if (currentNode) { + this.isRunning = true; + this.visitedNodes.add(this.currentNodeId); // Mark this node as visited + + let nodeElement = document.querySelector('#drawflow').querySelector(`.drawflow-node[id="node-${this.currentNodeId}"]`); + if (nodeElement) { + nodeElement.classList.add('current-execution'); + } else { + console.error('Node with ID ' + this.currentNodeId + ' not found'); + } + + if (currentNode.class === 'api') { + let model = JSON.parse(JSON.stringify(this.apiTester.getApiRequestModel(currentNode.data.id))); + model.responseModel = {}; + this.apiClient.sendAPIRequest(model, this.logHttpRequest.bind(this)) + .then(response => { + this.isRunning = false; + this.logHttpResponse(response); + if (response.status === 200 || response.status === 201) { + nodeElement.classList.add('result-success'); + } else { + nodeElement.classList.add('result-error'); + } + resolve(); + }) + .catch(error => { + nodeElement.classList.add('result-abort'); + $('.toast-body').text(error.message); + $('.toast').toast('show'); + this.appendLog(error.message); + reject(error); + }); + console.log(`Executing API request for node: ${currentNode.id} - ${currentNode.name}`); + } else if (currentNode.class === 'start') { + this.appendLog('Starting API scenario execution...'); + nodeElement.classList.add('result-success'); + resolve(); + } else { + console.log(`Executing script for node: ${currentNode.id} - ${currentNode.name}`); + resolve(); + } + + } + }); + + } + appendLog(text) { var range = new monaco.Range(this.outputEditor.getModel().getLineCount(), 1, this.outputEditor.getModel().getLineCount(), 1); var id = {major: 1, minor: 1}; @@ -141,11 +204,24 @@ class APIScenarioExecutor { } // Method to execute the entire scenario from start to end + // async executeAll() { + // while (this.currentNodeId) { + // console.log(this.currentNodeId) + // this.executeCurrentNode(); //TODO: how can I make sure this call wait until response is received + // await this.moveToNextNodeWithDelay(250); + // } + // console.log('Completed execution of all API nodes.'); + // } async executeAll() { while (this.currentNodeId) { - console.log(this.currentNodeId) - this.executeCurrentNode(); - await this.moveToNextNodeWithDelay(500); + console.log(this.currentNodeId); + try { + await this.executeCurrentNode(); + } catch (error) { + console.error('Error executing node: ', error); + // Handle error or break loop if necessary + } + await this.moveToNextNodeWithDelay(250); } console.log('Completed execution of all API nodes.'); }