1
1
using OpenQA . Selenium ;
2
+ using OpenQA . Selenium . Support . UI ;
2
3
using System ;
3
4
using System . Threading . Tasks ;
4
5
using TbsCore . Models . AccModels ;
@@ -86,13 +87,17 @@ public static string GetBearerToken(Account acc, bool log = true)
86
87
87
88
/// <summary>
88
89
/// Write coordinates into the number inputs. Used when sending troops, resources etc.
90
+ /// If coordinates are already there (embedded in url), skip this task.
89
91
/// </summary>
90
92
internal static async Task WriteCoordinates ( Account acc , Coordinates coordinates )
91
93
{
92
- await WriteById ( acc , "xCoordInput" , coordinates . x ) ;
93
- await WriteById ( acc , "yCoordInput" , coordinates . y ) ;
94
+ if ( string . IsNullOrEmpty ( acc . Wb . Html . GetElementbyId ( "xCoordInput" ) . GetAttributeValue ( "value" , "" ) ) )
95
+ await WriteById ( acc , "xCoordInput" , coordinates . x ) ;
96
+ if ( string . IsNullOrEmpty ( acc . Wb . Html . GetElementbyId ( "yCoordInput" ) . GetAttributeValue ( "value" , "" ) ) )
97
+ await WriteById ( acc , "yCoordInput" , coordinates . y ) ;
94
98
}
95
99
100
+ #region By Id
96
101
public static async Task < bool > ClickById ( Account acc , string query , bool log = true ) =>
97
102
await ExecuteAction ( acc , new QueryById ( query ) , new ActionClick ( ) , log ) ;
98
103
@@ -104,50 +109,89 @@ public static async Task<bool> CheckById(Account acc, string query, bool check,
104
109
105
110
public static async Task < bool > SelectIndexById ( Account acc , string query , int index , bool log = true ) =>
106
111
await ExecuteAction ( acc , new QueryById ( query ) , new ActionSelectIndex ( index ) , log ) ;
112
+ #endregion
107
113
108
- public static async Task < bool > ClickByClassName ( Account acc , string query , bool log = true ) =>
109
- await ExecuteAction ( acc , new QueryByClassName ( query ) , new ActionClick ( ) , log ) ;
114
+ #region By Class Name
110
115
111
- public static async Task < bool > WriteByClassName ( Account acc , string query , object text , bool log = true ) =>
112
- await ExecuteAction ( acc , new QueryByClassName ( query ) , new ActionWrite ( text ) , log ) ;
116
+ public static async Task < bool > ClickByClassName ( Account acc , string query , int qindex = 0 , bool log = true ) =>
117
+ await ExecuteAction ( acc , new QueryByClassName ( query , qindex ) , new ActionClick ( ) , log ) ;
113
118
114
- public static async Task < bool > CheckByClassName ( Account acc , string query , bool check , bool log = true ) =>
115
- await ExecuteAction ( acc , new QueryByClassName ( query ) , new ActionCheck ( check ) , log ) ;
119
+ public static async Task < bool > WriteByClassName ( Account acc , string query , object text , int qindex = 0 , bool log = true ) =>
120
+ await ExecuteAction ( acc , new QueryByClassName ( query , qindex ) , new ActionWrite ( text ) , log ) ;
116
121
117
- public static async Task < bool > SelectIndexByClassName ( Account acc , string query , int index , bool log = true ) =>
118
- await ExecuteAction ( acc , new QueryByClassName ( query ) , new ActionSelectIndex ( index ) , log ) ;
122
+ public static async Task < bool > CheckByClassName ( Account acc , string query , bool check , int qindex = 0 , bool log = true ) =>
123
+ await ExecuteAction ( acc , new QueryByClassName ( query , qindex ) , new ActionCheck ( check ) , log ) ;
119
124
120
- public static async Task < bool > ClickByName ( Account acc , string query , bool log = true ) =>
121
- await ExecuteAction ( acc , new QueryByName ( query ) , new ActionClick ( ) , log ) ;
125
+ public static async Task < bool > SelectIndexByClassName ( Account acc , string query , int index , int qindex = 0 , bool log = true ) =>
126
+ await ExecuteAction ( acc , new QueryByClassName ( query , qindex ) , new ActionSelectIndex ( index ) , log ) ;
127
+ #endregion
122
128
123
- public static async Task < bool > WriteByName ( Account acc , string query , object text , bool log = true , bool update = true ) =>
124
- await ExecuteAction ( acc , new QueryByName ( query ) , new ActionWrite ( text ) , log , update ) ;
129
+ #region By Name
125
130
126
- public static async Task < bool > CheckByName ( Account acc , string query , bool check , bool log = true ) =>
127
- await ExecuteAction ( acc , new QueryByName ( query ) , new ActionCheck ( check ) , log ) ;
131
+ public static async Task < bool > ClickByName ( Account acc , string query , int qindex = 0 , bool log = true ) =>
132
+ await ExecuteAction ( acc , new QueryByName ( query , qindex ) , new ActionClick ( ) , log ) ;
128
133
129
- public static async Task < bool > SelectIndexByName ( Account acc , string query , int index , bool log = true ) =>
130
- await ExecuteAction ( acc , new QueryByName ( query ) , new ActionSelectIndex ( index ) , log ) ;
134
+ public static async Task < bool > WriteByName ( Account acc , string query , object text , int qindex = 0 , bool log = true ) =>
135
+ await ExecuteAction ( acc , new QueryByName ( query , qindex ) , new ActionWrite ( text ) , log ) ;
136
+
137
+ public static async Task < bool > CheckByName ( Account acc , string query , bool check , int qindex = 0 , bool log = true ) =>
138
+ await ExecuteAction ( acc , new QueryByName ( query , qindex ) , new ActionCheck ( check ) , log ) ;
139
+
140
+ public static async Task < bool > SelectIndexByName ( Account acc , string query , int index , int qindex = 0 , bool log = true ) =>
141
+ await ExecuteAction ( acc , new QueryByName ( query , qindex ) , new ActionSelectIndex ( index ) , log ) ;
142
+ #endregion
143
+
144
+ #region By Attribute Value
145
+ public static async Task < bool > ClickByAttributeValue ( Account acc , string attribute , string value , bool log = true ) =>
146
+ await ExecuteAction ( acc , new QueryByAttributeVal ( attribute , value ) , new ActionClick ( ) , log ) ;
147
+
148
+ public static async Task < bool > WriteByAttributeValue ( Account acc , string attribute , string value , object text , bool log = true , bool update = true ) =>
149
+ await ExecuteAction ( acc , new QueryByAttributeVal ( attribute , value ) , new ActionWrite ( text ) , log , update ) ;
150
+
151
+ public static async Task < bool > CheckByAttributeValue ( Account acc , string attribute , string value , bool check , bool log = true ) =>
152
+ await ExecuteAction ( acc , new QueryByAttributeVal ( attribute , value ) , new ActionCheck ( check ) , log ) ;
153
+
154
+ public static async Task < bool > SelectByAttributeValue ( Account acc , string attribute , string value , int index , bool log = true ) =>
155
+ await ExecuteAction ( acc , new QueryByAttributeVal ( attribute , value ) , new ActionSelectIndex ( index ) , log ) ;
156
+ #endregion
131
157
132
158
private static async Task < bool > ExecuteAction ( Account acc , Query query , Action action , bool log = true , bool update = true ) =>
133
159
await ExecuteScript ( acc , $ "document.{ query . val } { action . val } ", log , update ) ;
134
160
135
- public class QueryById : Query { public QueryById ( string str ) => base . val = $ "getElementById('{ str } ')"; }
161
+ public class QueryById : Query
162
+ { public QueryById ( string str ) => base . val = $ "getElementById('{ str } ')"; }
163
+
164
+ public class QueryByName : Query
165
+ { public QueryByName ( string str , int index = 0 ) => base . val = $ "getElementsByName('{ str } ')[{ index } ]"; }
136
166
137
- public class QueryByName : Query { public QueryByName ( string str ) => base . val = $ "getElementsByName('{ str } ')[0]"; }
167
+ public class QueryByClassName : Query
168
+ { public QueryByClassName ( string str , int index = 0 ) => base . val = $ "getElementsByClassName('{ str } ')[{ index } ]"; }
138
169
139
- public class QueryByClassName : Query { public QueryByClassName ( string str ) => base . val = $ "getElementsByClassName('{ str } ')[0]"; }
170
+ public class QueryByAttributeVal : Query
171
+ { public QueryByAttributeVal ( string attribute , string value ) => base . val = $ "querySelectorAll('[{ attribute } =\" { value } \" ]')[0]"; }
140
172
141
- public class ActionWrite : Action { public ActionWrite ( object str ) => base . val = $ ".value='{ str } ';"; }
142
173
143
- public class ActionClick : Action { public ActionClick ( ) => base . val = ".click();" ; }
174
+ public class ActionWrite : Action
175
+ { public ActionWrite ( object str ) => base . val = $ ".value='{ str } ';"; }
144
176
145
- public class ActionCheck : Action { public ActionCheck ( bool check ) => base . val = $ ".checked={ ( check ? "true" : "false" ) } ;"; }
177
+ public class ActionClick : Action
178
+ { public ActionClick ( ) => base . val = ".click();" ; }
179
+ public class ActionCheck : Action
180
+ { public ActionCheck ( bool check ) => base . val = $ ".checked={ ( check ? "true" : "false" ) } ;"; }
146
181
147
- public class ActionSelectIndex : Action { public ActionSelectIndex ( int index ) => base . val = $ ".selectedIndex = { index } ;"; }
182
+ public class ActionSelectIndex : Action
183
+ { public ActionSelectIndex ( int index ) => base . val = $ ".selectedIndex = { index } ;"; }
148
184
149
185
public abstract class Action { public string val ; }
150
186
151
187
public abstract class Query { public string val ; }
188
+
189
+ public static async Task WaitLoaded ( Account acc , int delay = 15 )
190
+ {
191
+ var wait = new WebDriverWait ( acc . Wb . Driver , TimeSpan . FromSeconds ( delay ) ) ;
192
+ wait . Until ( driver => ( ( IJavaScriptExecutor ) driver ) . ExecuteScript ( "return document.readyState" ) . Equals ( "complete" ) ) ;
193
+ acc . Wb . UpdateHtml ( ) ;
194
+ await TaskExecutor . PageLoaded ( acc ) ;
195
+ }
152
196
}
153
197
}
0 commit comments