Posted By

jerryvig on 11/17/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hippopotamoi


Native Clicks 8 Java Robot


 / Published in: Java
 

  1. package com.mktneutral;
  2.  
  3. import java.util.Set;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Iterator;
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.io.FileReader;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.File;
  13. import java.awt.event.InputEvent;
  14. import java.awt.event.KeyEvent;
  15. import java.awt.Robot;
  16. import java.awt.AWTException;
  17. import com.thoughtworks.selenium.DefaultSelenium;
  18. import org.openqa.selenium.WebDriverBackedSelenium;
  19. import org.openqa.selenium.WebDriver;
  20. import org.openqa.selenium.firefox.FirefoxDriver;
  21. import java.util.regex.Pattern;
  22. import org.browsermob.proxy.ProxyServer;
  23. import org.openqa.selenium.Proxy;
  24. import org.openqa.selenium.remote.DesiredCapabilities;
  25. import org.openqa.selenium.remote.CapabilityType;
  26. import org.browsermob.core.har.Har;
  27. import org.browsermob.core.har.HarLog;
  28. import org.browsermob.core.har.HarEntry;
  29. import org.json.JSONArray;
  30. import org.json.JSONObject;
  31. import org.json.JSONException;
  32.  
  33. public class NativeClicks {
  34. private static WebDriverBackedSelenium selena;
  35. private static String winName;
  36. private static int fullScreen = 0;
  37. private static Har har;
  38.  
  39. public static void main( String[] args ) {
  40. try {
  41. ProxyServer server = new ProxyServer(4444);
  42. server.start();
  43. Proxy proxy = server.seleniumProxy();
  44. //Proxy proxy = new Proxy();
  45. DesiredCapabilities capabilities = new DesiredCapabilities();
  46. capabilities.setCapability(CapabilityType.PROXY, proxy);
  47.  
  48. FirefoxDriver driver = new FirefoxDriver( capabilities );
  49. server.newHar("univision.com");
  50. selena = new WebDriverBackedSelenium( driver, "univision.com" );
  51. doClicks( "http://www.univision.com/" );
  52.  
  53. har = server.getHar();
  54. har.writeTo( new FileOutputStream( "/tmp/harfile.txt" ) );
  55.  
  56. parseJSON();
  57.  
  58. server.stop();
  59. } catch ( NullPointerException npe ) { System.out.println( "threw npe" ); npe.printStackTrace(); }
  60. catch ( Exception e ) { e.printStackTrace(); }
  61. }
  62.  
  63. public static void doClicks( String _url ) {
  64. try {
  65. selena.getWrappedDriver().get( _url );
  66.  
  67. Robot bot = new Robot();
  68. bot.mouseMove(350,350);
  69. bot.delay(200);
  70. bot.mousePress( InputEvent.BUTTON3_MASK );
  71. bot.delay( 200 );
  72. bot.mouseRelease( InputEvent.BUTTON3_MASK );
  73. bot.delay( 200 );
  74. if ( fullScreen == 0 ) {
  75. selena.windowMaximize();
  76. bot.keyPress( java.awt.event.KeyEvent.VK_F11 );
  77. bot.delay( 50 );
  78. bot.keyRelease( java.awt.event.KeyEvent.VK_F11 );
  79. bot.delay( 3000 );
  80. bot.delay( 200 );
  81. fullScreen = 1;
  82. }
  83. bot.mousePress( InputEvent.BUTTON3_MASK );
  84. bot.delay( 200 );
  85. bot.mouseRelease( InputEvent.BUTTON3_MASK );
  86. bot.delay( 300 );
  87.  
  88. int screenHeight = Integer.parseInt(selena.getEval( "window.outerHeight;" ).trim());
  89.  
  90. System.out.println( screenHeight );
  91.  
  92. int embedCount = Integer.parseInt(selena.getEval( "var embedCount = 0; var embedList = window.document.getElementsByTagName('embed'); for ( var i=0; i<embedList.length; i++ ) { embedList.item(i).setAttribute('id','embed'+i); embedCount++; } embedCount;" ).trim());
  93. //int embedCount = Integer.parseInt(selena.getEval( "embedCount;" ));
  94.  
  95. System.out.println( embedCount );
  96.  
  97. ArrayList<String> embedList = new ArrayList<String>();
  98. for ( int i=0; i<embedCount; i++ ) {
  99. embedList.add( selena.getEval("window.document.getElementById('embed"+i+"').getAttribute('id')") );
  100. }
  101.  
  102. Set<String> windows = selena.getWrappedDriver().getWindowHandles();
  103. Iterator iter = windows.iterator();
  104. winName = "";
  105. while (iter.hasNext()) {
  106. winName = (String)iter.next();
  107. }
  108.  
  109. ArrayList<String> clickPoints = new ArrayList<String>();
  110. for ( String embedId : embedList ) {
  111. clickPoints.add( selena.getEval( "var height = window.document.getElementById( '"+embedId+"' ).height; var width = window.document.getElementById('"+embedId+"' ).width; var top = window.document.getElementById( '"+embedId+"' ).offsetTop + window.screenY; var left = window.document.getElementById('"+embedId+"' ).offsetLeft + window.screenX; var leftPt = Math.floor(left+(width/2)); var topPt = Math.floor(top+(height/2)); var clickPoint = new Array( leftPt, topPt, height ); clickPoint;" ) );
  112. }
  113.  
  114. int maxVerticalPos = screenHeight;
  115. for ( String clickPt : clickPoints ) {
  116. clickPt = clickPt.replaceAll(" ","");
  117. clickPt = clickPt.replaceAll(Pattern.quote("["),"");
  118. clickPt = clickPt.replaceAll(Pattern.quote("]"),"");
  119. String[] clickPoint = clickPt.split(",");
  120. System.out.println( clickPoint[1] );
  121. try {
  122. if ( Integer.parseInt(clickPoint[1].trim()) > maxVerticalPos ) {
  123. maxVerticalPos = Integer.parseInt(clickPoint[1].trim())+Integer.parseInt(clickPoint[2].trim())/2;
  124. }
  125. } catch ( NumberFormatException nfe ) { nfe.printStackTrace(); }
  126. catch ( ArrayIndexOutOfBoundsException nfe ) { nfe.printStackTrace(); }
  127. }
  128.  
  129. System.out.println( "maxVPos = " + maxVerticalPos );
  130.  
  131. for ( String clickPt : clickPoints ) {
  132. try {
  133. int maxPgDownCount = 0;
  134. if ( maxVerticalPos < screenHeight ) {
  135. maxPgDownCount = 0;
  136. }
  137. else {
  138. maxPgDownCount = (new Double(Math.floor((maxVerticalPos-(screenHeight-41))/(screenHeight-41)))).intValue();
  139. }
  140. int lastPgDown = maxVerticalPos - maxPgDownCount*(screenHeight-41) - screenHeight;
  141.  
  142. clickPt = clickPt.replaceAll(" ","");
  143. clickPt = clickPt.replaceAll(Pattern.quote("["),"");
  144. clickPt = clickPt.replaceAll(Pattern.quote("]"),"");
  145. String[] clickPoint = clickPt.split(",");
  146.  
  147. System.out.println( "maxPgDown = " + maxPgDownCount );
  148.  
  149. int pageDownCount = 0;
  150.  
  151. int verticalPos = Integer.parseInt(clickPoint[1].trim());
  152. for ( int i=0; i<maxPgDownCount+1; i++ ) {
  153. verticalPos = (Integer.parseInt(clickPoint[1].trim()) - (screenHeight-41)*i);
  154. if ( verticalPos < screenHeight ) {
  155. break;
  156. }
  157. pageDownCount++;
  158. }
  159.  
  160. System.out.println( clickPt + "," + verticalPos );
  161.  
  162. if ( verticalPos > screenHeight ) {
  163. bot.keyPress( KeyEvent.VK_END );
  164. bot.delay( 200 );
  165. bot.keyRelease( KeyEvent.VK_END );
  166. verticalPos = (Integer.parseInt(clickPoint[1].trim())) - maxPgDownCount*(screenHeight-41) - lastPgDown;
  167. System.out.println( "lastPgDown =" + lastPgDown + "verticalPos = " + verticalPos );
  168. doClick( bot, Integer.parseInt(clickPoint[0].trim()), verticalPos );
  169. }
  170. else {
  171. for ( int i=0; i<pageDownCount; i++ ) {
  172. bot.mouseMove(1,1);
  173. bot.delay(200);
  174. bot.mousePress( InputEvent.BUTTON1_MASK );
  175. bot.delay( 400 );
  176. bot.mouseRelease( InputEvent.BUTTON1_MASK );
  177. bot.delay( 200 );
  178. bot.keyPress( KeyEvent.VK_PAGE_DOWN );
  179. bot.delay( 400 );
  180. bot.keyRelease( KeyEvent.VK_PAGE_DOWN );
  181. System.out.println( "you executed the page downs" );
  182. }
  183. doClick( bot, Integer.parseInt(clickPoint[0].trim()), verticalPos );
  184. }
  185. } catch ( NumberFormatException nfe ) { nfe.printStackTrace(); }
  186. catch ( ArrayIndexOutOfBoundsException nfe ) { nfe.printStackTrace(); }
  187. }
  188.  
  189. int iframeCount = Integer.parseInt(selena.getEval( "var iframeCount = 0; var iframeList = window.document.getElementsByTagName('iframe'); for ( var i=0; i<iframeList.length; i++ ) { iframeList.item(i).setAttribute('id','iframe'+i); iframeCount++; } iframeCount;" ).trim());
  190. System.out.println( iframeCount );
  191.  
  192. ArrayList<String> iframeSrcList = new ArrayList<String>();
  193. for ( int i=0; i<iframeCount; i++ ) {
  194. String iframeSrc = selena.getEval( "window.document.getElementById('iframe"+i+"').getAttribute('src');" );
  195. iframeSrcList.add( iframeSrc );
  196. doClicks( iframeSrc );
  197. }
  198.  
  199. try {
  200. Process p = Runtime.getRuntime().exec( "/usr/bin/killall firefox-bin" );
  201. BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
  202. String s;
  203. while ((s=r.readLine())!=null) {
  204. System.out.println( s );
  205. }
  206. r.close();
  207. } catch ( IOException ioe ) { ioe.printStackTrace(); }
  208. } catch ( NullPointerException npe ) { npe.printStackTrace(); }
  209. catch ( Exception awte ) { awte.printStackTrace(); }
  210. }
  211.  
  212. public static void doClick( Robot bot, int leftPos, int verticalPos ) {
  213. try {
  214. bot.mouseMove( leftPos, verticalPos );
  215. bot.delay(200);
  216. bot.keyPress( KeyEvent.VK_CONTROL );
  217. bot.delay( 200 );
  218. bot.mousePress( InputEvent.BUTTON1_MASK );
  219. bot.delay( 400 );
  220. bot.mouseRelease( InputEvent.BUTTON1_MASK );
  221. bot.delay( 200 );
  222. bot.delay( 4000 );
  223. bot.keyRelease( KeyEvent.VK_CONTROL );
  224.  
  225. selena.selectWindow( winName );
  226.  
  227. bot.delay( 200 );
  228. bot.mouseMove(1,1);
  229. bot.delay(200);
  230. bot.mousePress( InputEvent.BUTTON1_MASK );
  231. bot.delay( 200 );
  232. bot.mouseRelease( InputEvent.BUTTON1_MASK );
  233. bot.delay( 200 );
  234. bot.keyPress( KeyEvent.VK_HOME );
  235. bot.delay( 200 );
  236. bot.keyRelease( KeyEvent.VK_HOME );
  237. bot.delay( 1500 );
  238.  
  239. } catch ( Exception awte ) { awte.printStackTrace(); }
  240. }
  241.  
  242. public static void parseJSON() {
  243. try {
  244. BufferedReader r = new BufferedReader( new FileReader( "/tmp/harfile.txt" ) );
  245. String s;
  246. String jsonString = "";
  247. while ((s=r.readLine())!=null) {
  248. jsonString += s.trim();
  249. }
  250. r.close();
  251.  
  252. JSONObject jsonObj = new JSONObject( jsonString );
  253. JSONObject logObj = jsonObj.getJSONObject( "log" );
  254. JSONArray entriesArray = logObj.getJSONArray("entries");
  255. System.out.println( entriesArray.length() );
  256. // System.out.println( "entries string = " + logObj.toString() );
  257. //String[] names = JSONObject.getNames( entriesObj );
  258. //System.out.println( " name are " );
  259. //for ( String name : names ) {
  260. // System.out.println( name );
  261. //}
  262. for ( int i=0; i<entriesArray.length(); i++ ) {
  263. try {
  264. JSONObject obj = entriesArray.getJSONObject( i );
  265. JSONObject resp = obj.getJSONObject( "response" );
  266. JSONObject content = resp.getJSONObject( "content" );
  267.  
  268. if ( content.getString( "mimeType" ).equals("image/png") || content.getString( "mimeType" ).equals("image/jpeg") || content.getString( "mimeType" ).equals("image/gif") || content.getString( "mimeType" ).equals("application/x-shockwave-flash") ) {
  269. System.out.println( content.getString( "mimeType" ) );
  270. JSONObject req = obj.getJSONObject("request");
  271. System.out.println( "url = " + req.getString( "url" ) );
  272. }
  273. } catch ( JSONException jsone ) {
  274. jsone.printStackTrace();
  275. }
  276. }
  277.  
  278. } catch ( IOException ioe ) { ioe.printStackTrace(); }
  279. catch ( JSONException jsone ) { jsone.printStackTrace(); }
  280. }
  281. }

Report this snippet  

You need to login to post a comment.