Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 080ee65

Browse files
committed
增加localStorage的处理
1 parent 14194c5 commit 080ee65

File tree

5 files changed

+258
-13
lines changed

5 files changed

+258
-13
lines changed

src/main/java/org/suren/autotest/web/framework/annotation/AutoCookie.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,31 @@
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
88

9+
import org.suren.autotest.web.framework.page.Page;
10+
911
/**
1012
* @author suren
1113
* @date 2017年6月26日 上午10:17:00
1214
*/
13-
@Target(ElementType.TYPE)
15+
@Target(ElementType.METHOD)
1416
@Retention(RetentionPolicy.RUNTIME)
1517
@Documented
1618
public @interface AutoCookie
1719
{
20+
/**
21+
* @return Page类
22+
*/
23+
Class<? extends Page> pageClazz();
1824

1925
/**
20-
* @return 是否跳过目标方法
26+
* @return Page类中代表的属性名称
2127
*/
22-
boolean skipMethod() default false;
28+
String sessionKey();
2329

2430
/**
25-
* @return 是否要执行打开页面操作
31+
* @return 是否跳过目标方法
2632
*/
27-
boolean openInvoke() default false;
33+
boolean skipMethod() default false;
2834

2935
/**
3036
* @return 保存cookie的文件名,在~/.autotest中
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2002-2007 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.suren.autotest.web.framework.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.suren.autotest.web.framework.page.Page;
26+
27+
/**
28+
* @author suren
29+
* @date 2017年6月30日 下午2:51:25
30+
*/
31+
@Target(ElementType.METHOD)
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Documented
34+
public @interface AutoLocalStorage
35+
{
36+
/**
37+
* @return Page类
38+
*/
39+
Class<? extends Page> pageClazz();
40+
41+
/**
42+
* @return Page类中代表的属性名称
43+
*/
44+
String sessionKey();
45+
46+
/**
47+
* @return 自定义选项,将会覆盖了localStorage中的选项
48+
*/
49+
AutoItem[] overItems() default {};
50+
51+
/**
52+
* @return 是否跳过目标方法
53+
*/
54+
boolean skipMethod() default true;
55+
}

src/main/java/org/suren/autotest/web/framework/annotation/AutoSessionStorage.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,4 @@
5454
* @return 是否跳过目标方法
5555
*/
5656
boolean skipMethod() default true;
57-
58-
/**
59-
* @return 是否要执行打开页面操作
60-
*/
61-
boolean openInvoke() default false;
6257
}

src/main/java/org/suren/autotest/web/framework/settings/AutoModuleProxy.java

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.lang.reflect.Field;
2828
import java.lang.reflect.Method;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
31+
import java.util.HashMap;
3032
import java.util.List;
3133
import java.util.Map;
3234
import java.util.Properties;
@@ -35,10 +37,13 @@
3537
import org.openqa.selenium.Cookie;
3638
import org.openqa.selenium.WebDriver;
3739
import org.openqa.selenium.WebDriver.Options;
40+
import org.openqa.selenium.html5.LocalStorage;
3841
import org.openqa.selenium.html5.SessionStorage;
3942
import org.openqa.selenium.html5.WebStorage;
4043
import org.suren.autotest.web.framework.annotation.AutoCookie;
4144
import 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;
4247
import org.suren.autotest.web.framework.annotation.AutoModule;
4348
import org.suren.autotest.web.framework.annotation.AutoSessionStorage;
4449
import 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
* 根据注解配置,是否要对异常进行处理
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
*
3+
* * Copyright 2002-2007 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * http://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package org.suren.autotest.web.framework.settings;
20+
21+
/**
22+
* @author suren
23+
*/
24+
public class LocalStorageConfig
25+
{
26+
/** 跳过登录 */
27+
private boolean skipLogin;
28+
/** 自动加载 */
29+
private boolean autoLoad;
30+
/** 账户名 */
31+
private String account;
32+
33+
public boolean isSkipLogin()
34+
{
35+
return skipLogin;
36+
}
37+
38+
public void setSkipLogin(boolean skipLogin)
39+
{
40+
this.skipLogin = skipLogin;
41+
}
42+
43+
public boolean isAutoLoad()
44+
{
45+
return autoLoad;
46+
}
47+
48+
public void setAutoLoad(boolean autoLoad)
49+
{
50+
this.autoLoad = autoLoad;
51+
}
52+
53+
public String getAccount()
54+
{
55+
return account;
56+
}
57+
58+
public void setAccount(String account)
59+
{
60+
this.account = account;
61+
}
62+
}

0 commit comments

Comments
 (0)