Commit e265d4b
authored
Refactor element handling and browser interaction (#149)
* Refactor element handling and browser interaction
Replaced direct XPath-based element retrieval with a new `GetElement` method, supporting both `By` selectors and `Func<HtmlDocument, HtmlNode?>` delegates. Updated `Click` and `Input` methods to accept `IWebElement` directly, reducing redundancy and improving maintainability.
Removed deprecated `WaitPageLoaded` and `WaitPageChanged` methods in favor of more generic `Wait` methods. Enhanced error handling with detailed messages for `WebDriverTimeoutException`. Updated the `Retry` class to include more descriptive timeout errors.
Refactored all commands to use the new `GetElement` method and streamlined interaction logic. Removed redundant code and unused methods, improving readability and maintainability. Updated the `IChromeBrowser` interface to reflect these changes.
Performed general code cleanup, including removing unused variables, redundant comments, and unnecessary method calls.
* Enhance debugging and reliability in ChromeBrowser
- Added `[CallerArgumentExpression]` to `Wait` and `GetElement`
methods for improved error reporting and debugging.
- Updated `Wait` in `ChromeBrowser.cs` to include better error
handling and context in timeout exceptions.
- Modified `Click` to include a `Wait` call for ensuring specific
conditions (e.g., visibility of the `logo` element).
- Added a new `GetElement` overload in `IChromeBrowser.cs`
supporting `By` locators with `[CallerArgumentExpression]`.
- Updated `Wait` signature in `IChromeBrowser.cs` to align with
the implementation in `ChromeBrowser.cs`.
- Introduced `IDelayService` in `LoginTask.cs` to add delays
for smoother execution during the login process.
- Improved maintainability and reliability across the codebase
by enhancing error handling and debugging capabilities.
* Add WaitPageChanged method for reliable page transitions
Introduced a reusable `WaitPageChanged` method in `ChromeBrowser.cs`
and `IChromeBrowser.cs` to handle URL changes and ensure pages are
fully loaded. Updated various commands (`LoginCommand.cs`,
`HandleUpgradeCommand.cs`, `ToBuildingByLocationCommand.cs`,
`ToDorfCommand.cs`) to use this method after navigation actions.
Removed redundant `Wait` logic from the `Click` method in
`ChromeBrowser.cs`, consolidating it into `WaitPageChanged` for
better modularity. Enhanced error handling in `WaitPageChanged`
to provide detailed messages for failures.
* Refactor task handling in BuildingsModified and JobsModified
Refactored the `BuildingsModified` method to improve readability
by reordering early exit checks and command executions.
In the `JobsModified` method, moved `_taskManager.AddOrUpdate`
for `UpgradeBuildingTask.Task` to the beginning of the method
to ensure proper task handling before other operations.
Removed redundant `_taskManager.AddOrUpdate` call at the end
of `JobsModified` to eliminate duplication and improve clarity.
* Refactor Wait method and improve error message
Simplified the `Wait` method call by condensing the lambda
expression into a single line for better readability. Enhanced
the error message in the `if (result.IsFailed)` block to include
the `CurrentUrl` property, providing additional context for
debugging when the URL change wait fails.
* fix new building on military&resource tab cannot be built
* Refactored the retrieval of the "train button" element to use an
asynchronous `GetElement` operation with a document parser
(`TrainTroopParser.GetTrainButton`) and a cancellation token.
* Refactor train button retrieval logic
Replaced the old `TrainTroopParser.GetTrainButton` logic with a more robust approach using `browser.GetElement` and a lambda function.
* Refactor error handling and improve logging
Enhanced error messages across the codebase for better clarity.
Refactored the `Retry` class to simplify and standardize error
handling using a new `Retry.Error` property. Introduced the
`WithError` method for chaining additional error context.
Improved logging to provide more context during execution,
including adventure details and specific failure points.
Simplified XPath handling by using `nodeGenerator` functions
for better readability. Updated tab index validation logic
in `SwitchTabCommand.cs` to ensure accurate error reporting.
Refactored exception handling in `ChromeBrowser` to use the
new `Retry.Error` mechanism. Removed redundant error-handling
methods and standardized error reporting with the
`Result.Fail(errors).WithError(...)` pattern.
Made namespace-specific changes to ensure consistency in
error handling and logging. Improved debugging support and
code readability by reducing redundancy and adding detailed
error messages. Added additional wait logic to enhance
automation reliability.
* Refactor `Skip` error handling for dynamic messages
Refactored the `Skip` class to replace static error instances with a generic `Skip.Error` that supports dynamic error messages via the `WithError` method. Updated all references to use the new approach, enabling more descriptive and context-specific error handling. Removed redundant static `Skip` error instances to improve maintainability and reduce code duplication.
* Refactor error handling for consistency
Refactored the error handling mechanism across the codebase to use
`WithError` and `WithErrors` methods for detailed and extensible
error messages.
- Replaced specific `Stop` error methods with a generic `Stop.Error`
instance, allowing customization via `WithError`.
- Made `Stop` constructor private to enforce the use of `Error`.
- Updated `Skip` to include a default message and support `WithErrors`.
- Replaced hardcoded error messages in various files with the new
`WithError` and `WithErrors` methods for better clarity.
- Removed redundant error methods like `JobNotAvailable` in
`UpgradeBuildingError`.
- Improved error propagation in `NPCTask` and `UpgradeBuildingTask`.
These changes improve code maintainability and ensure consistent,
user-friendly error messages throughout the application.1 parent 950b401 commit e265d4b
37 files changed
Lines changed: 293 additions & 316 deletions
File tree
- MainCore.Test/Behaviors
- MainCore
- Behaviors
- Commands
- Features
- ClaimQuest
- CompleteImmediately
- DisableContextualHelp
- NpcResource
- StartAdventure
- StartFarmList
- TrainTroop
- UpgradeBuilding
- UseHeroItem
- Misc
- Update
- Errors
- Services
- Tasks
- UI/ViewModels/Tabs/Villages
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
70 | | - | |
71 | 69 | | |
72 | 70 | | |
73 | 71 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
Lines changed: 12 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
41 | 48 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
45 | 53 | | |
46 | 54 | | |
47 | 55 | | |
| |||
Lines changed: 6 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 27 | + | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
| |||
Lines changed: 6 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
| 25 | + | |
| 26 | + | |
37 | 27 | | |
38 | | - | |
| 28 | + | |
39 | 29 | | |
40 | 30 | | |
41 | 31 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
| 22 | + | |
| 23 | + | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | 16 | | |
24 | 17 | | |
25 | 18 | | |
26 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
27 | 23 | | |
28 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
29 | 28 | | |
30 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
Lines changed: 15 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
78 | | - | |
79 | 77 | | |
80 | 78 | | |
81 | 79 | | |
| |||
116 | 114 | | |
117 | 115 | | |
118 | 116 | | |
119 | | - | |
120 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
| |||
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | 130 | | |
133 | 131 | | |
134 | 132 | | |
| |||
141 | 139 | | |
142 | 140 | | |
143 | 141 | | |
144 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
| |||
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 187 | + | |
| 188 | + | |
192 | 189 | | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
| 190 | + | |
202 | 191 | | |
203 | 192 | | |
204 | 193 | | |
205 | 194 | | |
206 | 195 | | |
207 | 196 | | |
208 | 197 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
| 198 | + | |
| 199 | + | |
224 | 200 | | |
225 | | - | |
| 201 | + | |
226 | 202 | | |
227 | 203 | | |
228 | 204 | | |
| |||
Lines changed: 9 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
22 | 29 | | |
23 | 30 | | |
24 | 31 | | |
25 | 32 | | |
26 | 33 | | |
27 | 34 | | |
28 | 35 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | 36 | | |
34 | 37 | | |
35 | | - | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| |||
0 commit comments