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

Commit 5365a78

Browse files
committed
增加读写sessionStorage的注解
1 parent e54eaae commit 5365a78

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.annotation;
20+
21+
import org.suren.autotest.web.framework.page.Page;
22+
23+
import java.lang.annotation.*;
24+
25+
/**
26+
* 用在登录方法上,第一次登录时,保存session信息;之后登录方法被调用时则会跳过
27+
* @author suren
28+
*/
29+
@Target(ElementType.METHOD)
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Documented
32+
public @interface AutoSessionStorage
33+
{
34+
/**
35+
* @return 用户登录Page类
36+
*/
37+
Class<? extends Page> accountPage();
38+
39+
/**
40+
* @return Page类中代表用户名的属性名称
41+
*/
42+
String accountName() default "userName";
43+
}

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
import net.sf.cglib.proxy.MethodProxy;
2424
import org.suren.autotest.web.framework.annotation.AutoExpect;
2525
import org.suren.autotest.web.framework.annotation.AutoModule;
26+
import org.suren.autotest.web.framework.annotation.AutoSessionStorage;
27+
import org.suren.autotest.web.framework.core.AutoTestException;
28+
import org.suren.autotest.web.framework.core.ui.Text;
29+
import org.suren.autotest.web.framework.page.Page;
2630
import org.suren.autotest.web.framework.report.RecordReportWriter;
2731
import org.suren.autotest.web.framework.report.record.ExceptionRecord;
2832
import org.suren.autotest.web.framework.report.record.NormalRecord;
2933

34+
import java.lang.reflect.Field;
3035
import java.lang.reflect.Method;
3136
import java.util.List;
3237

@@ -39,11 +44,13 @@ public class AutoModuleProxy implements MethodInterceptor
3944
private Enhancer enhancer = new Enhancer();
4045
private Object target;
4146
private List<RecordReportWriter> recordReportWriters;
47+
private SettingUtil util;
4248

43-
public AutoModuleProxy(Object target, List<RecordReportWriter> recordReportWriters)
49+
public AutoModuleProxy(Object target, List<RecordReportWriter> recordReportWriters, SettingUtil util)
4450
{
4551
this.target = target;
4652
this.recordReportWriters = recordReportWriters;
53+
this.util = util;
4754
}
4855

4956
public Object getProxy()
@@ -64,6 +71,7 @@ public Object intercept(Object obj, Method method, Object[] args,
6471
Class<?> superCls = obj.getClass().getSuperclass();
6572
AutoModule autoModule = superCls.getAnnotation(AutoModule.class);
6673
AutoExpect autoExpect = method.getAnnotation(AutoExpect.class);
74+
AutoSessionStorage autoSessionStorage = method.getAnnotation(AutoSessionStorage.class);
6775

6876
NormalRecord normalRecord = new NormalRecord();
6977
normalRecord.setBeginTime(beginTime);
@@ -74,6 +82,32 @@ public Object intercept(Object obj, Method method, Object[] args,
7482

7583
try
7684
{
85+
if(autoSessionStorage != null)
86+
{
87+
Class<? extends Page> accountClz = autoSessionStorage.accountPage();
88+
String accountNameField = autoSessionStorage.accountName();
89+
90+
Page page = util.getPage(accountClz);
91+
Field accountField = accountClz.getDeclaredField(accountNameField);
92+
93+
accountField.setAccessible(true);
94+
Object value = accountField.get(page);
95+
96+
if(value instanceof Text)
97+
{
98+
String accountNameValue = ((Text) value).getValue();
99+
100+
System.out.println("accountNameValue:" + accountNameValue);
101+
102+
loadSessionStorage(accountNameValue);
103+
}
104+
else
105+
{
106+
throw new AutoTestException("Wrong account type in class: " + accountClz + ", and field : "
107+
+ accountNameField + ". It should be Text type.");
108+
}
109+
}
110+
77111
result = methodProxy.invokeSuper(obj, args);
78112

79113
normalRecord.setEndTime(System.currentTimeMillis());
@@ -103,6 +137,10 @@ public Object intercept(Object obj, Method method, Object[] args,
103137
return result;
104138
}
105139

140+
private void loadSessionStorage(String accountNameValue)
141+
{
142+
}
143+
106144
/**
107145
* 根据注解配置,是否要对异常进行处理
108146
* @param autoExpect

src/main/java/org/suren/autotest/web/framework/spring/AutoModuleScope.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.springframework.beans.factory.ObjectFactory;
2222
import org.springframework.beans.factory.config.Scope;
2323
import org.springframework.context.annotation.ScopedProxyMode;
24+
import org.suren.autotest.web.framework.annotation.AutoSessionStorage;
2425
import org.suren.autotest.web.framework.core.EngineAware;
26+
import org.suren.autotest.web.framework.page.Page;
2527
import org.suren.autotest.web.framework.report.RecordReportWriter;
2628
import org.suren.autotest.web.framework.selenium.WebDriverAware;
2729
import org.suren.autotest.web.framework.settings.AutoModuleProxy;
@@ -56,7 +58,7 @@ public Object get(String name, ObjectFactory<?> objectFactory)
5658
if(object == null)
5759
{
5860
object = objectFactory.getObject();
59-
AutoModuleProxy proxy = new AutoModuleProxy(object, recordReportWriters);
61+
AutoModuleProxy proxy = new AutoModuleProxy(object, recordReportWriters, util);
6062
object = proxy.getProxy();
6163
putAware(object);
6264

0 commit comments

Comments
 (0)