Check Button Color with Xamarin UITest

HaraldHarald ATMember
edited February 2016 in Xamarin Test Cloud

Hi all,

does anyone know, if it is possible to check if the color of an ui element has changed. In my case its a background color of a button, that changes if the user "klicks" it.

Thx for you help!

Best Answers

  • Accepted Answer

    You will have to probably call an invoke (i.e. invoke a native Android method) on the Button you're working with.

    I figured out really quick how to do it for the Button's text color, and it should be pretty similar for the button bakground color (or it might not be possible at all, it all depends on the Android APIs).

    app.Query(e => e.Id("your_button").Invoke("getCurrentTextColor").Value<int>()).

    This returns an int representing Android color (more about colors in android here ). You will have to look at the Android API reference for a method that will return the background color to you: android.widget.Button . You might even have to stack multiple Invokes on top of each other - it really depends on how easy Android APIs will make it for you to retrieve the color.

Answers

  • Fabio_GottlicherFabio_Gottlicher USMember, University

    I am guessing you are asking how to do this on android (from your tags). You will have to call Invoke ( doc ) on your button and dig into the Android API reference to find a method that returns the color of background of a view.

    I figured out how to do it for the button text, and it should hopefully be pretty similar for the background color.

    app.Query(e => e.Id("your_button_id_or_other_qualifier_here").Invoke("getCurrentTextColor").Value<int>()) will return an int representing the color (more about Android colors here ). I found the method name on the Android API reference website, so you might have to look there for something that retrieves the background color: android.widget.Button

  • Fabio_GottlicherFabio_Gottlicher USMember, University
    Accepted Answer

    You will have to probably call an invoke (i.e. invoke a native Android method) on the Button you're working with.

    I figured out really quick how to do it for the Button's text color, and it should be pretty similar for the button bakground color (or it might not be possible at all, it all depends on the Android APIs).

    app.Query(e => e.Id("your_button").Invoke("getCurrentTextColor").Value<int>()).

    This returns an int representing Android color (more about colors in android here ). You will have to look at the Android API reference for a method that will return the background color to you: android.widget.Button . You might even have to stack multiple Invokes on top of each other - it really depends on how easy Android APIs will make it for you to retrieve the color.

  • Fabio_GottlicherFabio_Gottlicher USMember, University

    @Harald said:
    Hi all,

    does anyone know, if it is possible to check if the color of an ui element has changed. In my case its a background color of a button, that changes if the user "klicks" it.

    Thx for you help!

    You will have to probably call an invoke (i.e. invoke a native Android method) on the Button you're working with.

    I figured out really quick how to do it for the Button's text color, and it should be pretty similar for the button bakground color (or it might not be possible at all, it all depends on the Android APIs).

    app.Query(e => e.Id("your_button").Invoke("getCurrentTextColor").Value<int>()).

    This returns an int representing Android color (more about colors in android here ). You will have to look at the Android API reference for a method that will return the background color to you: android.widget.Button . You might even have to stack multiple Invokes on top of each other - it really depends on how easy Android APIs will make it for you to retrieve the color.

  • brijmitchellbrijmitchell USMember ✭✭
    edited May 2017

    Hi guys,

    I am having a bit of trouble getting this to work. When I run the command I get the following

    >>> app.Query(x => x.Marked("Login").Invoke("getBackground").Invoke("getColor"))
    Query for Marked("Login").Invoke("getBackground").Invoke("getColor") gave 1 results.
    [
        [0] {
            error => [
    
            ],
            receiverClass => [
    
            ],
            receiverString => [
    
            ],
            methodName => [
    
            ]
        }
    ]
    

    Any help will be hugely appreciated

  • sidharthgautamsidharthgautam USMember ✭✭
    edited October 2017

    Hi All,
    I am also having trouble getting this to work. When I run the command I get the following:

    app.Query(x => x.Id("elem_Id").Invoke("getBackground").Invoke("getColor"))

    Query for Id("elem_Id").Invoke("getBackground").Invoke("getColor") gave 1 results.
    [
    [0] {
    error => [

        ],
        receiverClass => [
    
        ],
        receiverString => [
    
        ],
        methodName => [
    
        ]
    }
    

    ]

  • PerdruPerdru Member ✭✭

    I'm seeing the same problem as @sidharthgautam
    app.Query(x => x.Id("elem_Id").Invoke("getBackground").Invoke("getClass"))
    returns "ButtonDrawable" instead of ColorDrawable as implied by @Harald, which explains why Invoking getColor() doesn't work anymore. Idk what would've changed (Android Version? Compat/other library?)

Sign In or Register to comment.