Forum Libraries, Components, and Plugins

Xamarin.UITest - What is the correct way to do a Swipe using an XPath in a hybrid app?

JorgePandoJorgePando USMember, University ✭✭

I'm trying to do a simple right to left swipe in a hybrid app (running on Android) as follows (in Repl):

app.SwipeRightToLeft(c=>c.XPath("//*[text() = 'MultiplePermissionsAlert']"))

I get the error:

(1,23): error CS1660: Cannot convert `lambda expression' to non-delegate type `double'
(1,6): error CS1502: The best overloaded method match for `Xamarin.UITest.Android.AndroidApp.SwipeRightToLeft(double, int, bool)' has some invalid      arguments
C:\QAAutomation\current\ParNetNG\ParNetNG\bin\Debug\Xamarin.UITest.dll (Location of the symbol related to previous error)
(1,23): error CS1503: Argument `#1' cannot convert `anonymous method' expression to type `double'

I know the XPath is correct because the simple Query:

app.Query(c=>c.XPath("//*[text() = 'MultiplePermissionsAlert']"))

Gives the following result:

Query for XPath("//*[text() = 'MultiplePermissionsAlert']") gave 1 results.
[0] {
    Id => "5af0a272a05cfb00101a9911_Name",
    NodeType => "ELEMENT_NODE",
    NodeName => "H2",
    Class => "",
    Html => "<h2 id="5af0a272a05cfb00101a9911_Name">MultiplePermissionsAlert</h2>",
    Value => null,
    WebView => "NoResourceEntry-100",
    TextContent => "MultiplePermissionsAlert",
    Rect => {
        Width => 510,
        Height => 38,
        CenterX => 370,
        CenterY => 962,
        Top => 447,
        Bottom => 0,
        Left => 58,
        Right => 0,
        X => 116,
        Y => 944

I can also "Tap" using that XPath. So, can someone please provide some example on how to Swipe on this element, given an XPath? I can't find any decent documentation. Thanks in advance.



  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai


    If you look at the API Docs for the IApp interface and scroll down to the SwipeRightToLeft methods, you will see that there are 4 method overloads with the following signatures:

    SwipeRightToLeft(Double, Int32, Boolean)
    Performs a right to left swipe gesture.
    SwipeRightToLeft(Func<Xamarin.UITest.Queries.AppQuery, Xamarin.UITest.Queries.AppQuery>, Double, Int32, Boolean)
    Performs a right to left swipe gesture on an element matched by 'query'.
    SwipeRightToLeft(Func<Xamarin.UITest.Queries.AppQuery, Xamarin.UITest.Queries.AppWebQuery>, Double, Int32, Boolean)
    Performs a right to left swipe gesture on an element matched by 'query'.
    SwipeRightToLeft(String, Double, Int32, Boolean)
    Performs a right to left swipe gesture on the matching element. If multiple elements are matched, the first one will be used.

    Your method call:

    app.SwipeRightToLeft(c=>c.XPath("//*[text() = 'MultiplePermissionsAlert']"))

    does not match any of them, so you will have to provide a couple of additional arguments, i.e.

    swipePercentage [a double type]
    How far across the element to swipe (from 0.0 to 1.0).
    swipeSpeed [an Int32 type]
    The speed of the gesture.
    withInertia [a boolean type]
    Whether swipes should cause inertia.

  • JorgePandoJorgePando USMember, University ✭✭

    Thank you. This is a step in the right direction. However, I am still having a problem with this. Given that the signature of the method I am trying to use is:

    SwipeRightToLeft(Func<Xamarin.UITest.Queries.AppQuery, Xamarin.UITest.Queries.AppWebQuery>, Double, Int32, Boolean)

    How do I express the XPath such that it complies with the type of the first parameter?

    For example, if I enter

    app.SwipeRightToLeft(c=>c.XPath("//*[text() = 'MultiplePermissionsAlert']"), 0.5, 1, false)

    it thinks I'm trying to use one of the other SwipeRightToLeft signatures (that has a string as the first parameter)

    error CS1660: Cannot convert lambda expression' to non-delegate typestring'. I guess the lambda expression isn't cutting it. I'd just like to see a simple example somewhere of using an XPath to do the swipe.


  • JorgePandoJorgePando USMember, University ✭✭

    Looks like the method has to have an AppQuery as the first parameter. Any attempt to use an XPath() method gives an AppWebQuery, which it doesn't like

  • JorgePandoJorgePando USMember, University ✭✭

    In the end, I couldn't find a way to directly use the SwipeLeftToRight method using an XPath. However, I went about it a different way. This seems to work (from blog:

    var rect = app.Query(c=>c.WebView().XPath("//*[text()='NopermissionsAlert']")).Single().Rect
    float fromCenterX = rect.CenterX * 0.9f
    app.DragCoordinates(rect.CenterX + fromCenterX, rect.CenterY, rect.CenterX - fromCenterX, rect.CenterY)

Sign In or Register to comment.