Changing ScrollView Position

Applies to TestComplete 15.40, last modified on July 05, 2022
Changing the ScrollView Position

To scroll the content of a scroll view control, assign the required value to the wPositionX and wPositionY properties of the iOS ScrollView object that TestComplete associates with that control. These properties determine the horizontal and vertical positions of the scroll view. The following example sets the position of the ScrollView control to 20 pixels horizontally and 10 pixels vertically:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the ScrollView object
  var p = Mobile.Device().Process("SampleApp");
  var scrollview = p.Window().ScrollView();

  // Set ScrollView position
  scrollview.wPositionX = 20;
  scrollview.wPositionY = 10;
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the ScrollView object 
  p = Mobile.Device().Process("SampleApp")
  scrollview = p.Window().ScrollView()

  # Set ScrollView position
  scrollview.wPositionX = 20
  scrollview.wPositionY = 10

VBScript

Sub Test()
  Dim p, scrollview
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the ScrollView object
  Set p = Mobile.Device.Process("SampleApp")
  Set scrollview = p.Window.ScrollView

  ' Set ScrollView position
  ScrollView.wPositionX = 20
  Scrollview.wPositionY = 10
End Sub

DelphiScript

procedure Test();
var
  p, scrollview;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the ScrollView object
  p := Mobile.Device.Process('SampleApp');
  scrollview := p.Window.ScrollView;

  // Set ScrollView position
  scrollview.wPositionX := 20;
  scrollview.wPositionY := 10;
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the ScrollView object
  var p = Mobile["Device"].Process("SampleApp");
  var scrollview = p["Window"]()["ScrollView"]();

  // Set ScrollView position
  scrollview["wPositionX"] = 20;
  scrollview["wPositionY"] = 10;
}

If the scroll view is in paging mode, iOS cannot detect a page change when you set the controls position. This is due to the way the wPosition property is implemented. If you need to scroll exactly one screen, use the methods and properties TestComplete provides to work with scroll views in paging mode. For more information, see Working with ScrollView in Paging Mode.

As an alternative, you can change the position relatively to the control's Width. The following example scrolls a scroll view exactly one screen to the right:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the ScrollView object
  var p = Mobile.Device().Process("SampleApp");
  var scrollview = p.Window().ScrollView();

  // Set ScrollView position relative to control size
  scrollview.wPositionX = scrollview.wPositionX+scrollview.Width;
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the ScrollView object 
  p = Mobile.Device().Process("SampleApp")
  scrollview = p.Window().ScrollView()

  # Set ScrollView position relative to control size
  scrollview.wPositionX = scrollview.wPositionX+scrollview.Width

VBScript

Sub Test()
  Dim p, scrollview
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the ScrollView object
  Set p = Mobile.Device.Process("SampleApp")
  Set scrollview = p.Window.ScrollView

  ' Set ScrollView position relative to control size
  ScrollView.wPositionX = scrollview.wPositionX+scrollview.Width
End Sub

DelphiScript

procedure Test();
var
  p, scrollview;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the ScrollView object
  p := Mobile.Device.Process('SampleApp');
  scrollview := p.Window.ScrollView;

  // Set ScrollView position relative to control size
  scrollview.wPositionX := scrollview.wPositionX+scrollview.Width;
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the ScrollView object
  var p = Mobile["Device"].Process("SampleApp");
  var scrollview = p["Window"]()["ScrollView"]();

  // Set ScrollView position relative to control size
  scrollview["wPositionX"] = scrollview["wPositionX"]+scrollview["Width"];
}

Changing Position From Keyword Tests

To set the current scroll view position from keyword tests, call the methods described above by using the On-Screen Action or Call Object Method operation. See Calling Object Methods.

See Also

Working With iOS Scroll View Controls
Checking ScrollView's Position Range
wPositionX Property (iOS Controls)
wPositionY Property (iOS Controls)
Working with ScrollView in Paging Mode

Highlight search results