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

Commit a700e5b

Browse files
committed
截图部分重构
1 parent 9d3f6a1 commit a700e5b

File tree

15 files changed

+396
-219
lines changed

15 files changed

+396
-219
lines changed

src/main/java/org/suren/autotest/web/framework/AutoApplicationConfig.java

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@
1616

1717
package org.suren.autotest.web.framework;
1818

19-
import java.util.Properties;
20-
21-
import org.springframework.beans.factory.annotation.Autowire;
22-
import org.springframework.beans.factory.annotation.Value;
23-
import org.springframework.context.annotation.Bean;
2419
import org.springframework.context.annotation.ComponentScan;
2520
import org.springframework.context.annotation.Configuration;
26-
import org.springframework.context.annotation.PropertySource;
27-
import org.springframework.mail.MailSender;
28-
import org.springframework.mail.javamail.JavaMailSenderImpl;
21+
import org.springframework.context.annotation.Import;
22+
import org.springframework.context.annotation.ImportResource;
23+
import org.suren.autotest.web.framework.log.Image4SearchLog;
24+
import org.suren.autotest.web.framework.mail.MailConfig;
2925

3026
/**
3127
* Spring零配置
@@ -34,39 +30,11 @@
3430
*/
3531
@Configuration
3632
@ComponentScan
37-
@PropertySource("classpath:application.properties")
33+
@ImportResource({"autoTestContext.xml", "beanScope.xml"})
34+
@Import({
35+
Image4SearchLog.class,
36+
MailConfig.class
37+
})
3838
public class AutoApplicationConfig
3939
{
40-
@Value("${mail.host}")
41-
private String mailHost;
42-
@Value("${mail.username}")
43-
private String mailUserName;
44-
@Value("${mail.password}")
45-
private String mailPassword;
46-
47-
@Bean(autowire = Autowire.BY_TYPE)
48-
public MailSender mailBean()
49-
{
50-
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
51-
mailSender.setHost(mailHost);
52-
mailSender.setUsername(mailUserName);
53-
mailSender.setPassword(mailPassword);
54-
setUpMailPro(mailSender);
55-
56-
return mailSender;
57-
}
58-
59-
/**
60-
* 设置邮箱服务器配置
61-
* @param mailSender
62-
*/
63-
private void setUpMailPro(JavaMailSenderImpl mailSender)
64-
{
65-
Properties javaMailProperties = new Properties();
66-
javaMailProperties.put("mail.smtp.auth", true);
67-
javaMailProperties.put("mail.smtp.starttls.enable", true);
68-
javaMailProperties.put("mail.smtp.timeout", 5000);
69-
70-
mailSender.setJavaMailProperties(javaMailProperties);
71-
}
7240
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.log;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.openqa.selenium.WebElement;
23+
24+
/**
25+
* @author suren
26+
* @date 2017年6月29日 下午3:55:04
27+
*/
28+
public class BlankElementMark implements ElementMark
29+
{
30+
31+
@Override
32+
public void mark(WebElement ele, File file) throws IOException
33+
{
34+
//this is blank class, just for simple.
35+
}
36+
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.log;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.openqa.selenium.WebElement;
23+
24+
/**
25+
* @author suren
26+
* @date 2017年6月29日 下午3:51:18
27+
*/
28+
public interface ElementMark
29+
{
30+
void mark(WebElement ele, File file) throws IOException;
31+
}

src/main/java/org/suren/autotest/web/framework/log/Image4SearchLog.java

Lines changed: 100 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.suren.autotest.web.framework.log;
1818

19-
import java.awt.Color;
20-
import java.awt.Graphics2D;
2119
import java.awt.image.BufferedImage;
2220
import java.io.File;
2321
import java.io.FileOutputStream;
@@ -28,6 +26,7 @@
2826
import java.util.ArrayList;
2927
import java.util.Enumeration;
3028
import java.util.List;
29+
import java.util.Map;
3130
import java.util.Properties;
3231

3332
import javax.annotation.PostConstruct;
@@ -37,18 +36,16 @@
3736
import org.aspectj.lang.ProceedingJoinPoint;
3837
import org.aspectj.lang.annotation.Around;
3938
import org.aspectj.lang.annotation.Aspect;
40-
import org.openqa.selenium.Dimension;
39+
import org.aspectj.lang.annotation.Before;
4140
import org.openqa.selenium.OutputType;
42-
import org.openqa.selenium.Point;
43-
import org.openqa.selenium.StaleElementReferenceException;
4441
import org.openqa.selenium.TakesScreenshot;
4542
import org.openqa.selenium.WebDriver;
4643
import org.openqa.selenium.WebElement;
4744
import org.slf4j.Logger;
4845
import org.slf4j.LoggerFactory;
4946
import org.springframework.beans.factory.annotation.Autowired;
50-
import org.springframework.context.annotation.Scope;
51-
import org.springframework.context.annotation.ScopedProxyMode;
47+
import org.springframework.context.annotation.Bean;
48+
import org.springframework.context.annotation.Configuration;
5249
import org.springframework.stereotype.Component;
5350
import org.suren.autotest.web.framework.selenium.SeleniumEngine;
5451
import org.suren.autotest.web.framework.util.AnimatedGifEncoder;
@@ -60,17 +57,18 @@
6057
*/
6158
@Component
6259
@Aspect
63-
@Scope(value = "autotest", proxyMode = ScopedProxyMode.TARGET_CLASS)
60+
@Configuration
6461
public class Image4SearchLog
6562
{
6663
private static final Logger LOGGER = LoggerFactory.getLogger(Image4SearchLog.class);
6764

6865
@Autowired
6966
private SeleniumEngine engine;
67+
@Autowired
68+
private ElementMark elementMark;
7069

71-
private Properties pro = new Properties();
70+
private Properties imagePro = new Properties();
7271

73-
private String progressIdentify;
7472
private File outputDir;
7573
private List<File> elementSearchImageFileList = new ArrayList<File>();
7674

@@ -88,7 +86,7 @@ public Image4SearchLog()
8886

8987
try(InputStream imgCfgStream = url.openStream())
9088
{
91-
pro.load(imgCfgStream);
89+
imagePro.load(imgCfgStream);
9290
}
9391
}
9492
}
@@ -102,17 +100,19 @@ public File getOutputFile()
102100
{
103101
if(outputDir == null)
104102
{
105-
outputDir = new File(pro.getProperty(LoggerConstants.IMG_LOG_DIR, System.getProperty("java.io.tmpdir")));
103+
outputDir = new File(imagePro.getProperty(LoggerConstants.IMG_LOG_DIR,
104+
System.getProperty("java.io.tmpdir")));
106105
if(!outputDir.isDirectory())
107106
{
108107
if(!outputDir.mkdirs())
109108
{
110109
LOGGER.error(String.format("Can not create img dir [%s].", outputDir));
111110

112111
outputDir = new File(System.getProperty("java.io.tmpdir"));
113-
pro.setProperty(LoggerConstants.IMG_LOG_DIR, outputDir.getAbsolutePath());
114112
}
115113
}
114+
115+
engine.setProgressId(LoggerConstants.IMG_LOG_DIR, outputDir.getAbsolutePath());
116116
}
117117

118118
return outputDir;
@@ -122,23 +122,76 @@ public File getOutputFile()
122122
public void init()
123123
{
124124
outputDir = getOutputFile();
125+
Map<Object, Object> engineConfig = engine.getEngineConfig();
126+
long currentTime = System.currentTimeMillis();
127+
128+
String progressIdentify = (String) engineConfig.getOrDefault(
129+
LoggerConstants.PROGRESS_IDENTIFY, String.valueOf(currentTime));
130+
String applicationIdentify = (String) engineConfig.getOrDefault(LoggerConstants.APP_IDENTIFY,
131+
"phoenix");
132+
133+
outputDir = new File(outputDir, applicationIdentify);
134+
outputDir.mkdirs();
135+
outputDir = new File(outputDir, progressIdentify);
136+
outputDir.mkdirs();
137+
138+
String gifPath = new File(outputDir, currentTime + ".gif").getAbsolutePath();
125139

126-
progressIdentify = (String) engine.getEngineConfig().get("progress_identify");
140+
engine.setProgressId(LoggerConstants.APP_IDENTIFY, applicationIdentify);
141+
engine.setProgressId(LoggerConstants.PROGRESS_IDENTIFY, progressIdentify);
142+
engine.setProgressId(LoggerConstants.GIF_CURRENT_PATH, gifPath);
127143

128144
animatedGifEncoder = new AnimatedGifEncoder();
129-
animatedGifEncoder.start(new File(outputDir, progressIdentify + ".gif").getAbsolutePath());
145+
animatedGifEncoder.start(gifPath);
130146
animatedGifEncoder.setDelay(800);
131147
animatedGifEncoder.setRepeat(0);
132148
}
149+
150+
@Before("within(org.suren.autotest.web.framework.core.ElementSearchStrategy)")
151+
public void search()
152+
{
153+
System.out.println("sdds");
154+
}
133155

134156
@Around("execution(* org.suren.autotest.web.framework.core.ElementSearchStrategy.search*(..))")
135-
public Object hello(ProceedingJoinPoint joinPoint) throws Throwable
157+
public Object imageLog(ProceedingJoinPoint joinPoint) throws Throwable
136158
{
137159
Object[] args = joinPoint.getArgs();
138160

139-
Object res = joinPoint.proceed(args);
161+
Object res = null;
162+
Throwable ea = null;
163+
try
164+
{
165+
res = joinPoint.proceed(args);
166+
}
167+
catch(Throwable e)
168+
{
169+
ea = e;
170+
171+
throw e;
172+
}
173+
finally
174+
{
175+
capture(res, ea);
176+
}
177+
178+
return res;
179+
}
180+
181+
/**
182+
* 截图捕捉
183+
* @param ele
184+
* @throws IOException
185+
*/
186+
private void capture(Object ele, Throwable e) throws IOException
187+
{
188+
if(ele == null)
189+
{
190+
return;
191+
}
192+
140193
WebDriver driver = engine.getDriver();
141-
if(res instanceof WebElement && driver instanceof TakesScreenshot)
194+
if(ele instanceof WebElement && driver instanceof TakesScreenshot)
142195
{
143196
TakesScreenshot shot = (TakesScreenshot) driver;
144197

@@ -147,29 +200,46 @@ public Object hello(ProceedingJoinPoint joinPoint) throws Throwable
147200

148201
try
149202
{
150-
WebElement webEle = (WebElement) res;
151-
Point loc = webEle.getLocation();
152-
Dimension size = webEle.getSize();
153-
154-
Graphics2D g = bufImg.createGraphics();
155-
g.setColor(Color.red);
156-
g.drawRect(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
203+
elementMark.mark((WebElement) ele, file);
204+
}
205+
catch(IOException ioEx)
206+
{
207+
ioEx.printStackTrace();
208+
}
209+
210+
long currentTime = System.currentTimeMillis();
211+
String fileName = null;
212+
if(e == null)
213+
{
214+
fileName = currentTime + ".png";
157215
}
158-
catch(StaleElementReferenceException e)
216+
else
159217
{
160-
//
218+
fileName = "ex_" + currentTime + ".png";
161219
}
162220

163-
File elementSearchImageFile = new File(outputDir, progressIdentify + "_" + System.currentTimeMillis() + ".png");
221+
File elementSearchImageFile = new File(outputDir, fileName);
164222
try(OutputStream output = new FileOutputStream(elementSearchImageFile))
165223
{
166224
ImageIO.write(bufImg, "gif", output);
167225
elementSearchImageFileList.add(elementSearchImageFile);
168226
animatedGifEncoder.addFrame(bufImg);
169227
}
170228
}
171-
172-
return res;
229+
}
230+
231+
@Bean
232+
public ElementMark createElementMark()
233+
{
234+
boolean mark = Boolean.valueOf(imagePro.getProperty("element.mark", "true"));
235+
if(mark)
236+
{
237+
return new TargetElementMark();
238+
}
239+
else
240+
{
241+
return new BlankElementMark();
242+
}
173243
}
174244

175245
@PreDestroy

0 commit comments

Comments
 (0)