/ Published in: Java
Expand |
Embed | Plain Text
package com.mktneutral; import java.util.Set; import java.util.ArrayList; import java.util.List; import java.util.Iterator; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.File; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.Robot; import java.awt.AWTException; import com.thoughtworks.selenium.DefaultSelenium; import org.openqa.selenium.WebDriverBackedSelenium; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.regex.Pattern; import org.browsermob.proxy.ProxyServer; import org.openqa.selenium.Proxy; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.CapabilityType; import org.browsermob.core.har.Har; import org.browsermob.core.har.HarLog; import org.browsermob.core.har.HarEntry; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; public class NativeClicks { private static WebDriverBackedSelenium selena; private static int fullScreen = 0; private static Har har; try { ProxyServer server = new ProxyServer(4444); server.start(); //Proxy proxy = new Proxy(); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, proxy); FirefoxDriver driver = new FirefoxDriver( capabilities ); server.newHar("univision.com"); selena = new WebDriverBackedSelenium( driver, "univision.com" ); doClicks( "http://www.univision.com/" ); har = server.getHar(); parseJSON(); server.stop(); } try { selena.getWrappedDriver().get( _url ); bot.mouseMove(350,350); bot.delay(200); bot.delay( 200 ); bot.delay( 200 ); if ( fullScreen == 0 ) { selena.windowMaximize(); bot.delay( 50 ); bot.delay( 3000 ); bot.delay( 200 ); fullScreen = 1; } bot.delay( 200 ); bot.delay( 300 ); 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()); //int embedCount = Integer.parseInt(selena.getEval( "embedCount;" )); ArrayList<String> embedList = new ArrayList<String>(); for ( int i=0; i<embedCount; i++ ) { embedList.add( selena.getEval("window.document.getElementById('embed"+i+"').getAttribute('id')") ); } Set<String> windows = selena.getWrappedDriver().getWindowHandles(); winName = ""; while (iter.hasNext()) { } ArrayList<String> clickPoints = new ArrayList<String>(); 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;" ) ); } int maxVerticalPos = screenHeight; clickPt = clickPt.replaceAll(" ",""); clickPt = clickPt.replaceAll(Pattern.quote("["),""); clickPt = clickPt.replaceAll(Pattern.quote("]"),""); try { } } try { int maxPgDownCount = 0; if ( maxVerticalPos < screenHeight ) { maxPgDownCount = 0; } else { } int lastPgDown = maxVerticalPos - maxPgDownCount*(screenHeight-41) - screenHeight; clickPt = clickPt.replaceAll(" ",""); clickPt = clickPt.replaceAll(Pattern.quote("["),""); clickPt = clickPt.replaceAll(Pattern.quote("]"),""); int pageDownCount = 0; for ( int i=0; i<maxPgDownCount+1; i++ ) { if ( verticalPos < screenHeight ) { break; } pageDownCount++; } if ( verticalPos > screenHeight ) { bot.delay( 200 ); verticalPos = (Integer.parseInt(clickPoint[1].trim())) - maxPgDownCount*(screenHeight-41) - lastPgDown; } else { for ( int i=0; i<pageDownCount; i++ ) { bot.mouseMove(1,1); bot.delay(200); bot.delay( 400 ); bot.delay( 200 ); bot.delay( 400 ); } } } 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()); ArrayList<String> iframeSrcList = new ArrayList<String>(); for ( int i=0; i<iframeCount; i++ ) { String iframeSrc = selena.getEval( "window.document.getElementById('iframe"+i+"').getAttribute('src');" ); iframeSrcList.add( iframeSrc ); doClicks( iframeSrc ); } try { String s; while ((s=r.readLine())!=null) { } r.close(); } try { bot.mouseMove( leftPos, verticalPos ); bot.delay(200); bot.delay( 200 ); bot.delay( 400 ); bot.delay( 200 ); bot.delay( 4000 ); selena.selectWindow( winName ); bot.delay( 200 ); bot.mouseMove(1,1); bot.delay(200); bot.delay( 200 ); bot.delay( 200 ); bot.delay( 200 ); bot.delay( 1500 ); } public static void parseJSON() { try { String s; while ((s=r.readLine())!=null) { jsonString += s.trim(); } r.close(); JSONObject jsonObj = new JSONObject( jsonString ); JSONObject logObj = jsonObj.getJSONObject( "log" ); JSONArray entriesArray = logObj.getJSONArray("entries"); // System.out.println( "entries string = " + logObj.toString() ); //String[] names = JSONObject.getNames( entriesObj ); //System.out.println( " name are " ); //for ( String name : names ) { // System.out.println( name ); //} for ( int i=0; i<entriesArray.length(); i++ ) { try { JSONObject obj = entriesArray.getJSONObject( i ); JSONObject resp = obj.getJSONObject( "response" ); JSONObject content = resp.getJSONObject( "content" ); 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") ) { JSONObject req = obj.getJSONObject("request"); } } catch ( JSONException jsone ) { jsone.printStackTrace(); } } catch ( JSONException jsone ) { jsone.printStackTrace(); } } }
You need to login to post a comment.
