How to perform a long click on a mobile object

How to perform a long click on a mobile object

In the current release of Silk Test, it is currently not possible to declare a specific duration for a Click() or LongClick() method call. 

It is however possible to use the following workaround detailed in this article to perform such an action.

The workaround involves using the center points of the desired control to perform a swipe action with the start and end points of the swipe being the same. This will effectively perform in the same way as a user touching a single point on the screen for a specified period of time.

1) To retrieve the coordinates of the required control, return the absolute rectangle of the object where the long click should be executed.

  Rectangle controlRect = _desktop.MobileObject(locator).GetRect(true);

2) Find the Mobile Device and execute "swipeWithSegments" with the center of the absolute rectangle of the object twice and add a delay of 10 seconds for the 'delayBetweenSteps' parameter.

_desktop.MobileDevice("//MobileDevice").SwipeWithSegments(new List<Point>() { controlRect.Center, controlRect.Center }, 5, 10000);
 

 For efficiency and practicality, you can wrap the code within a custom function and pass in the locator of the object and the desired duration as parameters. Below you can find examples of this for each Silk Test client.

Silk4NET (C#)

[TestMethod]
        public void PerformLongClick()
        {
           LongPress("//MobileDevice//MobileObject[@content-desc='Play Store']", 10000);
        }


        public void LongPress(String locator, int duration)
        {
            Rectangle controlRect = _desktop.MobileObject(locator).GetRect(true);
            _desktop.MobileDevice("//MobileDevice").SwipeWithSegments(new List<Point>() { controlRect.Center, controlRect.Center }, 5, duration);

        }

 

Silk4J

@Test
    public void PerformLongClick() {        
        longPress("//MobileDevice//MobileObject[@content-desc='Play Store']", 10000);        
    }
    
        
    public void longPress(String locator, int duration) {        
        Rect controlRect = desktop.<MobileObject> find(locator).getRect(true);    
        desktop.<MobileDevice>find("//MobileDevice").swipeWithSegments(Arrays.asList(controlRect.getCenter(),controlRect.getCenter()), 5, duration);            
    }

 

Silk Test Workbench (Vb.NET)

    Public Sub Main()
        LongPress("//MobileDevice//MobileObject[@content-desc='Play Store']", 10000)        
    End Sub
    
    Public Function LongPress(locator As String, duration As Integer)        
        Dim controlRect As Rectangle = _desktop.MobileObject(locator).GetRect(True)
        Dim points As New List(Of Point)
        points.Add(controlRect.Center)
        points.Add(controlRect.Center)
        _desktop.MobileDevice("//MobileDevice").SwipeWithSegments(points, 5, duration)            
    End Function

 

Silk Test Classic

[TestMethod]
        public void PerformLongClick()
        {
           LongPress("//MobileDevice//MobileObject[@content-desc='Play Store']", 10000);
        }


        public void LongPress(String locator, int duration)
        {
            Rectangle controlRect = _desktop.MobileObject(locator).GetRect(true);
            _desktop.MobileDevice("//MobileDevice").SwipeWithSegments(new List<Point>() { controlRect.Center, controlRect.Center }, 5, duration);

        }

 


DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2018-06-08 11:55
Updated by:
 
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.