Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How do I access the unparsed HTML of a page?

Options
LegacyForum
LegacyForum Posts: 1,664 ✭✭
Getting the unparsed HTML of the page is not an exposed API, although it
can be done easily. Our example will show how to do this from the paths
tree.

Highlight the primary path node at which you want to get the HTML. On the
right, choose the Actions tab, then click the Tools button. Click the New
button at the bottom. You will get a chooser asking you what kind of tool
you would like to create. Choose Method. In the Language box choose Java.
Specify the class (including the package), along with the method from that
class that you would like to execute. The class that you write must be on
your system classpath, or placed in the build directory in the WebKing
installation directory, or in a jar file that sits in the WebKing
installation directory.

Here is a sample class that might do something similiar to what you want:

import com.parasoft.api.*;
import webtool.site.*;

public class HTMLTest {
public static void testHTML(WebItem item) {
String url = item.getURL();
if (url.equals("http://www.parasoft.com/jsp/home.jsp")) {
String html = item.getCachedHTML();
// Do some check here on the html
if (!isGoodHTML(html)) {
Application.report("Test Functionality", "HTML error found",
item, 0, null);
}
}
}
private static boolean isGoodHTML(String html) {
return false;
}
}

The classes and methods that you are interested in are:

webtool.site.WebItem - This refers to a single web page.
getURL() - returns the URL of the web page. If the primary node to
which you add a Method Tool in the Actions tab has more than one subitem,
then you need to check this and only do your test for the pages that match
the URL that you want to test.
getCachedHTML() - returns the html of the page as a string.

com.parasoft.api.Application
report() - Use the 5-argument report method. Javadocs for that class
can be found by going to Help > Scripting API from the menu bar. The first
argument reports the error to a named error view. You want this name to be
the same as the name of whatever tool you are running. If running "Test
Path" from the menu or the "Test Functionality" tool, use the name "Test
Functionality". If testing using a CodeWizard tool, use the name of that
tool. I have added a feature request to add a report method that adds the
error to whatever is the "current" error view, so you don't have to specify
the name.