Working With Check Box Controls in Web Applications

Applies to TestComplete 15.31, last modified on March 17, 2022

TestComplete recognizes standard <input> elements of the checkbox type in web applications and on web pages as Web Check Box objects. It can also recognize check box controls implemented with third-party libraries and frameworks.

In your tests, you can work with these elements using methods and properties of the Web Check Box object. In addition, you can access attributes of the underlying web element and properties and methods provided to the object by a web browser in which the tested web page or web application is running.

Select or clear check box

In keyword tests

  1. Add the On-Screen Action operation to your keyword test. TestComplete will show the Operation Parameters wizard.

  2. In the wizard, specify the check box you want to select or clear. You can do it by pointing to the check box on the web page.

  3. Select the check box object’s method to call. To simulate selecting or clearing a check box, use the ClickChecked method.

  4. To select or clear the check box, set the method’s parameter to True or False respectively.

  5. Click Finish to add the operation and close the wizard.

Selecting a check box on a web page

Click the image to enlarge it.

In script tests

To simulate selecting or clearing a check box, use the WebCheckBoxObj.ClickChecked(Checked) method.

The script sample below shows how to simulate selecting a check box on a web page:

Web (Cross-Platform)

JavaScript, JScript

function Sample_Select_CheckBox()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers.Item(btChrome).Run(url);

  var cb = Sys.Browser().Page(url).FindElement("#RememberMe");

  // Select the Remember me check box
  cb.ClickChecked(true);
}

Python

def Sample_Select_CheckBox():
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item[btChrome].Run(url)

  cb = Sys.Browser().Page(url).FindElement("#RememberMe")

  # Select the Remember me check box
  cb.ClickChecked(True)

VBScript

Sub Sample_Select_CheckBox()
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item(btChrome).Run(url)

  Set cb = Sys.Browser().Page(url).FindElement("#RememberMe")

  ' Select the Remember me check box
  Call cb.ClickChecked(True)
End Sub

DelphiScript

procedure Sample_Select_CheckBox();
var url, cb;
begin
  url := 'https://services.smartbear.com/samples/TestComplete15/smartstore/login';
  Browsers.Item(btChrome).Run(url);

  cb := Sys.Browser().Page(url).FindElement('#RememberMe');

  // Select the Remember me check box
  cb.ClickChecked(true);

end;

C++Script, C#Script

function Sample_Select_CheckBox()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers["Item"](btChrome).Run(url);

  var cb = Sys["Browser"]()["Page"](url)["FindElement"]("#RememberMe");

  // Select the Remember me check box
  cb["ClickChecked"](true);
}

Web (Classic)

JavaScript, JScript

function Sample_Select_CheckBox()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers.Item(btChrome).Run(url);

  var PropNames = new Array("ObjectType", "ObjectIdentifier");
  var PropValues = new Array("Checkbox", "RememberMe");

  var cb = Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, true);

  // Select the Remember me check box
  cb.ClickChecked(true);
}

Python

def Sample_Select_CheckBox():
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item[btChrome].Run(url)

  PropNames = ["ObjectType", "ObjectIdentifier"]
  PropValues = ["Checkbox", "RememberMe"]

  cb = Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, True);

  # Select the Remember me check box
  cb.ClickChecked(True)

VBScript

Sub Sample_Select_CheckBox()
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item(btChrome).Run(url)

  PropNames = Array("ObjectType", "ObjectIdentifier")
  PropValues = Array("Checkbox", "RememberMe")

  Set cb = Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, True)

  ' Select the Remember me check box
  Call cb.ClickChecked(True)
End Sub

DelphiScript

procedure Sample_Select_CheckBox();
var url, PropNames, PropValues, cb;
begin
  url := 'https://services.smartbear.com/samples/TestComplete15/smartstore/login';
  Browsers.Item(btChrome).Run(url);

  PropNames := ['ObjectType', 'ObjectIdentifier'];
  PropValues := ['Checkbox', 'RememberMe'];

  cb := Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, true);

  // Select the Remember me check box
  cb.ClickChecked(true);

end;

C++Script, C#Script

function Sample_Select_CheckBox()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers["Item"](btChrome).Run(url);

  var PropNames = new Array("ObjectType", "ObjectIdentifier");
  var PropValues = new Array("Checkbox", "RememberMe");

  var cb = Sys["Browser"]()["Page"](url)["FindChildEx"](PropNames, PropValues, 15, true);

  // Select the Remember me check box
  cb["ClickChecked"](true);
}

Get check box state

In your tests, you may need to check the state of a check box on a web page. To do this, you can use properties provided by the web browsers in which the tested web page is opened, for example, the checked property, or the attributes of the underlying web element, for example, the value attribute.

In keyword tests

In keyword tests, to get the check box state and perform various actions depending on the result, you can use the If … Then and Else operations:

  1. Add the If … Then operation to your test.

  2. Specify the condition that will get the check box state and compare it against the expected value. For example:

    1. In the Value 1 column of the Operation Parameters dialog, click the ellipsis button.

    2. In the resulting Edit Value dialog, set the mode to Object Property.

    3. Set the value to Onscreen Object.

    4. Point to the check box on the web page.

    5. To get the check box state, select the checked property or the attributes property, and then the value attribute. Which property to use depends on the check box implementation.

    6. Click OK to close the dialog.

    7. In the Value 2 column, enter the baseline value against which the check box state will be compared: true for selected and false for cleared.

    8. Click OK to save the changes and close the Operation Parameters dialog.

  3. To specify operations to run if the check box has the expected state, add them as child items of the If … Then operation.

  4. To make the operation run otherwise, add the Else operation right after the If … Then operation, and then add the needed operations as child items of the Else operation.

