Currently Browsing

Author Archive

How to check presence of an element when it disappears in less than 500ms

It can happen that the element you want to check only appears for a fraction of a second. This can produce false failures when the element appears in between the standard 500ms when WebDriver checks the wait condition. What you can do is to set up a FluentWait object with a specific polling time. It works almost the same as other kinds of wait functions in WebDriver.

The code is the following:



	Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
				.withTimeout(20, java.util.concurrent.TimeUnit.SECONDS)
				.pollingEvery(200, java.util.concurrent.TimeUnit.MILLISECONDS)
				.ignoring(NoSuchElementException.class);
		
		Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>() {
			
			@Override
			public WebElement apply(WebDriver driver) {
				return driver.findElement(exportSnackbarCss);
			}		
		};
		
		wait.until(function);

Stop PowerShell beep in Windows 10

The beep in PowerShell is pretty annoying. It can be turned off with a simple set of commands.

Steps:

  • Open PowerShell with Admin permissions
  • Enter the following into the console and press enter:
    Get-Service Beep | Stop-Service -PassThru | Set-Service -StartupType Disabled

Extracting data from pdf files in selenium webdriver environment

From time-to-time we face with a situation when we have to verify the contents of a pdf file.
There is a nice tool in the Apache toolbox which can do the hard work instead of us called Apache PDFBox.
Beside data extraction it allows creation of new PDF documents, manipulation of existing documents.

The current major version is 2 which came with lots of changes and we have to use a slightly different way to extract the data than it was previously done.

	@Test
	public void testPDFReader() throws Exception {
		// page with example pdf document
		driver.get("http://www.vandevenbv.nl/dynamics/modules/SFIL0200/view.php?fil_Id=5515");

		URL url = new URL(driver.getCurrentUrl());
		BufferedInputStream fileToParse = new BufferedInputStream(url.openStream());

		PDDocument document = null;
		try{
			document = PDDocument.load(fileToParse);
			String output = new PDFTextStripper().getText(document);
			System.out.println(output);
		}finally{

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

You can download an example project from here
Further information can be found on their website.

Running webdriver code in JMeter

With the improvements in technology more and more of the business logic gets executed on the client side and it’s not easy to get a clear picture of the perceivable performance by using pure JMeter tests only.
To extend these measurements JMeter-Webdriver plugin in JMeter can be handy to run WebDriver tests using actual browsers and to get client side execution statistics.

In the following section I will demonstrate how to configure and run a simple WebDriver test in JMeter.

First you have to download the plugin from the JMeter plugin page http://jmeter-plugins.org/downloads/all/ and then copy the library files into JMeter installation folder.

From now on when you start up JMeter you should have the extra Config Element and Sampler components visible on the tool list and they are ready to use

Running simple Ant builds via Jenkins

I assume that a Jenkins is already installed and running on your system.
the first step is to open Jenkins in your web browser a If you didn’t changed the default settings you can reach Jenkins on http://localhost:8080/

After you login to Jenkins the following screen should appear:
2015-10-13_1724[1]

Set up ant in Jenkins in Manage Jenkins -> Configure System. If you use auto install Jenkins will download Ant immediately before the next build.
2015-10-13_1745[1]

Sending notification for broken builds via gmail account in Jenkins

To be able to send emails in Jenkins we have to do two things:
Setup SMTP server in global settings and set the notification option in the project.

    To set SMTP server:

  • go to Manage Jenkins -> Global Settings
  • SMTP server (i.e., outgoing mail): smtp.gmail.com
  • SMTP username: Your full Gmail or Google Apps email address (e.g. example@gmail.com or example@yourdomain.com)
  • SMTP password: Your Gmail or Google Apps email password – be aware if you have a 2-Step Verification, you have to generate a code for the setup
  • SMTP port: 465
  • SMTP TLS/SSL required: yes

2015-10-13_2252[1]

Jenkins setup and basic security settings

You have to download a platform-specific installer from Jenkins site: https://jenkins-ci.org/
2015-10-13_1557[1]

The installation should be easy, if you have any issues with it you can find additional information on the site https://wiki.jenkins-ci.org/display/JENKINS/Containers

After the installation in windows you should restart your machine, Jenkins will start up with windows.
To check if it running you have to go to http://localhost:8080/
2015-10-13_1623[1]

My advice is that the first thing should be to set up an administrator account to block people from the network to misuse the application.

Create an Ant build for TestNG tests

The goal of this exercise is to run TestNG tests through Apache Ant.

First you have to have a sample TestNg project at your hand.
If you don’t have any, you can create the sample using the instructions below, or you can download it from using the link from the bottom of the post.
Create a simple class Exampletest.java

package main;

import org.testng.annotations.Test;

public class ExampleTest {

	@Test
	public void testA() throws Exception {

		System.out.println("Test finished.");

	}
}

Wedbriver wait for ajax to finish and JQuery animation

JQuery’s ajax calls can make an automated tester’s life miserable if you don’t know how to handle them.

The quickest way to handle all of the Jquery ajax calls is to wait for the jQuery.active property to be null.
We can easily construct a sample code trough the JavasscriptExecutor class.It sends out JavaScript code which can be used for testing the property.

public void waitForAjaxToFinish() {
int timeout = 0;

while(timeout < 80) {
	boolean ajaxFinished = (boolean) ((JavascriptExecutor) driver)
			.executeScript("return !!jQuery && jQuery.active == 0");
	if(ajaxFinished) 
		return;
	timeout++;
	waitFor(500);
	}

throw new AssertionError("Ajax haven't finished its job in 40 sec");
}

The above showed code is best for a single Ajax requests, but when we have multiple Ajax requests there can be problems.
Multiple Ajax requests have different completion time and there is no guarantee that there is no fancy Ajax refresher gadget used, which can make previous wait function timeout.

Cookie manipulation in webdriver sessions

Manipulating cookies in selenium Webdriver sessions is really easy, it can enhance your testing and you can make more detailed logs and pinpointing issues.
To be able to insert a cookie you have to have a Webdriver instance at hand, you have to create a Webdriver cookie add it to your Webdriver instance.
To create a cookie you can use on of the constructors of the org.openqa.selenium.Cookie class:

Cookie(java.lang.String name, java.lang.String value)

Create a cookie for the default path with the given name and value with no expiry set.

Cookie(java.lang.String name, java.lang.String value, java.lang.String path)

Create a cookie with the given name value and path.

Cookie(java.lang.String name, java.lang.String value, java.lang.String path, java.util.Date expiry)

Creates an insecure non-httpOnly cookie with no domain specified.