How to Auto Accept Self Signed SSL Certificate with Selenium RC + Firefox

Submitted by on 2013-08-30 16:54:39

When a browser accesses a site with HTTPS which works with self signed certificate, we usually get an trust alert. This often happends if your company has a lot of development sites and don't purchase comercial certificate for cost control purpose.

Let me show you a very simple way to solve this problem.

See the following command which use to start your Selenium RC server:

java -jar selenium-server-standalone-2.33.0.jar -trustAllSSLCertificates -userExtensions user-extensions.js -maxSession 6 -role remotecontrol -hub http://localhost:4444/grid/register -nodeConfig nodeconfig.json

Pay attention to the string "-trustAllSSLCertificates", this is the matter.

In many other articles, they say you need to run with *firefoxproxy, but I found it can play well with *firefox.

Hope you enjoy it.

Java MySQL performance improved 10x by using executeBatch

Submitted by on 2013-08-13 15:22:49

Recently I've been loading some apache access logs into MySQL table for statistics. When I received the log files from my colleague, WOW, big files, the file size was over 9GB.

I immediately wrote some java code to accomplish this task but found that it was very slow by insert the records row by row, over 3 hours to complete the insertion.

Though I already did following tuning the performance is still bad:

  1. Delete index from this table before insertion
  2. Utilize connection pool
  3. Utilize multithreading (10 threads)


Since there will be more and more needs to load the access log, I have to resolve the performance issue.

At this point, I suddently recalled some code snippet with keyword "executeBatch" then I gave it a try and finally the performance improved 10x. It only cost about 15 minutes to complete inserting over 90 million rows. Let me show you the code:
 

    public static void batchInsert(String sql, List<object[]> params) throws Exception {
        Connection conn = MySQLConnection.getConnection(connStr);

        PreparedStatement stmt = conn.prepareStatement(sql);
        for (Object[] o : params) {
            for (int i = 0; i < o.length; i++) {
                stmt.setObject(i + 1, o[i]);
            }
            stmt.addBatch(); // run addBatch() for each SQL
        }

        stmt.executeBatch(); // start batch execute

        if (stmt != null) {
            stmt.close();
        }

        if (conn != null)
            conn.close();
    }


 

How to Have Wordpress Support Paste Images from Word

Submitted by ksblog on 2013-06-15 11:01:04

Sometimes we paste articles directly from Word which contains some images.

The images from Word will be converted to base64 Data URI in Wordpress editor like the following format:

<img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAACpCAIAAAA......chgAAAABJRU5ErkJggg==" />

But in some previous version of Wordpress, it will always filter this kind of data which protocal is "data:" and causes the images pasted from Word couldn't be displayed correctly.

Now let's see how to make it work.

  1. Open file "wordpress\wp-includes\functions.php"
  2. Look for "static $protocols;"
  3. Add "data" as shown below, then restart apache and enjoy it.
function wp_allowed_protocols() {
	static $protocols;

	if ( empty( $protocols ) ) {
		$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'data' );
		$protocols = apply_filters( 'kses_allowed_protocols', $protocols );
	}

	return $protocols;
}

 

Set or change the characters to upper case, lower case, capitalize with VBA in Excel.

Submitted by ksblog on 2012-07-13 14:23:26
  1. UPPER(a1): set a1 cell to be upper case.
  2. LOWER(a1): set a1 cell to be lower case.
  3. UPPER(LEFT(A1,1)) & LOWER(RIGHT(A1,LEN(A1)-1)): To capitalize the a1 cell, which means first letter with upper case and other letters with lower case.

WebDriver Download

Submitted by admin on 2011-11-15 22:58:56

The following URL is for downloading WebDriver libs:

http://code.google.com/p/selenium/downloads/list

Usually I download selenium-java-XX.jar.

You can download dotnet version: selenium-dotnet-XX.jar

The one with name "selenium-server-XX.jar" is the package with selenium RC.

You can see more information in that page.

WebDriver and Select

Submitted by admin on 2011-10-30 21:16:07

WebDriver provides simple and useful API for Select. With them you can easily deal with html Select with WebDriver. Please see the following sample code:

WebElement e = driver.findElements(By.id("select_id"));
Select select = new Select(e);
select.selectByValue("value"); // select by option value
select.selectByIndex(specific_index); // select by option index, start from "0"
select.selectByVisibleText("label_text"); // select by the visible option text

 

The above code show you how to select an option with WebDriver while the following is to deselect options:

