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

Commit cb8e90e

Browse files
committed
实现sessionStorage注解解析
1 parent 5365a78 commit cb8e90e

File tree

3 files changed

+204
-12
lines changed

3 files changed

+204
-12
lines changed

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

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import net.sf.cglib.proxy.Enhancer;
2222
import net.sf.cglib.proxy.MethodInterceptor;
2323
import net.sf.cglib.proxy.MethodProxy;
24+
import org.openqa.selenium.WebDriver;
25+
import org.openqa.selenium.html5.SessionStorage;
26+
import org.openqa.selenium.html5.WebStorage;
2427
import org.suren.autotest.web.framework.annotation.AutoExpect;
2528
import org.suren.autotest.web.framework.annotation.AutoModule;
2629
import org.suren.autotest.web.framework.annotation.AutoSessionStorage;
@@ -30,10 +33,13 @@
3033
import org.suren.autotest.web.framework.report.RecordReportWriter;
3134
import org.suren.autotest.web.framework.report.record.ExceptionRecord;
3235
import org.suren.autotest.web.framework.report.record.NormalRecord;
36+
import org.suren.autotest.web.framework.util.PathUtil;
3337

38+
import java.io.*;
3439
import java.lang.reflect.Field;
3540
import java.lang.reflect.Method;
3641
import java.util.List;
42+
import java.util.Properties;
3743

3844
/**
3945
* 模块代理类
@@ -82,8 +88,10 @@ public Object intercept(Object obj, Method method, Object[] args,
8288

8389
try
8490
{
91+
SessionStorageConfig sessionStorageConfig = new SessionStorageConfig();
8592
if(autoSessionStorage != null)
8693
{
94+
sessionStorageConfig.setAutoLoad(true);
8795
Class<? extends Page> accountClz = autoSessionStorage.accountPage();
8896
String accountNameField = autoSessionStorage.accountName();
8997

@@ -96,10 +104,12 @@ public Object intercept(Object obj, Method method, Object[] args,
96104
if(value instanceof Text)
97105
{
98106
String accountNameValue = ((Text) value).getValue();
99-
100-
System.out.println("accountNameValue:" + accountNameValue);
101107

102-
loadSessionStorage(accountNameValue);
108+
if(loadSessionStorage(accountNameValue))
109+
{
110+
sessionStorageConfig.setAccount(accountNameValue);
111+
sessionStorageConfig.setSkipLogin(true);
112+
}
103113
}
104114
else
105115
{
@@ -108,7 +118,19 @@ public Object intercept(Object obj, Method method, Object[] args,
108118
}
109119
}
110120

111-
result = methodProxy.invokeSuper(obj, args);
121+
if(sessionStorageConfig.isSkipLogin())
122+
{
123+
result = Void.TYPE;
124+
}
125+
else
126+
{
127+
result = methodProxy.invokeSuper(obj, args);
128+
129+
if(sessionStorageConfig.isAutoLoad())
130+
{
131+
saveSessionStorage(sessionStorageConfig.getAccount());
132+
}
133+
}
112134

113135
normalRecord.setEndTime(System.currentTimeMillis());
114136

@@ -137,8 +159,58 @@ public Object intercept(Object obj, Method method, Object[] args,
137159
return result;
138160
}
139161

140-
private void loadSessionStorage(String accountNameValue)
162+
/**
163+
* 保存sessionStorage信息
164+
* @param account
165+
*/
166+
private void saveSessionStorage(String account)
167+
{
168+
WebDriver driver = util.getEngine().getDriver();
169+
if(driver instanceof WebStorage)
170+
{
171+
WebStorage webStorage = (WebStorage) driver;
172+
SessionStorage sessionStorage = webStorage.getSessionStorage();
173+
174+
Properties pro = new Properties();
175+
for(String key : sessionStorage.keySet())
176+
{
177+
pro.setProperty(key, sessionStorage.getItem(key));
178+
}
179+
180+
PathUtil.proStore(pro, "sessionStorage." + account);
181+
}
182+
}
183+
184+
/**
185+
* 加载sessionStorage信息
186+
* @param accountNameValue
187+
* @return
188+
*/
189+
private boolean loadSessionStorage(String accountNameValue)
141190
{
191+
WebDriver webDriver = util.getEngine().getDriver();
192+
if(webDriver instanceof WebStorage)
193+
{
194+
WebStorage webStorage = (WebStorage) webDriver;
195+
SessionStorage sessionStorage = webStorage.getSessionStorage();
196+
197+
Properties pro = new Properties();
198+
if(PathUtil.proLoad(pro, "sessionStorage." + accountNameValue))
199+
{
200+
if(pro.isEmpty())
201+
{
202+
return false;
203+
}
204+
205+
pro.stringPropertyNames().parallelStream().forEach((key) -> {
206+
sessionStorage.setItem(key, pro.getProperty(key));
207+
});
208+
209+
return true;
210+
}
211+
}
212+
213+
return false;
142214
}
143215

