Simulating Keystrokes in Browsers

Applies to TestComplete 15.70, last modified on December 17, 2024

Generally, simulating keystrokes in web browsers is similar to simulating keystrokes in other applications. So, all the techniques described in the Simulating Keystrokes topic can be applied to browsers as well. However, there are some aspects and techniques that improve interaction with web browsers. This topic describes these aspects.

Entering Text in the Address Bar

  • Important: Use the Navigate keyword test operation or the Navigate scripting method to open web pages. When possible, avoid automating actions against the address bar. See Navigate to Web Pages for examples.

  • TestComplete records text input into the browser’s address bar by using the Navigate operation or the Navigate scripting method. However, the Navigate operation is not recorded if you do not perform any actions (clicks, text input and so on) on the open page.

  • If you need to simulate typing text in Internet Explorer's address bar, you can use the Keys method.

Interaction With Web Page Controls

  • TestComplete exposes methods and properties of objects that correspond to web page elements. So, when you need to check or modify some values on a web page, it is better to use native members of appropriate controls. To find out which members are exposed, explore the web page structure in the Object Browser.

    Note: The following code demonstrates how to assign text to the value property. The sample procedure works with the edit boxes located on the https://services.smartbear.com/Samples/TestComplete15/WebOrders/Login.aspx web page.

    JavaScript, JScript

    function Test()
    {

      var url = "http://services.smartbear.com/samples/TestComplete15/WebOrders/Login.aspx";
      Browsers.Item(btIExplorer).Run(url);
      var page = Sys.Browser("*").Page("*");

      page.NativeWebObject.Find("id", "ctl00_MainContent_username", "input").value = "Tester";
      page.NativeWebObject.Find("id", "ctl00_MainContent_password", "input").value = "test";
      page.NativeWebObject.Find("id", "ctl00_MainContent_login_button", "input").Click();

    }

    Python

    def Test():
      url = "http://services.smartbear.com/samples/TestComplete14/WebOrders/Login.aspx";
      Browsers.Item[btIExplorer].Run(url);
      page = Sys.Browser("*").Page("*");
    
      page.NativeWebObject.Find("id", "ctl00_MainContent_username", "input").value = "Tester";
      page.NativeWebObject.Find("id", "ctl00_MainContent_password", "input").value = "test";
      page.NativeWebObject.Find("id", "ctl00_MainContent_login_button", "input").Click();

    VBScript

    Sub Test

      Dim url, page
      url = "http://services.smartbear.com/samples/TestComplete15/WebOrders/Login.aspx"
      Browsers.Item(btIExplorer).Run url
      Set page = Sys.Browser("*").Page("*")

      page.NativeWebObject.Find("id", "ctl00_MainContent_username", "input").value = "Tester"
      page.NativeWebObject.Find("id", "ctl00_MainContent_password", "input").value = "test"
      page.NativeWebObject.Find("id", "ctl00_MainContent_login_button", "input").Click

    End Sub

    DelphiScript

    procedure Test();
    var url, page;
    begin

      url := 'http://services.smartbear.com/samples/TestComplete15/WebOrders/Login.aspx';
      Browsers.Item(btIExplorer).Run(url);
      page := Sys.Browser('*').Page('*');

      page.NativeWebObject.Find('id', 'ctl00_MainContent_username', 'input').value := 'Tester';
      page.NativeWebObject.Find('id', 'ctl00_MainContent_password', 'input').value := 'test';
      page.NativeWebObject.Find('id', 'ctl00_MainContent_login_button', 'input').Click;

    end;

    C++Script, C#Script

    function Test()
    {

      var url = "http://services.smartbear.com/samples/TestComplete15/WebOrders/Login.aspx";
      Browsers["Item"](btIExplorer)["Run"](url);
      var page = Sys["Browser"]("*")["Page"]("*");
      page["NativeWebObject"]["Find"]("id", "ctl00_MainContent_username", "input")["value"] = "Tester";
      page["NativeWebObject"]["Find"]("id", "ctl00_MainContent_password", "input")["value"] = "test";
      page["NativeWebObject"]["Find"]("id", "ctl00_MainContent_login_button", "input")["Click"]();

    }

  • During the recording, TestComplete analyzes actions on the controls and substitutes keyboard and mouse actions with actions on the corresponding native members. For example, input into an edit box will be interpreted as textbox1.Text = "New Text" rather than textbox1.Keys("New Text[Enter]").

See Also

Web Testing - Examples
Simulating Keystrokes
Differences Between Simulated User Actions and Real User Actions

Highlight search results