-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathreq_helper.dart
141 lines (127 loc) · 5.06 KB
/
req_helper.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 'package:apidash_core/apidash_core.dart';
import 'package:apidash_design_system/apidash_design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:spot/spot.dart';
import 'package:apidash/consts.dart';
import 'package:apidash/widgets/widgets.dart';
import 'package:apidash/screens/common_widgets/common_widgets.dart';
import 'package:apidash/screens/home_page/collection_pane.dart';
import 'package:apidash/screens/home_page/editor_pane/url_card.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_params.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_headers.dart';
class ApidashTestRequestHelper {
final WidgetTester tester;
ApidashTestRequestHelper(this.tester);
Future<void> addNewRequest({bool isMobile = false}) async {
if (isMobile) {
kHomeScaffoldKey.currentState!.openDrawer();
await tester.pumpAndSettle();
}
await act.tap(
spot<CollectionPane>().spot<ElevatedButton>().spotText(kLabelPlusNew));
await tester.pumpAndSettle();
}
Future<void> renameNewRequest(String newReqName) async {
Finder reqItems = find.byType(RequestItem);
Finder newReqItem = reqItems.at(0);
expect(find.descendant(of: newReqItem, matching: find.text(kUntitled)),
findsOneWidget);
Finder itemCardMenu =
find.descendant(of: newReqItem, matching: find.byType(ItemCardMenu));
await tester.tap(itemCardMenu);
await tester.pumpAndSettle();
await tester.tap(find.text(ItemMenuOption.edit.label).last);
await tester.pump();
await tester.enterText(newReqItem, newReqName);
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
}
Future<void> addRequestURL(String url) async {
await act.tap(spot<URLTextField>());
tester.testTextInput.enterText(url);
}
Future<void> setRequestMethod(HTTPVerb method) async {
final methodDropdown = spot<DropdownButtonHTTPMethod>();
await act.tap(methodDropdown);
await tester.pumpAndSettle();
await tester.tap(find.text(method.name.toUpperCase()).last);
await tester.pumpAndSettle();
}
Future<void> addRequestParams(List<(String, String)> keyValuePairs) async {
final paramTabButton =
spot<RequestPane>().spot<TabBar>().spotText(kLabelURLParams);
await act.tap(paramTabButton);
await tester.pumpAndSettle();
var paramCells = find.descendant(
of: find.byType(EditRequestURLParams),
matching: find.byType(EnvCellField));
for (var i = 0; i < keyValuePairs.length; i++) {
await tester.tap(paramCells.at(i));
tester.testTextInput.enterText(keyValuePairs[i].$1);
await tester.tap(paramCells.at(i + 1));
tester.testTextInput.enterText(keyValuePairs[i].$2);
paramCells = find.descendant(
of: find.byType(EditRequestURLParams),
matching: find.byType(EnvCellField));
}
}
Future<void> addRequestHeaders(List<(String, String)> keyValuePairs) async {
final headerTabButton =
spot<RequestPane>().spot<TabBar>().spotText(kLabelHeaders);
await act.tap(headerTabButton);
await tester.pumpAndSettle();
var headerCells = find.descendant(
of: find.byType(EditRequestHeaders),
matching: find.byType(HeaderField));
var valueCells = find.descendant(
of: find.byType(EditRequestHeaders),
matching: find.byType(EnvCellField));
for (var i = 0; i < keyValuePairs.length; i++) {
await tester.tap(headerCells.at(i));
tester.testTextInput.enterText(keyValuePairs[i].$1);
await tester.tap(valueCells.at(i));
tester.testTextInput.enterText(keyValuePairs[i].$2);
headerCells = find.descendant(
of: find.byType(EditRequestHeaders),
matching: find.byType(HeaderField));
valueCells = find.descendant(
of: find.byType(EditRequestHeaders),
matching: find.byType(EnvCellField));
}
}
Future<void> unCheckFirstHeader() async {
final headerCells = find.descendant(
of: find.byType(EditRequestHeaders), matching: find.byType(ADCheckBox));
await tester.tap(headerCells.at(0));
await tester.pumpAndSettle();
}
Future<void> addRequest(
String url, {
String? name,
HTTPVerb method = HTTPVerb.get,
List<(String, String)> params = const [],
List<(String, String)> headers = const [],
bool isMobile = false,
}) async {
await addNewRequest(isMobile: isMobile);
if (name != null) {
await renameNewRequest(name);
}
if (isMobile) {
kHomeScaffoldKey.currentState!.closeDrawer();
await tester.pumpAndSettle();
}
if (method != HTTPVerb.get) {
await setRequestMethod(method);
}
await addRequestURL(url);
if (params.isNotEmpty) await addRequestParams(params);
if (headers.isNotEmpty) await addRequestHeaders(headers);
}
Future<void> sendRequest(
{Duration stallTime = const Duration(seconds: 3)}) async {
await act.tap(spot<SendRequestButton>());
await tester.pumpAndSettle(stallTime);
}
}