144216
/**
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 SessionStorageConfig
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+
}

src/main/java/org/suren/autotest/web/framework/util/PathUtil.java

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
*/
44
package org.suren.autotest.web.framework.util;
55

6-
import java.io.File;
7-
import java.io.FileNotFoundException;
8-
import java.io.FileOutputStream;
9-
import java.io.IOException;
10-
import java.io.InputStream;
11-
import java.io.OutputStream;
6+
import java.io.*;
7+
import java.util.Properties;
128

139
import org.apache.commons.io.IOUtils;
1410

@@ -17,8 +13,11 @@
1713
* @author suren
1814
* @date 2016年12月27日 上午8:12:18
1915
*/
20-
public class PathUtil
16+
public abstract class PathUtil
2117
{
18+
/** properties文件后缀 */
19+
public static final String PRO_SUFFIX = ".properties";
20+
2221
/**
2322
* @return 用于保存框架缓存数据的根目录,如果不存在会自动创建
2423
*/
@@ -33,6 +32,65 @@ public static File getRootDir()
3332

3433
return rootFile;
3534
}
35+
36+
/**
37+
* @see #proStore(Properties, String, String)
38+
* @param pro
39+
* @param fileName
40+
*/
41+
public static void proStore(Properties pro, String fileName)
42+
{
43+
proStore(pro, fileName, "auto generate by phoenix framework, do not modify it.");
44+
}
45+
46+
/**
47+
* 保存Properties文件到框架根目录中
48+
* @param pro 不能为空
49+
* @param fileName 无需带后缀,例如:demo的话,会保存到demo.properties中
50+
* @param comment 注释
51+
*/
52+
public static void proStore(Properties pro, String fileName, String comment)
53+
{
54+
try(OutputStream out = new FileOutputStream(
55+
new File(PathUtil.getRootDir(), fileName + PRO_SUFFIX)))
56+
{
57+
pro.store(out, comment);
58+
}
59+
catch (FileNotFoundException e)
60+
{
61+
e.printStackTrace();
62+
}
63+
catch (IOException e)
64+
{
65+
e.printStackTrace();
66+
}
67+
}
68+
69+
/**
70+
* @see #proStore(Properties, String, String)
71+
* @param pro 不能为空
72+
* @param fileName
73+
* @return 读取成功返回true,否则false
74+
*/
75+
public static boolean proLoad(Properties pro, String fileName)
76+
{
77+
try(InputStream in = new FileInputStream(new File(PathUtil.getRootDir(), fileName + PRO_SUFFIX)))
78+
{
79+
pro.load(in);
80+
81+
return true;
82+
}
83+
catch (FileNotFoundException e)
84+
{
85+
e.printStackTrace();
86+
}
87+
catch (IOException e)
88+
{
89+
e.printStackTrace();
90+
}
91+
92+
return false;
93+
}
3694

3795
/**
3896
* 如果存在,则跳过,不复制

0 commit comments

Comments
 (0)