Java Embedded Browser

Why Embed a Browser?

So why embed a browser in an application, when anybody can just access a site via Firefox, Google Chrome, Internet Explorer or others? Why re-invent the wheel?

In terms of security, this is useful because it can restrict access to a web host through the application or it can prevent users from accessing third-party plugins to exploit vulnerabilities with the website.

Another reason is to provide a unique user experience with custom controls and functionality without creating individual plugins for each browser.

Although creating a browser application can lead to poor distribution, this is not important

• • •

Build Maven Standalone Tests

Maven projects are not only used for building standalone or web applications they are also used for providing robust test frameworks using Java.

Running a standalone test is difficult, because the problem is that Maven packages the JAR file with only the main classes or test classes separately without the full dependencies.

The following describes how to create a standalone JAR file with test and main classes as well as all the dependencies.

1. Create a new XML file, e.g. test-jar-with-dependencies.xml, in the project path, e.g. src/main/assembly

2. Edit the test-jar-with-dependencies.xml file and copy

• • •

Upload a File Using Selenium WebDriver

Web application testing is not only about the on-screen functionality but also involves interacting with external resources, such as images, documents and other media.

To upload a file using Selenium, the file path is needed to be set in the input element control before submitting the form.

On the form the control to inspect should look like the image below.

Browse... No files selected

When the folder button is clicked the folder dialog is displayed by the browser.

File Upload dialog

The HTML input element should look similar to the code below.
Take note of the type="file" attribute.

<input id="fileUpload" type="file">Browse.
• • •

Remove Extra New Lines and Whitespace Using Java Regex

Sometimes when extracting text from another item may result in formatting issues that involve extra blank lines or leading/trailing whitespace on each line.

This commonly occurs when extracting from HTML elements or XML documents.

The following String regular expressions can fix the following issues.

(?m) = multi-line mode

The following removes the leading/trailing whitespace from each line in the string.

        "(?m)^[\\s&&[^\n]]+|[\\s+&&[^\n]]+$", "");
   The quick brown fox
      jumps over
         the lazy dog.

The quick brown fox  
jumps over  
the lazy dog.  

The following removes extra blank lines from

• • •

Get the Parent Node of the Current Selenium WebElement

There are times that the parent is required from the child element.

There are two options JavaScript or XPath:

WebElement myElement = driver  
WebElement parent = (WebElement) ((JavascriptExecutor) driver)  
        "return arguments[0].parentNode;", myElement);
WebElement myElement = driver.findElement("myDiv"));  
WebElement parent = myElement.findElement(By.xpath(".."));  

But what if the parent of the parent is required?

The JavaScript would be

return arguments[0].parentNode.parentNode;  

and the XPath will be

• • •