Getting a check box state on a web page

Click the image to enlarge it.

In script tests

To get the state of a check box in script tests, you can use properties of the Web Check Box object. For example, you can use the checked property provided by a web browser or you can use the value attribute of the underlying web element, depending on the check box implementation. The following code sample shows how you can get the state of a check box:

Web (Cross-Platform)

JavaScript, JScript

function Sample_CheckBox_State()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers.Item(btChrome).Run(url);

  var cb = Sys.Browser().Page(url).FindElement("#RememberMe");

  // Check if the Remember me check box is selected
  if (cb.checked)
    Log.Message("The Remember me check box is selected.");
  else
    Log.Message("The Remember me check box is cleared.");
}

Python

def Sample_CheckBox_State():
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item[btChrome].Run(url)

  cb = Sys.Browser().Page(url).FindElement("#RememberMe")

  # Check if the Remember me check box is selected
  if (cb.checked):
    Log.Message("The Remember me check box is selected.")
  else:
    Log.Message("The Remember me check box is cleared.")

VBScript

Sub Sample_CheckBox_State()
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item(btChrome).Run(url)

  Set cb = Sys.Browser().Page(url).FindElement("#RememberMe")

  ' Check if the Remember me check box is selected
  If cb.checked Then
    Log.Message("The Remember me check box is selected.")
  Else
    Log.Message("The Remember me check box is cleared.")
  End If
End Sub

DelphiScript

procedure Sample_CheckBox_State();
var url, cb;
begin
  url := 'https://services.smartbear.com/samples/TestComplete15/smartstore/login';
  Browsers.Item(btChrome).Run(url);

  cb := Sys.Browser().Page(url).FindElement('#RememberMe');

  // Check if the Remember me check box is selected
  if cb.checked then
    Log.Message('The Remember me check box is selected.')
  else
    Log.Message('The Remember me check box is cleared.');
end;

C++Script, C#Script

function Sample_CheckBox_State()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers["Item"](btChrome).Run(url);

  var cb = Sys["Browser"]()["Page"](url)["FindElement"]("#RememberMe");

  // Check if the Remember me check box is selected
  if (cb["checked"])
    Log["Message"]("The Remember me check box is selected.");
  else
    Log["Message"]("The Remember me check box is cleared.");
}

Web (Classic)

JavaScript, JScript

function Sample_CheckBox_State()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers.Item(btChrome).Run(url);

  var PropNames = new Array("ObjectType", "ObjectIdentifier");
  var PropValues = new Array("Checkbox", "RememberMe");

  var cb = Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, true);

  // Check if the Remember me check box is selected
  if (cb.checked)
    Log.Message("The Remember me check box is selected.");
  else
    Log.Message("The Remember me check box is cleared.");
}

Python

def Sample_CheckBox_State():
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item[btChrome].Run(url)

  PropNames = ["ObjectType", "ObjectIdentifier"]
  PropValues = ["Checkbox", "RememberMe"]

  cb = Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, True);

  # Check if the Remember me check box is selected
  if (cb.checked):
    Log.Message("The Remember me check box is selected.")
  else:
    Log.Message("The Remember me check box is cleared.")

VBScript

Sub Sample_CheckBox_State()
  url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login"
  Browsers.Item(btChrome).Run(url)

  PropNames = Array("ObjectType", "ObjectIdentifier")
  PropValues = Array("Checkbox", "RememberMe")

  Set cb = Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, True)

  ' Check if the Remember me check box is selected
  If cb.checked Then
    Log.Message("The Remember me check box is selected.")
  Else
    Log.Message("The Remember me check box is cleared.")
  End If
End Sub

DelphiScript

procedure Sample_CheckBox_State();
var url, PropNames, PropValues, cb;
begin
  url := 'https://services.smartbear.com/samples/TestComplete15/smartstore/login';
  Browsers.Item(btChrome).Run(url);

  PropNames := ['ObjectType', 'ObjectIdentifier'];
  PropValues := ['Checkbox', 'RememberMe'];

  cb := Sys.Browser().Page(url).FindChildEx(PropNames, PropValues, 15, true);

  // Check if the Remember me check box is selected
  if cb.checked then
    Log.Message('The Remember me check box is selected.')
  else
    Log.Message('The Remember me check box is cleared.');
end;

C++Script, C#Script

function Sample_CheckBox_State()
{
  var url = "https://services.smartbear.com/samples/TestComplete15/smartstore/login";
  Browsers["Item"](btChrome).Run(url);

  var PropNames = new Array("ObjectType", "ObjectIdentifier");
  var PropValues = new Array("Checkbox", "RememberMe");

  var cb = Sys["Browser"]()["Page"](url)["FindChildEx"](PropNames, PropValues, 15, true);

  // Check if the Remember me check box is selected
  if (cb["checked"])
    Log["Message"]("The Remember me check box is selected.");
  else
    Log["Message"]("The Remember me check box is cleared.");
}

See Also

Working With Application Objects and Controls
Web and RIA Testing

Highlight search results