Navigating to Web Pages in Hybrid Applications (Legacy)

Applies to TestComplete 15.48, last modified on March 01, 2023
The information below concerns legacy mobile tests that work with mobile devices connected to the local computer. For new mobile tests, we recommend using the newer cloud-compatible approach.

This topic explains how you can navigate to web pages in a hybrid application.

Basic Concepts

In hybrid mobile applications, you typically do not open a web page by entering its URL. You do this by touching buttons in the application or links in the web page that the application already displays. To simulate these actions, you need to simulate the touches on buttons and links.

If you need to open a page using a URL, call the WebView.ToUrl or Page.ToUrl method and pass the desired URL to it as a parameter. See the example below.

Two notes:

  • We recommend specifying the protocol in the URL. Otherwise, the page may fail to load. For example, use http://www.example.com rather than www.example.com.

    • HTML files that come as part of your Android application are stored in the android_asset folder on the mobile device. To open an HTML file that is stored on a device, use a URL like file:///android_asset/PageName.html.

    • HTML files that come as part of your iOS application are stored in the android_asset folder on the mobile device. To open an HTML file that is stored on a device, use a URL like >file:///private/var/mobile/containers/bundle/application/*/Application.ipa/PageName.html.

  • The ToUrl methods opens the specified web page and pauses the test run until the WebView control signals it has loaded the page. In some cases, this moment may differ from the time point when the page load is over. To learn how to wait until a page is loaded completely, see Waiting for Web Pages in Hybrid Mobile Applications (Legacy).

Script Example

The following code snippet demonstrates how you can use the WebView.ToUrl and Page.ToUrl methods to open pages by their URLs in a hybrid Android application:

JavaScript, JScript

function WebViewNavigation()
{
  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Run the hybrid application
  TestedApps.WebViewSample.Run();

  // Obtain the WebView object
  // On Android devices
  var p = Mobile.Device().Process("smartbear.webview_sample");
  var WebView = p.RootLayout("").WebView("mainWebView");
  // On iOS devices
  // var p = Mobile.Device().Process("SampleWebViewApp");
  // var WebView = p.Window(0).WebView(0);

  // Navigate to the web page
  WebView.ToUrl("http://en.m.wikipedia.org/");
  
  // Wait for the page to load and post the page url to the log
  var page = WebView.WaitPage("*");
  Log.Message(page.URL);

  // Navigate to the different web page and post new page url to the log
  page.ToUrl("http://www.example.com/");
  Log.Message(page.URL);
}

Python

def WebViewNavigation():
  # Select the mobile device
  Mobile.SetCurrent("MyDevice");

  # Run the hybrid application
  TestedApps.WebViewSample.Run();

  # Obtain the WebView object
  # On Android devices
  p = Mobile.Device().Process("smartbear.webview_sample");
  WebView = p.RootLayout("").WebView("mainWebView");
  # On iOS devices
  # p = Mobile.Device().Process("SampleWebViewApp");
  # WebView = p.Window(0).WebView(0);

  # Navigate to the web page
  WebView.ToUrl("http://en.m.wikipedia.org/");
  
  # Wait for the page to load and post the page url to the log
  page = WebView.WaitPage("*");
  Log.Message(page.URL);

  # Navigate to the different web page and post new page url to the log
  page.ToUrl("http://www.example.com/");
  Log.Message(page.URL);

VBScript

Sub WebViewNavigation()
  Dim p, WebView, page

  ' Select the mobile device
  Mobile.SetCurrent("MyDevice")
  ' Run the hybrid application
  TestedApps.WebViewSample.Run

  ' Obtain the WebView object
  ' On Android devices
  Set p = Mobile.Device.Process("smartbear.webview_sample")
  Set WebView = p.RootLayout("").WebView("mainWebView")
  ' On iOS devices
  ' Set p = Mobile.Device.Process("SampleWebViewApp")
  ' Set WebView = p.Window(0).WebView(0)

  ' Navigate to the web page
  WebView.ToUrl("http://en.m.wikipedia.org/")
  
  ' Wait for the page to load and post the page url to the log
  Set page = WebView.WaitPage("*")
  Log.Message(page.URL)

  ' Navigate to the different web page and post new page url to the log
  page.ToUrl("http://www.example.com/")
  Log.Message(page.URL)
End Sub

DelphiScript

function WebViewNavigation();
var
  p, WebView, page: OleVariant;
begin
  // Select the mobile device
  Mobile.SetCurrent('MyDevice');

  // Run the hybrid application
  TestedApps.WebViewSample.Run;

  // Obtain the WebView object
  // On Android devices
  p := Mobile.Device.Process('smartbear.webview_sample');
  WebView := p.RootLayout('').WebView('mainWebView');
  // On iOS devices
  // p := Mobile.Device.Process('SampleWebViewApp');
  // WebView := p.Window(0).WebView(0);

  // Navigate to the web page
  WebView.ToUrl('http://en.m.wikipedia.org/');
  
  // Wait for the page to load and post the page url to the log
  page := WebView.WaitPage('*');
  Log.Message(page.URL);

  // Navigate to the different web page and post new page url to the log
  page.ToUrl('file:///android_asset/index.html');
  Log.Message(page.URL);
end;

C++Script, C#Script

function WebViewNavigation()
{
  // Select the mobile device
  Mobile["SetCurrent"]("MyDevice");

  // Run the hybrid application
  TestedApps["WebViewSample"]["Run"]();

  // Obtain the WebView object
  // On Android devices
  var p = Mobile["Device"]["Process"]("smartbear.webview_sample");
  var WebView = p["RootLayout"]("")["WebView"]("mainWebView");
  // On iOS devices
  // var p = Mobile["Device"]["Process"]("SampleWebViewApp");
  // var WebView = p["Window"](0)["WebView"](0);

  // Navigate to the web page
  WebView["ToUrl"]("http://en.m.wikipedia.org/");
  
  // Wait for the page to load and post the page url to the log
  var page = WebView["WaitPage"]("*");
  Log["Message"](page["URL"]);

  // Navigate to the different web page and post new page url to the log
  page["ToUrl"]("file:///android_asset/index.html");
  Log["Message"](page["URL"]);
}

Navigating to Web Pages in Keyword Tests

To open web pages from keyword tests, call the WebView.ToUrl and Page.ToUrl methods with the Call Object Method or Run Code Snippet operation. For a detailed explanation of how to call methods from your keyword tests, see Calling Object Methods.

See Also

Common Tasks for Hybrid Mobile Application Testing (Legacy)
Testing Hybrid Mobile Applications - Overview (Legacy)
Waiting for Web Pages in Hybrid Mobile Applications (Legacy)

Highlight search results