1616
1717package org .suren .autotest .web .framework .log ;
1818
19- import java .awt .Color ;
20- import java .awt .Graphics2D ;
2119import java .awt .image .BufferedImage ;
2220import java .io .File ;
2321import java .io .FileOutputStream ;
2826import java .util .ArrayList ;
2927import java .util .Enumeration ;
3028import java .util .List ;
29+ import java .util .Map ;
3130import java .util .Properties ;
3231
3332import javax .annotation .PostConstruct ;
3736import org .aspectj .lang .ProceedingJoinPoint ;
3837import org .aspectj .lang .annotation .Around ;
3938import org .aspectj .lang .annotation .Aspect ;
40- import org .openqa . selenium . Dimension ;
39+ import org .aspectj . lang . annotation . Before ;
4140import org .openqa .selenium .OutputType ;
42- import org .openqa .selenium .Point ;
43- import org .openqa .selenium .StaleElementReferenceException ;
4441import org .openqa .selenium .TakesScreenshot ;
4542import org .openqa .selenium .WebDriver ;
4643import org .openqa .selenium .WebElement ;
4744import org .slf4j .Logger ;
4845import org .slf4j .LoggerFactory ;
4946import 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 ;
5249import org .springframework .stereotype .Component ;
5350import org .suren .autotest .web .framework .selenium .SeleniumEngine ;
5451import org .suren .autotest .web .framework .util .AnimatedGifEncoder ;
6057 */
6158@ Component
6259@ Aspect
63- @ Scope ( value = "autotest" , proxyMode = ScopedProxyMode . TARGET_CLASS )
60+ @ Configuration
6461public 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