2727import java .lang .reflect .Field ;
2828import java .lang .reflect .Method ;
2929import java .util .ArrayList ;
30+ import java .util .Arrays ;
31+ import java .util .HashMap ;
3032import java .util .List ;
3133import java .util .Map ;
3234import java .util .Properties ;
3537import org .openqa .selenium .Cookie ;
3638import org .openqa .selenium .WebDriver ;
3739import org .openqa .selenium .WebDriver .Options ;
40+ import org .openqa .selenium .html5 .LocalStorage ;
3841import org .openqa .selenium .html5 .SessionStorage ;
3942import org .openqa .selenium .html5 .WebStorage ;
4043import org .suren .autotest .web .framework .annotation .AutoCookie ;
4144import org .suren .autotest .web .framework .annotation .AutoExpect ;
45+ import org .suren .autotest .web .framework .annotation .AutoItem ;
46+ import org .suren .autotest .web .framework .annotation .AutoLocalStorage ;
4247import org .suren .autotest .web .framework .annotation .AutoModule ;
4348import org .suren .autotest .web .framework .annotation .AutoSessionStorage ;
4449import org .suren .autotest .web .framework .core .AutoTestException ;
@@ -92,6 +97,7 @@ public Object intercept(Object obj, Method method, Object[] args,
9297 AutoModule autoModule = superCls .getAnnotation (AutoModule .class );
9398 AutoExpect autoExpect = method .getAnnotation (AutoExpect .class );
9499 AutoSessionStorage autoSessionStorage = method .getAnnotation (AutoSessionStorage .class );
100+ AutoLocalStorage autoLocalStorage = method .getAnnotation (AutoLocalStorage .class );
95101 AutoCookie autoCookie = method .getAnnotation (AutoCookie .class );
96102
97103 NormalRecord normalRecord = new NormalRecord ();
@@ -120,9 +126,18 @@ public Object intercept(Object obj, Method method, Object[] args,
120126 {
121127 String accountNameValue = ((Text ) value ).getValue ();
122128 sessionStorageConfig .setAccount (accountNameValue );
129+
130+ Map <String , String > customMap = new HashMap <String , String >();
131+ AutoItem [] overItems = autoSessionStorage .overItems ();
132+ if (overItems != null && overItems .length > 0 )
133+ {
134+ Arrays .asList (overItems ).forEach ((item ) -> {
135+ customMap .put (item .key (), item .value ());
136+ });
137+ }
123138
124139 page .open ();
125- if (loadSessionStorage (accountNameValue ))
140+ if (loadSessionStorage (accountNameValue , customMap ))
126141 {
127142 sessionStorageConfig .setAccount (accountNameValue );
128143
@@ -139,6 +154,51 @@ public Object intercept(Object obj, Method method, Object[] args,
139154 }
140155 }
141156
157+ LocalStorageConfig localStorageConfig = new LocalStorageConfig ();
158+ if (autoLocalStorage != null )
159+ {
160+ localStorageConfig .setAutoLoad (true );
161+ Class <? extends Page > accountClz = autoLocalStorage .pageClazz ();
162+ String accountNameField = autoLocalStorage .sessionKey ();
163+
164+ Page page = util .getPage (accountClz );
165+ Field accountField = accountClz .getDeclaredField (accountNameField );
166+
167+ accountField .setAccessible (true );
168+ Object value = accountField .get (page );
169+
170+ if (value instanceof Text )
171+ {
172+ String accountNameValue = ((Text ) value ).getValue ();
173+ localStorageConfig .setAccount (accountNameValue );
174+
175+ Map <String , String > customMap = new HashMap <String , String >();
176+ AutoItem [] overItems = autoLocalStorage .overItems ();
177+ if (overItems != null && overItems .length > 0 )
178+ {
179+ Arrays .asList (overItems ).forEach ((item ) -> {
180+ customMap .put (item .key (), item .value ());
181+ });
182+ }
183+
184+ page .open ();
185+ if (loadLocalStorage (accountNameValue , customMap ))
186+ {
187+ localStorageConfig .setAccount (accountNameValue );
188+
189+ if (autoLocalStorage .skipMethod ())
190+ {
191+ localStorageConfig .setSkipLogin (true );
192+ }
193+ }
194+ }
195+ else
196+ {
197+ throw new AutoTestException ("Wrong account type in class: " + accountClz + ", and field : "
198+ + accountNameField + ". It should be Text type." );
199+ }
200+ }
201+
142202 //加载cookie信息
143203 boolean skipForCookie = false ;
144204 if (autoCookie != null && PathUtil .isFile (autoCookie .fileName ()))
@@ -147,6 +207,19 @@ public Object intercept(Object obj, Method method, Object[] args,
147207 Options manage = util .getEngine ().getDriver ().manage ();
148208 File cookieFile = PathUtil .getFile (autoCookie .fileName ());
149209
210+ Class <? extends Page > accountClz = autoCookie .pageClazz ();
211+ String accountNameField = autoCookie .sessionKey ();
212+
213+ Page page = util .getPage (accountClz );
214+ Field accountField = accountClz .getDeclaredField (accountNameField );
215+
216+ accountField .setAccessible (true );
217+ Object value = accountField .get (page );
218+ if (value instanceof Text )
219+ {
220+ page .open ();
221+ }
222+
150223 try (ObjectInputStream input = new ObjectInputStream (new FileInputStream (cookieFile )))
151224 {
152225 Object cookiesObj = input .readObject ();
@@ -171,7 +244,7 @@ public Object intercept(Object obj, Method method, Object[] args,
171244 }
172245 }
173246
174- if (sessionStorageConfig .isSkipLogin () || skipForCookie )
247+ if (sessionStorageConfig .isSkipLogin () || localStorageConfig . isSkipLogin () || skipForCookie )
175248 {
176249 result = Void .TYPE ;
177250 }
@@ -185,6 +258,11 @@ public Object intercept(Object obj, Method method, Object[] args,
185258 {
186259 saveSessionStorage (sessionStorageConfig .getAccount ());
187260 }
261+
262+ if (localStorageConfig .isAutoLoad ())
263+ {
264+ saveLocalStorage (localStorageConfig .getAccount ());
265+ }
188266
189267 //保存cookie信息
190268 if (autoCookie != null )
@@ -251,13 +329,31 @@ private void saveSessionStorage(String account)
251329 PathUtil .proStore (pro , "sessionStorage." + account );
252330 }
253331 }
332+
333+ private void saveLocalStorage (String account )
334+ {
335+ WebDriver driver = util .getEngine ().getDriver ();
336+ if (driver instanceof WebStorage )
337+ {
338+ WebStorage webStorage = (WebStorage ) driver ;
339+ LocalStorage localStorage = webStorage .getLocalStorage ();
340+
341+ Properties pro = new Properties ();
342+ for (String key : localStorage .keySet ())
343+ {
344+ pro .setProperty (key , localStorage .getItem (key ));
345+ }
346+
347+ PathUtil .proStore (pro , "localStorage." + account );
348+ }
349+ }
254350
255351 /**
256352 * 加载sessionStorage信息
257353 * @param accountNameValue
258354 * @return
259355 */
260- private boolean loadSessionStorage (String accountNameValue )
356+ private boolean loadSessionStorage (String accountNameValue , Map < String , String > customMap )
261357 {
262358 WebDriver webDriver = util .getEngine ().getDriver ();
263359 if (webDriver instanceof WebStorage )
@@ -272,6 +368,8 @@ private boolean loadSessionStorage(String accountNameValue)
272368 {
273369 return false ;
274370 }
371+
372+ pro .putAll (customMap );
275373
276374 pro .stringPropertyNames ().parallelStream ().forEach ((key ) -> {
277375 sessionStorage .setItem (key , pro .getProperty (key ));
@@ -283,6 +381,35 @@ private boolean loadSessionStorage(String accountNameValue)
283381
284382 return false ;
285383 }
384+
385+ private boolean loadLocalStorage (String accountNameValue , Map <String , String > customMap )
386+ {
387+ WebDriver webDriver = util .getEngine ().getDriver ();
388+ if (webDriver instanceof WebStorage )
389+ {
390+ WebStorage webStorage = (WebStorage ) webDriver ;
391+ LocalStorage localStorage = webStorage .getLocalStorage ();
392+
393+ Properties pro = new Properties ();
394+ if (PathUtil .proLoad (pro , "localStorage." + accountNameValue ))
395+ {
396+ if (pro .isEmpty ())
397+ {
398+ return false ;
399+ }
400+
401+ pro .putAll (customMap );
402+
403+ pro .stringPropertyNames ().parallelStream ().forEach ((key ) -> {
404+ localStorage .setItem (key , pro .getProperty (key ));
405+ });
406+
407+ return true ;
408+ }
409+ }
410+
411+ return false ;
412+ }
286413
287414 /**
288415 * 根据注解配置,是否要对异常进行处理
0 commit comments