Search code examples
seleniumappiumappium-ios

Get CSS property via Appium


I want to click on a button only if it is enabled "Green color". To find the color of a button I tried the below ways, but none worked and giving "UnsupportedCommandException"

MobileElement mobileElement = appiumDriver.findElement(By.xpath("//XCUIElementTypeButton[@name='Get Quote']"));

String color = mobileElement.getCssProperty("style");
String color = mobileElement.getCssProperty("background-color");
String color = mobileElement.getCssProperty("color");

Exception:

org.openqa.selenium.UnsupportedCommandException: Unhandled endpoint: {
    wildcards =     (
        "session/element/EF010000-0000-0000-8007-000000000000/css/background-color"
    );
} (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

Driver info: io.appium.java_client.ios.IOSDriver



at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.ios.IOSDriver.execute(IOSDriver.java:1)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at io.appium.java_client.DefaultGenericMobileElement.execute(DefaultGenericMobileElement.java:45)
at io.appium.java_client.MobileElement.execute(MobileElement.java:1)
at io.appium.java_client.ios.IOSElement.execute(IOSElement.java:1)
at org.openqa.selenium.remote.RemoteWebElement.getCssValue(RemoteWebElement.java:171)
at io.appium.java_client.DefaultGenericMobileElement.getCssValue(DefaultGenericMobileElement.java:177)
at io.appium.java_client.MobileElement.getCssValue(MobileElement.java:1)
at io.appium.java_client.ios.IOSElement.getCssValue(IOSElement.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at io.appium.java_client.pagefactory.ElementInterceptor.getObject(ElementInterceptor.java:40)
at io.appium.java_client.pagefactory.interceptors.InterceptorOfASingleElement.intercept(InterceptorOfASingleElement.java:60)
at io.appium.java_client.ios.IOSElement$$EnhancerByCGLIB$$d311658.getCssValue(<generated>)
at com.haulhub.pageactions.mobileapp.HomePageAction.clickPendingFilterButton(DemoPageAction.java:73)
at com.haulhub.tests.DotSlipDisplayFiltersTest.verifyPendingTicketMatchesPendingInspectorReview(DemoFiltersTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

Solution

  • According to Appium documentation:

    Get Element CSS Value command only applies to webview contexts

    https://appium.io/docs/en/commands/element/attributes/css-property/

    So it will not work for native context.

    Check if element is enabled

    There is another way to check if the button is enabled or not:

    https://appium.io/docs/en/commands/element/attributes/enabled

    MobileElement mobileElement = appiumDriver.findElement(By.xpath("//XCUIElementTypeButton[@name='Get Quote']"));
    
    if(mobileElement.isEnabled()) {
        mobileElement.click();
    }
    
    

    Check element color

    If you also need to verify the element color, you should take the page (or element) screenshot, then get the element location and try to get the pixel color witing the element area on the screenshot by coordinates.

    MobileElement mobileElement = appiumDriver.findElement(By.xpath("//XCUIElementTypeButton[@name='Get Quote']"));
    
    File screenshot = ((TakesScreenshot)appiumDriver).getScreenshotAs(OutputType.FILE);
    BufferedImage image = ImageIO.read(screenshot);
    Point elementLocation = mobileElement.getLocation();
    // Lets take color at point x=10, y=10 inside the element (0, 0 is the top left)
    int checkColorX = elementLocation.getX() + 10;
    int checkColorY = elementLocation.getY() + 10;
    
    // Optionaly you may try to take not the page screenshot, but just the element screenshot
    // File screenshot = mobileElement.getScreenshotAs(OutputType.FILE);
    // BufferedImage image = ImageIO.read(screenshot);
    // int checkColorX = 10;
    // int checkColorY = 10;
    
    
    // Getting pixel color by position x and y 
    int color =  image.getRGB(checkColorX, checkColorY); 
    int red   = (color & 0x00ff0000) >> 16;
    int green = (color & 0x0000ff00) >> 8;
    int blue  =  color & 0x000000ff;
    
    System.out.println("Red Color value = "+ red);
    System.out.println("Green Color value = "+ green);
    System.out.println("Blue Color value = "+ blue);
    
    // Then you need to compare each color with the values, you expect.