Java Embedded 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 […]

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 […]

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 […]

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 […]

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 .findElement(By.id("myDiv")); WebElement parent = (WebElement) ((JavascriptExecutor) driver) .executeScript( "return arguments[0].parentNode;", myElement); WebElement myElement = driver.findElement(By.id("myDiv")); WebElement parent = myElement.findElement(By.xpath("..")); But what if the parent of the parent is required? The JavaScript would […]

Switch Windows Using Selenium WebDriver

Some web applications open a new window in order to section specific data entry or view. Selenium can switch windows via the window handle. The following is an example and must be configured for a specific website under test. // Store the current window handle String winHandleBefore = driver.getWindowHandle(); // Perform the click operation that […]

Get Entity Field Values as String in Java

The following uses the Apache Commons Lang library that provides the ReflectionToStringBuilder class to assist in the construction of object field values into a single String value. This is quite useful for the viewing all the objects field values and assignments in an easily readable format. The below example uses JDK 1.8 and the Apache […]

JUnit Theory Tests

A Theory JUnit test is a subset of the Parameterized JUnit test feature. It is very similar in respects to driving tests with a set of test data, except with a few differences as explained below. The test class is annotated with @RunWith(Theories.class) The test does not require a constructor to assign values, which reduces […]

Map Two Collections Together in Java

There are times where two separate entity object collections need to be mapped one-to-one. The following code will combine two generic type collections together. It also assesses the size of both collections and key uniqueness. /** * Combines two generic type lists into a map. * * Detects key collisions and duplicate keys. * * […]