e-testing Blog

Extending HP UFT with Selenium Part 2

UFT and Selenium 1In the last article, we went through some of the reasons for considering integrating Selenium into HP UFT. This article will walk you through the process in more detail.

 

There is more than one approach to tackling the task. We could:

1. rebuild the Selenium .NET libraries to make them accessible to COM and then make a library to interact with the newly compiled libraries.

2. Create a new .NET project which references the Selenium Libraries and exposes a webdriver object.

3. Implement the JSONWireProtocol directly.

Option 1 would be a good solution if the .NET libraries were already compiled for COM usage. Maybe this could be a task to take back to SeleniumHQ.

Option 2 gets around the issue of COM exposure but we will now have two projects to manage. One in .NET and a VBScript library to interact with it.

However, Option 3 not only will get us up and running, it will also give us the chance to get a better understanding of how Selenium works, which is handy when things don’t go according to plan. Remember, we are taking a step into the open source world and whilst there are lots of friendly people willing to lend a hand, it isn’t the same as an HP service contract.

First, we need to download Selenium Server. We will use this for testing out our library before using a cloud-based provider.

At the time of writing, the latest version is 2.41 so can be downloaded with this link.

http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar

However, it may be best to check there isn’t a more recent stable release at SeleniumHQ

http://docs.seleniumhq.org/download/

Let’s run this jar file from a command line (at the command prompt type java -jar selenium-server-standalone-2.41.0.jar assuming you are in the directory you downloaded to)

selenium server

It will now sit and listen for requests, by default on port 4444. (the eagle-eyed among you will notice I have mine running on port 4445 but sticking with the default should be fine)

Any properly formed request made to it will get a valid response. Let’s see what happens when we navigate to it with a browser. Open a browser of your choice and go to the url http://localhost:4444/wd/hub

WDHub

The service is up.

So, let’s talk to it through UFT:

Open UFT and create a new test.

To talk to it we need to use a library that handles HTTP traffic. There are several HTTP libraries available for VBScript but not all behave correctly across 32 and 64 bit machines, so I’ve chosen WinHttpRequest.

We need to create an instance of the new HTTP request object

Set objWebDriver = CreateObject(“WinHttp.WinHttpRequest.5.1”)

Now we want to create a session, so we send a message asking to create a new session.

objWebDriver.Open “POST”, “http://localhost:4444/wd/hub/session”, true

We need to tell it we will accept a JSON object by return

objWebDriver.setRequestHeader “Content-type”, “application/json;charset=UTF-8”

we also need to tell it what type or browser we want to use – in this case Chrome.

objWebDriver.send “{“”sessionId””: null, “”desiredCapabilities””: {“”browserName””: “”chrome””}}”

then we need to wait for the response

objWebDriver.WaitForResponse

… and collect the response

response = objWebDriver.responseText

then put the reply in a messagebox:

msgbox response

then cross fingers and run the test…

response

Here comes our first challenge- reading the JSON reply.

Some libraries exist for parsing JSON in VBScript but I haven’t found one I am happy with. Also, the information we want to extract isn’t particularly complex, so I have made a JSON parser function which just does enough to get what we need. We can go through that later.

sessionId = getJsonValue(response, “sessionId”)

Now we have the session id we can go places.

and all our future requests will have this session id in them.

To learn more about the JsonWireProtocol, check out the project documentation here

Our next challenge is how to use/reuse our object repositories. And this we will cover in the next post.

 

CLICK HERE FOR UPDATES

Subscribe to our RSS feed and get the latest updates in your inbox weekly

logo