Currently Browsing

Posts Tagged ‘ webdriver ’

Read out browser console logs in java with selenium 2.53.1

Sometimes the usual test log and screenshot is not enough to find the reason of the failed test case. Especially when the test case occasionally fails in special circumstances and it is really hard to reproduce. For this reason, we need to collect for each fail as much information as possible. One possible source of information is the console output of the browser.

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

Change iOS Settings on virtual device with Appium

In our previous post we introduced the steps to set up your Appium environment for iOS 9 devices. In this post we will write about how can you open the Settings app and do some changes in it. We will use Java as programming language, Eclipse for IDE and JUnit as runner.

Ok, so first step is to get the Java client of Selenium from www.seleniumhq.org/download/. Extract the downloaded *.zip file, create a new Java Project in Eclipse and add all extracted *.jar files to Build Path. Now, we can start to implement our first test case for iOS device where we will open the Settings app and disable the Location Service on iPhone. As we mentioned above, we will use JUnit as runner, but you can also choose TestNG or just create a standard Java program with the main() function.

Let’s create a before() method with annotation @Before where we will set up all of the necessary things for the desired test case.

Getting started with Android automation testing

As the automation testing is more and more present in the development of web and desktop applications it is also taking more place in the mobile world. There are various tools and frameworks helping the testers out the job.
First of all it is necessary to get the Android SDK and those API levels which are running on the target devices. Check this link for Android SDK: http://developer.android.com/sdk/index.html. The recommended IDE for android development is the Android Studio, however the example in this guide is written in Eclipse. To add the android plugin to Eclipse follow this guide: http://developer.android.com/sdk/installing/installing-adt.html#Download.

It is possible to run the test cases both on real and virtual devices. In the case if the target device is a virtual one there are two options. The first and more traditional way is using the android emulator which comes with the SDK or the other one is using Genymotion.

Genymotion is an emulator offers the same functionality as the traditional one and even more, beside that the boot of these devices is much faster. It can definitely improve the performance of the tests. You can download here: https://www.genymotion.com/#!/store. It is recommend to add the Genymotion plugin to Eclipse to make the job easier. Find the details on this page: https://www.genymotion.com/#!/download#genymotion-eclipse.

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.

Some thoughts about the infamous StaleElementReferenceException

With the widespread of JavaScript we more and more met with one of our favourite exception which is the StaleElementReferenceException.
It can be tricky because it does not have to appear on the test development machine. This issue can hide in the test code and appear in unexpected situations.

The exception happens when the dom changes and the system can’t be sure if the Webelement is still on the page.
Dom changes can be caused by page refresh, javascript effects, ajax responses and so on.

Navigating between browser windows with WD

Hello,
I’ll continue more about testing with JavaScript WD test frame and Selenium stand-alone server.
Today’s topic is navigating between browser windows (or tabs) with WD.

The Webdriver sees each tab and window as a window, it doesn’t make difference. The difference is only at the browser how it is set to handle the windows appearance, opens them in separate browser or as tabs. With WD the navigation is pretty straight forward and not so difficult. There is a command called windowHandles() which as you can read handles the windows. Actually it returns a list of opened windows based on a generated ID by the browser. I’ll give some minor example about navigating and work between two windows.

Webdriver using javascript expressions in Browser Console

Today I will write about javascript expressions executing in browser consoles during testsing. Sometimes you need to inject some javascript code into the web page, for additional testing, additional features or by client request.
In some cases a simple console.log() could do the trick, but sometimes it is not enough , especially when you use an already opened cmd.exe through which you run tests, because that way the console.log() will write in that console not into the browser’s.