// undo the selection for all options
select.deselectAll(); 

// undo selection by option index,  start from "0"
select.deselectByIndex(index);

// undo selection by option value of Select
select.deselectByValue("value");

// undo selection by option text of Select
select.deselectByVisibleText("text");

 

If you want to go through all options then this will help you a lot:

List options = select.getOptions();

 

Sometimes, you will operate on multi-selection Selects, below is also useful:

// check if the Select can be multiple selected
boolean isMultiple = select.isMultiple();

// return all the selected options
List selectedOptions = select.getAllSelectedOptions();

// only return the first one of all the selected options
WebElement option = select.getFirstSelectedOption();

 

WebDriver Select api makes your work efficient, right?

Use WebDriverWait to wait for AJAX/Javascript asynchronized refresh

Submitted by admin on 2011-10-30 20:22:32

Web Driver use Jna to drive local browser to test web application, it's really useful that we don't need to start Selenium Remote Control.

The advantage of browser driven testing is that it can test Ajax/Javascript.

In Selenium IDE we can use "waitForXXX" to wait until what we expected, but Web Driver is different from that, we need to write some additional code to handle that.

The following code is writen in Java.

	Function() {
	            public WebElement apply(WebDriver driver) {
	                return driver.findElement(locator);
	            }
	        };
	}

When complete the above code, you can use the following code to wait for your condition.

	WebDriverWait wait = new WebDriverWait(driver, 60);
	wait.until(presenceOfElementLocated(locator));

Related links: One Useful Class for WebDriver - A Package Class for WebDriverWait

HtmlUnit Http Headers Handling

Submitted by admin on 2011-10-26 13:02:35

How can I handle http headers with HtmlUnit? Easy!

1. Get http headers with Htmlunit.

List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();

String headerValue = webResponse.getResponseHeaderValue(headerName);

2. Add http headers to request with HtmlUnit.

WebRequest req = new WebRequest(new URL("http://sss.com"));
req.setAdditionalHeader(name, value); // or req.setAdditionalHeaders(Map<String, String> additionalHeaders); 
WebResponse webResponse = webClient.loadWebResponse(req);

3. Remove http headers from request with HtmlUnit.

webRequest.removeAdditionalHeader(name);

Look, easy?

Please post me if you have any questions about http headers handling in HtmlUnit.

How to: HtmlUnit Cookies Handling

Submitted by admin on 2011-10-24 20:12:18

HtmlUnit has its mechanism to handle Cookies which is useful for our Web Application testing.

1. Enable/disable Cookies with HtmlUnit.

webClient.getCookieManager().setCookiesEnabled(true);//enable cookies

webClient.getCookieManager().setCookiesEnabled(false);//disable cookies

To see if Cookies is enabled:

webClient.getCookieManager().isCookiesEnabled();

 

2. Get Cookies values with HtmlUnit.

public String getCookieValue(String cookieName) {
		Cookie cookie = webClient.getCookieManager().getCookie(cookieName);
		if(cookie != null){
			return cookie.getValue();
		}
		return "";
	}

Two other methods:

webClient.getCookieManager().getCookies(); //Returns the currently configured cookies, in an unmodifiable set.

webClient.getCookieManager().getCookies(URL url); //Returns the currently configured cookies applicable to the specified URL, in an unmodifiable set.

3. Set cookies values with HtmlUnit.

public void setCookieValue(String cookieName, String value){
		Cookie cookie = new Cookie(cookieName, value);
		this.webClient.getCookieManager().addCookie(cookie);
	}

4. Remove Cookies with HtmlUnit.

public void removeCookies(String cookieName, String value){
		Cookie cookie = new Cookie(cookieName, value);
		webClient.getCookieManager().removeCookie(cookie);
	}

To clear all cookies with HtmlUnit:

public void clearCookies(){
		webClient.getCookieManager().clearCookies();
	}

 

See all code above, you can build up your own class for HtmlUnit Cookies handling.

How To: WebDriver Wait for Element Present

Submitted by admin on 2011-10-23 12:56:21

How to wait for element present with WebDriver?

In the web world, there are more and more web sites utilize AJAX technology to refresh the partial page without reloading the whole page.

Therefore, in the testing world, we often need to type code to wait for some elements' presence to process the next testing steps.

How can we do that? The answer is WebDriverWait Class.

Related Topics: WebDriver Ajax, WebDriver Wait

I've a post in this blog talking about this, please see in this link: Use WebDriverWait to wait for AJAX/Javascript asynchronized refresh