From 47c4ebd99d1b14f3c5891a4dc2f771d099069774 Mon Sep 17 00:00:00 2001 From: asklie <760956257@qq.com> Date: Wed, 18 Dec 2024 12:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8node=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: asklie <760956257@qq.com> --- .../integration_test/key_input_test.dart | 49 ++++++ .../key_input_test_10000.dart | 110 ++++++++++++ .../path_provider_test_10000.dart | 145 +++++++++++++++ .../integration_test/sqflite_test_10000.dart | 132 ++++++++++++++ ohos/node_test_server/index.js | 166 +++++++++++++++--- ohos/node_test_server/index_10000.js | 120 +++++++++++++ 6 files changed, 694 insertions(+), 28 deletions(-) create mode 100644 ohos/automated_testing_demo/integration_test/key_input_test_10000.dart create mode 100644 ohos/automated_testing_demo/integration_test/path_provider_test_10000.dart create mode 100644 ohos/automated_testing_demo/integration_test/sqflite_test_10000.dart create mode 100644 ohos/node_test_server/index_10000.js diff --git a/ohos/automated_testing_demo/integration_test/key_input_test.dart b/ohos/automated_testing_demo/integration_test/key_input_test.dart index 6beeb1c0..93dcea6e 100644 --- a/ohos/automated_testing_demo/integration_test/key_input_test.dart +++ b/ohos/automated_testing_demo/integration_test/key_input_test.dart @@ -33,6 +33,20 @@ void main() { // 验证文本是否已经输入 expect(find.text('Hello, World!'), findsOneWidget); }); + + testWidgets('Test Input Box Enter', (WidgetTester tester) async { + // 准备测试 + await tester.pumpWidget(MaterialApp(home: EnterTextField())); + + // 找到显示按钮并点击 + await tester.tap(find.text('Show Dialog')); + await tester.pump(); // 更新视图以显示弹窗 + + // 检查弹窗是否显示 + expect(find.text('Alert Dialog'), findsOneWidget); + expect(find.text('This is an alert dialog.'), findsOneWidget); + expect(find.text('OK'), findsOneWidget); + }); } class MyTextField extends StatelessWidget { @@ -45,3 +59,38 @@ class MyTextField extends StatelessWidget { ); } } + +class EnterTextField extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + child: ElevatedButton( + key: const Key('my-text-field'), + child: Text('Show Dialog'), + onPressed: () async { + await showDialog(context: context, builder: (BuildContext context) { + return AlertDialog( + title: const Text('Alert Dialog'), + content: Text('This is an alert dialog.'), + actions: [ + TextButton(onPressed: () { + Navigator.pop(context); + }, child: const Text('OK')) + ], + ); + }); + }, + ), + ), + ); + } +} + +Future waiting() async { + int num = 0; + await Future.delayed(Duration(milliseconds: 10000), () { + num = num + 1; + }); + return num; +} diff --git a/ohos/automated_testing_demo/integration_test/key_input_test_10000.dart b/ohos/automated_testing_demo/integration_test/key_input_test_10000.dart new file mode 100644 index 00000000..58cc4b2b --- /dev/null +++ b/ohos/automated_testing_demo/integration_test/key_input_test_10000.dart @@ -0,0 +1,110 @@ +/* +* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/services.dart'; + +const times = 10000; +void main() { + testWidgets('Test keyboard input in a TextField', + (WidgetTester tester) async { + // 准备测试 + await tester.pumpWidget(MaterialApp(home: MyTextField())); + + // 找到文本字段 + Finder textFieldFinder = find.byType(TextField); + expect(textFieldFinder, findsOneWidget); + + // 输入文本 + var txt = ''; + for(var i = 0; i < times; i++) { + txt += 'a'; + await tester.enterText(textFieldFinder, 'a'); + } + await tester.pump(); // 刷新界面以便测试者可以看到变化 + + // 验证文本是否已经输入 + expect(find.text(txt), findsOneWidget); + }); + + + testWidgets('Test Input Box Enter', (WidgetTester tester) async { + // 准备测试 + await tester.pumpWidget(MaterialApp(home: EnterTextField())); + + // 找到显示按钮并点击 + for (var i = 0; i < times; i++) { + await tester.tap(find.text('Show Dialog')); + await tester.pump(); // 更新视图以显示弹窗 + + // 检查弹窗是否显示 + expect(find.text('Alert Dialog'), findsOneWidget); + expect(find.text('This is an alert dialog.'), findsOneWidget); + expect(find.text('OK'), findsOneWidget); + } + print('END'); + // await waiting(); + }); +} + +class MyTextField extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + child: TextField(), + ), + ); + } +} + +class EnterTextField extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + child: ElevatedButton( + key: const Key('my-text-field'), + child: Text('Show Dialog'), + onPressed: () async { + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Alert Dialog'), + content: Text('This is an alert dialog.'), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text('OK')) + ], + ); + }); + }, + ), + ), + ); + } +} + +Future waiting() async { + int num = 0; + await Future.delayed(Duration(milliseconds: 10000), () { + num = num + 1; + }); + return num; +} diff --git a/ohos/automated_testing_demo/integration_test/path_provider_test_10000.dart b/ohos/automated_testing_demo/integration_test/path_provider_test_10000.dart new file mode 100644 index 00000000..4d0052f1 --- /dev/null +++ b/ohos/automated_testing_demo/integration_test/path_provider_test_10000.dart @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'dart:io'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; +var times = 10000; +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + testWidgets('getTemporaryDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final String? result = await provider.getTemporaryPath(); + _verifySampleFile(result, 'temporaryDirectory'); + } + print('END'); + }); + + testWidgets('getApplicationDocumentsDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final String? result = await provider.getApplicationDocumentsPath(); + _verifySampleFile(result, 'applicationDocuments'); + } + print('END'); + }); + + testWidgets('getApplicationSupportDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final String? result = await provider.getApplicationSupportPath(); + _verifySampleFile(result, 'applicationSupport'); + } + print('END'); + }); + + testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final String? result = await provider.getApplicationCachePath(); + _verifySampleFile(result, 'applicationSupport'); + } + print('END'); + }); + + testWidgets('getLibraryDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + expect(() => provider.getLibraryPath(), + throwsA(isInstanceOf())); + } + print('END'); + }); + + testWidgets('getExternalStorageDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final String? result = await provider.getExternalStoragePath(); + _verifySampleFile(result, 'externalStorage'); + } + print('END'); + }); + + testWidgets('getDownloadsPathDirectory', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final String? result = await provider.getDownloadsPath(); + _verifySampleFile(result, 'externalStorage'); + } + print('END'); + }); + + testWidgets('getExternalCacheDirectories', (WidgetTester tester) async { + for (var i = 0; i < times; i++) { + final PathProviderPlatform provider = PathProviderPlatform.instance; + final List? directories = await provider.getExternalCachePaths(); + expect(directories, isNotNull); + for (final String result in directories!) { + _verifySampleFile(result, 'externalCache'); + } + } + print('END'); + }); + + final List allDirs = [ + null, + StorageDirectory.music, + StorageDirectory.podcasts, + StorageDirectory.ringtones, + StorageDirectory.alarms, + StorageDirectory.notifications, + StorageDirectory.pictures, + StorageDirectory.movies, + ]; + + for (final StorageDirectory? type in allDirs) { + testWidgets('getExternalStorageDirectories (type: $type)', + (WidgetTester tester) async { + final PathProviderPlatform provider = PathProviderPlatform.instance; + + final List? directories = + await provider.getExternalStoragePaths(type: type); + expect(directories, isNotNull); + expect(directories, isNotEmpty); + for (final String result in directories!) { + _verifySampleFile(result, '$type'); + } + }); + } +} + +/// Verify a file called [name] in [directoryPath] by recreating it with test +/// contents when necessary. +void _verifySampleFile(String? directoryPath, String name) { + expect(directoryPath, isNotNull); + if (directoryPath == null) { + return; + } + final Directory directory = Directory(directoryPath); + final File file = File('${directory.path}${Platform.pathSeparator}$name'); + + if (file.existsSync()) { + file.deleteSync(); + expect(file.existsSync(), isFalse); + } + + file.writeAsStringSync('Hello world!'); + expect(file.readAsStringSync(), 'Hello world!'); + expect(directory.listSync(), isNotEmpty); + file.deleteSync(); +} diff --git a/ohos/automated_testing_demo/integration_test/sqflite_test_10000.dart b/ohos/automated_testing_demo/integration_test/sqflite_test_10000.dart new file mode 100644 index 00000000..f36da1d3 --- /dev/null +++ b/ohos/automated_testing_demo/integration_test/sqflite_test_10000.dart @@ -0,0 +1,132 @@ +/* +* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import 'dart:io'; +import 'dart:io' as io; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart' hide test; +import 'package:path/path.dart'; +import 'package:sqflite/sqflite.dart'; +import 'package:test/test.dart' show test; +import 'dart:math'; + +void main() { + WidgetsFlutterBinding.ensureInitialized(); + Database? _database; + final String _tableName = "users"; + test('open-data', () async { + var _dbName = "${Random().nextInt(36000)}.db"; + try { + _database = await openDatabase(_dbName); + final path = await getDatabasesPath(); + print("====open-data-success:$path"); + } catch (error) { + print('====open-data-error' + error.toString()); + } + }); + + test('create', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + try { + await _database!.execute( + "CREATE TABLE $name(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)"); + print('====create-success'); + } catch (error) { + print('====create-error:' + error.toString()); + } + } + print('END'); + }); + test('insert-data', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + try { + final id = await _database!.insert(name, {'name': 'John', 'age': 25}); + print("====insert-data-success:id=$id"); + } catch (error) { + print("====insert-data-error:id=" + error.toString()); + } + } + print('END'); + }); + test('check-data', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + final maps = await _database!.query(name); + print('====check-data-success:' + + maps.map((map) => map.toString()).join("\n").toString()); + } + print('END'); + }); + test('update', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + try { + final changeRows = await _database!.update( + name, {'name': 'Jack', 'age': 25}, + where: 'id = ?', whereArgs: [2]); + print("====update-success:$changeRows"); + } catch (error) { + print("====update-error:" + error.toString()); + } + } + print('END'); + }); + test('update-raw', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + try { + final changeRows = await _database!.rawUpdate( + 'UPDATE $name SET name = ?, age = ? WHERE id = ?', + ['Jack', 25, 1 + i]); + print("====update-raw-success:$changeRows"); + } catch (error) { + print("====update-raw-error:" + error.toString()); + } + } + print('END'); + }); + test('update-raw-1', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + try { + final changeRows = await _database!.rawUpdate( + 'UPDATE OR ROLLBACK $name Set name = ?, age = ? WHERE name = ?', + ['Marco', 25 + i, "Jack"]); + print("====update-raw-1-success:$changeRows"); + } catch (error) { + print("====update-raw-1-error:" + error.toString()); + } + } + print('END'); + }); + + test('delete-raw', () async { + for (var i = 0; i < 10000; i++) { + var name = _tableName + i.toString(); + try { + int count = + await _database!.rawDelete("DELETE FROM $name WHERE id = ?", [2]); + print("====delete-raw-success:$count"); + } catch (e) { + print("====delete-raw-error:" + e.toString()); + } + } + print('END'); + }); +} diff --git a/ohos/node_test_server/index.js b/ohos/node_test_server/index.js index 54d1e9b6..b86f8723 100644 --- a/ohos/node_test_server/index.js +++ b/ohos/node_test_server/index.js @@ -14,41 +14,125 @@ */ process.stdout.setEncoding('utf-8'); const Excel = require('exceljs'); - +const { exec } = require('child_process'); const fs = require('fs'); const path = require('path'); -const fileUrl = './logs'; -const files = fs.readdirSync(fileUrl); -var time = new Date(); -var dirName = (time.getMonth() + 1) + '' + time.getDate() -try { - if (!fs.existsSync(path.resolve(__dirname, dirName))) { - fs.mkdirSync(path.resolve(__dirname, dirName), { recursive: true }); - } else { +const FILE_URL = './logs'; +const FILE_10000_URL = './logs_10000'; +// 先创建下logs文件夹,避免没有报错 +createFolder(FILE_URL); +const files = fs.readdirSync(FILE_URL); + +// 时间格式化,1、2、3、4变成01、02、03、04 +function doubleStr(number) { + return number < 10 ? '0' + number : number +} +// main(); +// createXlsx(FILE_URL); +longTimeTest(); +// 单次测试 +async function main() { + await runCmd(`chcp 65001`); + console.log('更新代码'); + await runCmd(`for /f "delims=" %i in ('where flutter') do @cd /d "%~dpi" && git pull`); + // console.log('camera_test'); + // await runCmd(`cd ..\\automatic_camera_test && flutter test .\\integration_test\\camera_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\camera_test.txt`); + console.log('sqflite_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\sqflite_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\sqflite_test.txt`); + console.log('path_provider_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\path_provider_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\path_provider_test.txt`); + console.log('key_input_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\key_input_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\key_input_test.txt`); + // console.log('video_player_test'); + // await runCmd(`cd ..\\automatic_video_player_test && flutter test .\\integration_test\\video_player_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\video_player_test.txt`); + // console.log('webview_flutter_test'); + // await runCmd(`cd ..\\automatic_webview_flutter_test && flutter test .\\integration_test\\webview_flutter_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\webview_flutter_test.txt`); + await runCmd(`timeout /t 10`); + createXlsx(FILE_URL); +} +// 压力测试 +async function pressureTest() { + await runCmd(`chcp 65001`); + console.log('更新代码'); + await runCmd(`for /f "delims=" %i in ('where flutter') do @cd /d "%~dpi" && git pull`); + console.log('camera_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\path_provider_test_10000.dart --machine > ..\\node_test_server\\${FILE_10000_URL}\\path_provider_test.txt`); + console.log('sqflite_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\key_input_test_10000.dart --machine > ..\\node_test_server\\${FILE_10000_URL}\\key_input_test.txt`); + console.log('path_provider_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\sqflite_test_10000.dart --machine > ..\\node_test_server\\${FILE_10000_URL}\\sqflite_test.txt`); + console.log('key_input_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\key_input_test.dart --machine > ..\\node_test_server\\${FILE_10000_URL}\\key_input_test.txt`); + console.log('video_player_test'); + await runCmd(`cd ..\\automatic_video_player_test && flutter test .\\integration_test\\video_player_test.dart --machine > ..\\node_test_server\\${FILE_10000_URL}\\video_player_test.txt`); + console.log('webview_flutter_test'); + await runCmd(`cd ..\\automatic_webview_flutter_test && flutter test .\\integration_test\\webview_flutter_test.dart --machine > ..\\node_test_server\\${FILE_10000_URL}\\webview_flutter_test.txt`); + createXlsx(FILE_10000_URL); +} + +// 超长时间测试 +async function longTimeTest() { + await runCmd(`chcp 65001`); + console.log('更新代码'); + await runCmd(`for /f "delims=" %i in ('where flutter') do @cd /d "%~dpi" && git pull`); + // console.log('camera_test'); + // await runCmd(`cd ..\\automatic_camera_test && flutter test .\\integration_test\\camera_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\camera_test.txt`); + for (let i = 0; i < 1000; i++) { + console.log('清理logs文件'); + deleteAllFilesInFolder(FILE_URL); + console.log('sqflite_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\sqflite_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\sqflite_test.txt`); + console.log('path_provider_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\path_provider_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\path_provider_test.txt`); + console.log('key_input_test'); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\key_input_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\key_input_test.txt`); + // console.log('video_player_test'); + // await runCmd(`cd ..\\automatic_video_player_test && flutter test .\\integration_test\\video_player_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\video_player_test.txt`); + // console.log('webview_flutter_test'); + // await runCmd(`cd ..\\automatic_webview_flutter_test && flutter test .\\integration_test\\webview_flutter_test.dart --machine > ..\\node_test_server\\${FILE_URL}\\webview_flutter_test.txt`); + // await runCmd(`timeout /t 10`); + createXlsx(FILE_URL); } -} catch (err) { - console.error(err); } -const workbook = new Excel.Workbook(); -files.forEach(async item => { - var fileStr = fs.readFileSync(fileUrl + '/' + item, 'utf8'); - const result = fomateTxt(fileStr) - const sheet = workbook.addWorksheet(item); - sheet.columns = [ - { header: '测试项', key: 'testName', width: 20 }, - { header: '结果', key: 'result', width: 10 }, - { header: '打印值', key: 'message', width: 50 }, - { header: '路径', key: 'testUrl', width: 50 }, - ]; - result.forEach(item => { - sheet.addRow(item) - }) - // 通过流发送文件 -}) -workbook.xlsx.writeFile(dirName + '/' + time.getHours() + '-' + time.getMinutes() + '.xlsx'); +function createFolder(dirName) { + try { + if (!fs.existsSync(path.resolve(__dirname, dirName))) { + fs.mkdirSync(path.resolve(__dirname, dirName), { recursive: true }); + } else { + } + } catch (err) { + console.error(err); + } +} + +// 生成表格 +function createXlsx(url) { + var time = new Date(); + var dirName = (time.getMonth() + 1) + '' + doubleStr(time.getDate()); + createFolder(dirName) + const workbook = new Excel.Workbook(); + files.forEach(async item => { + var fileStr = fs.readFileSync(url + '/' + item, 'utf8'); + const result = fomateTxt(fileStr) + const sheet = workbook.addWorksheet(item); + sheet.columns = [ + { header: '测试项', key: 'testName', width: 20 }, + { header: '结果', key: 'result', width: 10 }, + { header: '运行时间(ms)', key: 'difference', width: 10 }, + { header: '打印值', key: 'message', width: 50 }, + { header: '路径', key: 'testUrl', width: 50 }, + ]; + result.forEach(item => { + sheet.addRow(item) + }) + // 通过流发送文件 + }) + workbook.xlsx.writeFile(dirName + '/' + doubleStr(time.getHours()) + '-' + doubleStr(time.getMinutes()) + '.xlsx'); +} +// 处理txt数据 function fomateTxt(data) { let result = []; let resultMap = new Map(); @@ -60,6 +144,7 @@ function fomateTxt(data) { } if (itemJson.testID) { const newItem = resultMap.get(itemJson.testID); + newItem.difference = itemJson.time - newItem.time if (itemJson.type === 'testDone') { newItem.result = itemJson.result; } else if (itemJson.type === 'print') { @@ -76,4 +161,29 @@ function fomateTxt(data) { result.push(mapValue); } return result; +} +// 执行cmd命令 +function runCmd(cmd) { + return new Promise(function (resolve, reject) { + console.log(cmd) + exec(cmd, { + maxBuffer: 1024 * 2000 + }, function (err, stdout, stderr) { + if (err) { + // console.log(err); + reject(err); + } else if (stderr.lenght > 0) { + reject(new Error(stderr.toString())); + } else { + resolve(); + } + }); + }); +}; +// 清理文件内容 +function deleteAllFilesInFolder(folderPath) { + fs.readdirSync(folderPath).forEach(file => { + const filePath = path.join(folderPath, file); + fs.unlinkSync(filePath); + }); } \ No newline at end of file diff --git a/ohos/node_test_server/index_10000.js b/ohos/node_test_server/index_10000.js new file mode 100644 index 00000000..69c8c2ed --- /dev/null +++ b/ohos/node_test_server/index_10000.js @@ -0,0 +1,120 @@ +/* +* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +process.stdout.setEncoding('utf-8'); +const Excel = require('exceljs'); + +const fs = require('fs'); +const path = require('path'); + +const fileUrl = './logs_10000'; +createFolder(fileUrl); +const files = fs.readdirSync(fileUrl); +var exec = require("child_process").exec; +function runCmd(cmd) { + return new Promise(function (resolve, reject) { + exec(cmd, { + maxBuffer: 1024 * 2000 + }, function (err, stdout, stderr) { + if (err) { + console.log(err); + reject(err); + } else if (stderr.lenght > 0) { + reject(new Error(stderr.toString())); + } else { + console.log(stdout); + resolve(); + } + }); + }); +}; +// 时间格式化,1、2、3、4变成01、02、03、04 +function doubleStr(number) { + return number < 10 ? '0' + number : number +} +main(); +async function main() { + await runCmd(`for /f "delims=" %i in ('where flutter') do @cd /d "%~dpi" && git pull`); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\path_provider_test_10000.dart --machine > ..\\node_test_server\\${fileUrl}\\path_provider_test.txt`); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\key_input_test_10000.dart --machine > ..\\node_test_server\\${fileUrl}\\key_input_test.txt`); + await runCmd(`cd ..\\automated_testing_demo && flutter test .\\integration_test\\sqflite_test_10000.dart --machine > ..\\node_test_server\\${fileUrl}\\sqflite_test.txt`); + createXlsx(); +} + +function createFolder(dirName) { + try { + if (!fs.existsSync(path.resolve(__dirname, dirName))) { + fs.mkdirSync(path.resolve(__dirname, dirName), { recursive: true }); + } else { + } + } catch (err) { + console.error(err); + } +} + +// 生成表格 +function createXlsx() { + var time = new Date(); + var dirName = (time.getMonth() + 1) + '' + doubleStr(time.getDate()); + createFolder(dirName) + const workbook = new Excel.Workbook(); + files.forEach(async item => { + var fileStr = fs.readFileSync(fileUrl + '/' + item, 'utf8'); + const result = fomateTxt(fileStr) + const sheet = workbook.addWorksheet(item); + sheet.columns = [ + { header: '测试项', key: 'testName', width: 20 }, + { header: '结果', key: 'result', width: 10 }, + { header: '运行时间(ms)', key: 'difference', width: 10 }, + { header: '打印值', key: 'message', width: 50 }, + { header: '路径', key: 'testUrl', width: 50 }, + ]; + result.forEach(item => { + sheet.addRow(item) + }) + // 通过流发送文件 + }) + + workbook.xlsx.writeFile(dirName + '/' + doubleStr(time.getHours()) + '-' + doubleStr(time.getMinutes()) + '.xlsx'); +} +// 处理txt数据 +function fomateTxt(data) { + let result = []; + let resultMap = new Map(); + data.split('\n').forEach(item => { + if (/^\{.*\}$/.test(item)) { + const itemJson = JSON.parse(item); + if (itemJson.type === 'testStart') { + resultMap.set(itemJson.test.id, { ...itemJson, messageList: [] }); + } + if (itemJson.testID) { + const newItem = resultMap.get(itemJson.testID); + newItem.difference = itemJson.time - newItem.time + if (itemJson.type === 'testDone') { + newItem.result = itemJson.result; + } else if (itemJson.type === 'print') { + newItem.messageList.push(itemJson.message); + } + resultMap.set(itemJson.testID, newItem) + } + } + }) + for (let [key, mapValue] of resultMap) { + mapValue.testName = mapValue.test.name + mapValue.testUrl = mapValue.test.url + mapValue.message = mapValue.messageList.join('\n'); + result.push(mapValue); + } + return result; +} -- Gitee