Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How to get the page source from a BrowserWindow

Micro Focus Contributor
Micro Focus Contributor
0 0 335

There are times when the source of a web page needs to be analyzed instead of just looking at individual GUI elements that appear on the web page.

Typically a user can view the source of a web page by right-clicking on the web page and selecting the menu item View Page Source (this varies slightly for each browser type). This typically brings up another window or tab that shows the source HTML for the web page. Code could be written to automate gathering this information and performing the required analysis. 

The problem here is that each web browser shows the information in a different way and it is difficult and cumbersome to gather this in a simple and straightforward manner. This gets even more difficult with mobile browsers which may not have a way of doing this at all.

BrowserWindow.GetPageSource() to the Rescue

The function GetPageSource() does exactly what it says, it retrieves the page source of the current web page and returns the function part of the BrowserWindow object to the caller as a string.

Example

The following example shows two usages of this function. The code is written in Silk4J but it is easily adaptable to Silk4Net (C#) or a Workbench VB.Net Script.

The code performs some automation to get to a specific page, getPageSource() is called and the returned string is searched for the existence of an IMG tag.

The remainder of the code saves the text of the page into a text file so that subsequent executions of the script will validate that the contents are the same. A helper function (using the SystemFunctions object) is used to create the files.

import com.borland.silktest.jtf.BrowserBaseState;
import org.junit.Before;
import org.junit.Test;
import com.borland.silktest.jtf.xbrowser.DomLink;
import com.borland.silktest.jtf.xbrowser.DomElement;
import com.borland.silktest.jtf.Desktop;
import com.borland.silktest.jtf.SystemFunctions;
import com.borland.silktest.jtf.SystemFunctions.FileHandle;
import com.borland.silktest.jtf.xbrowser.BrowserWindow;
import org.junit.Assert;

public class MyClass {

  private Desktop desktop = new Desktop();

  @Before
  public void baseState() {
    // Go to web page 'demo.borland.com'
    BrowserBaseState baseState = new BrowserBaseState();
    baseState.execute(desktop);
  }

  @Test
  public void MyTestMethod() {
    desktop.<DomLink>find("demo_borland_com.BrowserWindow.Demo Application").click();
    desktop.<DomElement>find("demo_borland_com.BrowserWindow.http   demo borland").click();

    BrowserWindow wnd = desktop.<BrowserWindow>find("demo_borland_com.BrowserWindow");

    // Get the page source
    String pageSource = wnd.getPageSource();

    // Validate that the page contains the image tag
    int position = pageSource.indexOf("<img src=\"images/tilesV.png\" />");
    Assert.assertEquals(position != -1, true);

    // Remove text that varies, in this case an ID.
    pageSource = pageSource.replaceAll("j_id[0-9]+", "j_id{nnn}");
    
    // The first time the test is executed save the page source as the expected text.
    // Subsequent executions compare the current values with the expected.
    
    SystemFunctions sys = desktop.getSystemFunctions();
    String expectedFile = "C:\\Temp\\NewLetterPage-expected.txt";
    String actualFile = "C:\\Temp\\NewLetterPage-actual.txt";

    if(sys.fileExists(expectedFile)) {
      // Expected file already exists, create the actual file 
      Assert.assertTrue(createFile(sys, actualFile, pageSource));
      
      // Compare actual expected and actual files
      Assert.assertEquals(true,  sys.compareTextFiles(expectedFile, actualFile));
    }
    else {
      // Expected file does not exist, create it here
      Assert.assertTrue(createFile(sys, expectedFile, pageSource));
    }
  }

  // Helper function to create a file.
  private boolean createFile(SystemFunctions sys, String fileName, String contents) {
    FileHandle handle = sys.fileOpen(fileName, 
                                     SystemFunctions.FileOpenMode.WRITE,
                                     SystemFunctions.FileShareMode.DENY_ALL,
                                     SystemFunctions.CharSet.UTF8);
    if(null != handle) {
      sys.fileWriteLine(handle, contents);
      sys.fileClose(handle);
      return true;
    }
    return false;
  }
}

Conclusion

GetPageSource() can be useful when other methods of gathering the source of a web page are impractical. It also offers ways of validating elements of a web page that are not visualized in the GUI of the web browser.

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.