How to read text from PDF file using Java and Selenium Webdriver

Sometimes we need to verify a PDF content but Selenium WebDriver doesn’t have any direct methods to do that.
If we want to extract the PDF content then we can use for example Apache PDFBox.
Simply download the .jar files and add them to your Eclipse Class path.
Here is a sample script which will extract text from a sample PDF file:


import java.io.BufferedInputStream;
import java.net.URL;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.util.PDFTextStripper;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class pdfReader {

WebDriver driver;

@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
}

@AfterClass
public void tearDown() {
driver.quit();
}

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

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

PDFParser parser = new PDFParser(fileToParse);
parser.parse();

String output = new PDFTextStripper().getText(parser.getPDDocument());
System.out.println(output);
parser.getPDDocument().close();
}
}

Simply load the website with the PDF file. The browser will open the PDF file then we read the url, parse it with PDFParser and that’s it!

How to test different resolutions – Part two – Firefox resolutions testing

Continuing from the last months Chrome resolution testing, we are going to test tools from Firefox. None of the tools listed simulate mobile devices. So for mobile testing the mobile web address will have to be manually used, as the browser won’t be recognized as a mobile device. Unfortunately this will not work with every site, and even on sites that work, some pages may not be displayed correctly.

FireSizer

This add-on uses the Add-on bar at the bottom of the Firefox window. Unfortunately this feature was removed in Firefox version 29+, so an additional add-on is required to make it work (https://addons.mozilla.org/en-US/firefox/addon/the-addon-bar/). After installing the add-on bar it will appear at the bottom of the screen, from where the FireSizer can be used. The FireSizer will be located in the bottom right of the screen. It will show only as the current window resolution, right clicking the displayed resolution shows additional options. You can select from existing presets, or add you own. You can also save the current window size. This add-on doesn’t resize only the HTML area (viewport), but takes into account window borders, top/bottom status bars, menus, navigation buttons, etc. This isn’t so good for mobile testing, but makes more sense for desktop testing.

Random encounter #01

If you see the code below, then you probably copied the wrong url to your browser, try to remove /wd/hub from its end.
Eg.: instead of localhost:4444/wd/hub try just localhost:4444

{"status":13,"value":{"class":"java.lang.NullPointerException","stackTrace":
[{"fileName":"ExternalSessionKey.java","lineNumber":75,"className":"org.openqa.grid.internal.ExternalSessionKey","methodName":"fromWebDriverRequest"},
{"fileName":"WebDriverRequest.java","lineNumber":60,"className":"org.openqa.grid.web.servlet.handler.WebDriverRequest","methodName":"extractSession"},
{"fileName":"RequestHandler.java","lineNumber":237,"className":"org.openqa.grid.web.servlet.handler.RequestHandler","methodName":"getSession"},
{"fileName":"RequestHandler.java","lineNumber":119,"className":"org.openqa.grid.web.servlet.handler.RequestHandler","methodName":"process"},
{"fileName":"DriverServlet.java","lineNumber":83,"className":"org.openqa.grid.web.servlet.DriverServlet","methodName":"process"},
{"fileName":"DriverServlet.java","lineNumber":61,"className":"org.openqa.grid.web.servlet.DriverServlet","methodName":"doGet"},
{"fileName":"HttpServlet.java","lineNumber":707,"className":"javax.servlet.http.HttpServlet","methodName":"service"},
{"fileName":"HttpServlet.java","lineNumber":820,"className":"javax.servlet.http.HttpServlet","methodName":"service"},
{"fileName":"ServletHolder.java","lineNumber":565,"className":"org.seleniumhq.jetty7.servlet.ServletHolder","methodName":"handle"},
{"fileName":"ServletHandler.java","lineNumber":479,"className":"org.seleniumhq.jetty7.servlet.ServletHandler","methodName":"doHandle"},
{"fileName":"SessionHandler.java","lineNumber":225,"className":"org.seleniumhq.jetty7.server.session.SessionHandler","methodName":"doHandle"},
{"fileName":"ContextHandler.java","lineNumber":1031,"className":"org.seleniumhq.jetty7.server.handler.ContextHandler","methodName":"doHandle"},
{"fileName":"ServletHandler.java","lineNumber":406,"className":"org.seleniumhq.jetty7.servlet.ServletHandler","methodName":"doScope"},
{"fileName":"SessionHandler.java","lineNumber":186,"className":"org.seleniumhq.jetty7.server.session.SessionHandler","methodName":"doScope"},
{"fileName":"ContextHandler.java","lineNumber":965,"className":"org.seleniumhq.jetty7.server.handler.ContextHandler","methodName":"doScope"},
{"fileName":"ScopedHandler.java","lineNumber":117,"className":"org.seleniumhq.jetty7.server.handler.ScopedHandler","methodName":"handle"},
{"fileName":"HandlerWrapper.java","lineNumber":111,"className":"org.seleniumhq.jetty7.server.handler.HandlerWrapper","methodName":"handle"},
{"fileName":"Server.java","lineNumber":349,"className":"org.seleniumhq.jetty7.server.Server","methodName":"handle"},
{"fileName":"AbstractHttpConnection.java","lineNumber":452,"className":"org.seleniumhq.jetty7.server.AbstractHttpConnection","methodName":"handleRequest"},
{"fileName":"BlockingHttpConnection.java","lineNumber":47,"className":"org.seleniumhq.jetty7.server.BlockingHttpConnection","methodName":"handleRequest"},
{"fileName":"AbstractHttpConnection.java","lineNumber":884,"className":"org.seleniumhq.jetty7.server.AbstractHttpConnection","methodName":"headerComplete"},
{"fileName":"AbstractHttpConnection.java","lineNumber":938,"className":"org.seleniumhq.jetty7.server.AbstractHttpConnection$RequestHandler","methodName":"headerComplete"},
{"fileName":"HttpParser.java","lineNumber":634,"className":"org.seleniumhq.jetty7.http.HttpParser","methodName":"parseNext"},
{"fileName":"HttpParser.java","lineNumber":230,"className":"org.seleniumhq.jetty7.http.HttpParser","methodName":"parseAvailable"},
{"fileName":"BlockingHttpConnection.java","lineNumber":66,"className":"org.seleniumhq.jetty7.server.BlockingHttpConnection","methodName":"handle"},
{"fileName":"SocketConnector.java","lineNumber":254,"className":"org.seleniumhq.jetty7.server.bio.SocketConnector$ConnectorEndPoint","methodName":"run"},
{"fileName":"QueuedThreadPool.java","lineNumber":599,"className":"org.seleniumhq.jetty7.util.thread.QueuedThreadPool","methodName":"runJob"},
{"fileName":"QueuedThreadPool.java","lineNumber":534,"className":"org.seleniumhq.jetty7.util.thread.QueuedThreadPool$3","methodName":"run"},
{"lineNumber":-1,"className":"java.lang.Thread","methodName":"run"}]}}

How to open a link in a new tab or window and switch to it with Selenium WebDriver

The following codes are works both in Firefox and Chrome browsers.

Open a link in a new window and switch to it:

How to test different resolutions – Part one – Chrome resolutions testing

Often testing can mean checking a sites pages in different resolutions. When a site gets a new layout, it is important to make sure it displays correctly on different browsers and resolutions. While adjusting the resolution manually is an option, it takes too long.
In this post we will talk about resolution testing on Chrome. We were testing some possibilities, and will share our experience with you. We tried a few extensions to see which of them can speed up or make our work easier for us.
Their drawback of these extensions is that web-sites won’t recognize small resolutions as handheld devices, because the browser windows are only re-sized. This unfortunately means that you can’t see what a site would look like, on Chrome, on a mobile device. However they are still useful for desktop testing, and if you don’t want to pay for the service, the following extensions are a must try.

Quick and dirty test parametrization – JUnit params

Today I want to talk about a little tool which makes the life of us testers a bit easier.
It is called Junit params.

The  following situation happened with me not so long ago:
The dev team lead asked us a favor to make a set of quick and dirty smoke tests which tests different sites and check if some elements are present.
There are of course different approaches, if we are lucky the function is already automated and documented somewhere, and we just have to make it accessible and runnable by others too.

How to count number of images available on a web page then download them using selenium webdriver?

The simplest way to gather the images from a website is to find them by tagName.
First we create a WebElement list with the elements which tagName is ”img”.
Then we check the elements to be sure that they are displayed.
If they are displayed then we get their names and sources.
To download them we use the “org.apache.commons.io.FileUtils.copyURLToFile” function.
It is very simple, we just need to add the URL and the file path to it.
In the end we write out the number of displayed elements.

Here is to full code:

Screen recording – screencast – part III

In my previous posts I have described my favorite solutions for screen recording(Screen recording – screencast – part I and Screen recording – screencast – part II). Applications are free, just have to create an account. They have some limitations, but for the basic screen recording and image editing, they will work perfectly.
This time I will describe some paid/professional and easy-to-use programs. When we are talking about professional image/video editing, we have to say that there are many applications for this purpose. Most of them are very expensive, but they give lot of tools for image/video editing.

Graphwalker – Guards and actions

In our previous articles we have already discussed about the finite state machines and about how we can take advantage from this utility of the Graphwalker API. It is based on nodes and vertices which are connected to each other to become a graph and forming a test case to make your job easier and clear.
Moreover Graphwalker provides additional possibilities to make the job be in a batter quality. The edges could be extended by expressions and these can modify the further way of test run. Excluding the label the edge can contain two other elements. The first one called guard and another one is action. These have a unique order like this: Label [Guard] / Action1; Action2;

Graphwalker Path generators and Stop conditions

In this post we will try to give some ideas how to use the Graphwalker Path generators combined with Stop conditions. We recommend you to read our latest post about the Graphwalker API , however here is the snippet of code from last post:

ModelHandler modelHandler = new ModelHandler();
File loginModelFile = new File("'your location for the graphml file'/LoginModel.graphml");
Test testModel = new Test(loginModelFile, true, new A_StarPathGenerator(new EdgeCoverage(1.0)), false);
modelHandler.add("Test", testModel);
modelHandler.execute("Test");
Assert.assertTrue(modelHandler.isAllModelsDone());
String actualResult = modelHandler.getStatistics();
System.out.println(actualResult);
testModel.getDriver().quit();