Using selenium and phantomjs with Maven

Here are the dependencies you will need to use Selenium and PhantomJS with Maven.

		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>2.53.0</version>
		</dependency>
		<dependency>
			<groupId>io.github.bonigarcia</groupId>
			<artifactId>webdrivermanager</artifactId>
			<version>1.4.2</version>
		</dependency>

and in your code call the following

PhantomJsDriverManager.getInstance().setup();

WebDriverManager downloads the latest version of the required PhantomJS binary to be used with Selenium WebDriver.

Turn off log messages for Selenium PhantomJS

When using PhantomJS visa GhostDriver with Selenium, you will get all kinds of log messages which would not be necessary in some instances. The example below shows how to disable these log messages.

            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setJavascriptEnabled(true);
            caps.setCapability("takesScreenshot", true);
            caps.setCapability("screen-resolution", "1280x1024");
            caps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\Users\\USER\\Downloads\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe");

            ArrayList<String> cliArgsCap = new ArrayList<String>();
            cliArgsCap.add("--webdriver-loglevel=NONE");
            caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
            Logger.getLogger(PhantomJSDriverService.class.getName()).setLevel(Level.OFF);

            driver = new PhantomJSDriver(caps);

Using proxy with Selenium PhantomJS

The example below shows how you can configure PhantomJS to use proxies with your web testing.

ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=127.0.0.1:1024");
// cliArgsCap.add("--proxy-auth=username:password");
cliArgsCap.add("--proxy-type=socks5");

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability("screen-resolution", "1280x1024");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\Users\\USER\\Downloads\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe");

caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

Logger.getLogger(PhantomJSDriverService.class.getName()).setLevel(Level.OFF);

driver = new PhantomJSDriver(caps);

Checking response time with HTTPWebRequest

If you want to time the response time of your webrequests/proxies, etc using C# there is a easy way to do it. We need to use a stopwatch object as shown in the example.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
System.Diagnostics.Stopwatch timer = new Stopwatch();

timer.Start();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close ();

timer.Stop();

TimeSpan timeTaken = timer.Elapsed;

Java programs that contain file paths, making them compatible on Windows and Linux

If your Java application makes use of files using paths, it’s best not to use slash “/” or backslash “\” hard coded in your source code. Use the following to ensure your program would work as expected on both Windows and Unix like systems.

File.separator

Increasing java heap space with command line argument

If you find that your Java application is running our of heap space and you don’t have a memory leak, you can give the JMV more memory. This is very simple to do by starting your java application with the following argument.

java -Xmx6g myprogram

You have three options for increasing size

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size

java -Xms16m -Xmx64m ClassName

Capturing network traffic through Java with Fiddler

If you do any socket programming on Java or use any library that does, there will come a time that you need to monitor the traffic going through your application. You will need the following class in order to see the traffic in Fiddler

public final class XTrustProvider extends java.security.Provider
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private final static String	NAME		= "XTrustJSSE";
	private final static String	INFO		= "XTrust JSSE Provider (implements trust factory with truststore validation disabled)";
	private final static double	VERSION	= 1.0D;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public XTrustProvider()
	{
		super(NAME, VERSION, INFO);

		AccessController.doPrivileged(new PrivilegedAction()
		{
			public Object run()
			{
				put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(), TrustManagerFactoryImpl.class.getName());
				return null;
			}
		});
	}

	public static void install()
	{
		if (Security.getProvider(NAME) == null)
		{
			Security.insertProviderAt(new XTrustProvider(), 2);
			Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm());
		}
	}

	public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi
	{
		public TrustManagerFactoryImpl()
		{
		}

		public static String getAlgorithm()
		{
			return "XTrust509";
		}

		protected void engineInit(KeyStore keystore) throws KeyStoreException
		{
		}

		protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException
		{
			throw new InvalidAlgorithmParameterException(XTrustProvider.NAME + " does not use ManagerFactoryParameters");
		}

		protected TrustManager[] engineGetTrustManagers()
		{
			return new TrustManager[]
			{ new X509TrustManager()
			{
				public X509Certificate[] getAcceptedIssuers()
				{
					return null;
				}

				public void checkClientTrusted(X509Certificate[] certs, String authType)
				{
				}

				public void checkServerTrusted(X509Certificate[] certs, String authType)
				{
				}
			} };
		}
	}
}

You can initialize the above class by the following

XTrustProvider.install();