appium - 如何在原生 android 应用程序中获取元素的背景颜色

appium - how to get background color of an element in native android app

我正在尝试使用 appium 使应用程序自动化。如何在 Android app.

中获取元素的背景颜色

我尝试使用

element.getCssValue("background-color")

但我面临以下异常:

java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to java.lang.String at org.openqa.selenium.remote.RemoteWebElement.getCssValue(RemoteWebElement.java:167) 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.android.AndroidElement.getCssValue(AndroidElement.java:1) at com.mahindracomviva.digibanktest.tests.corecontrollers.DynamicControlsTest.validateThemeColor(DynamicControlsTest.java:130) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

  1. 原生元素没有 css 属性。您不能在本机应用程序元素上使用此方法。

  2. 只有当您的上下文是 webview 时,您才能使用此方法 getCssValue()。

Appium 不直接支持获取背景颜色。

您可以尝试解决方法,例如 the one described here:

MobileElement elem = (MobileElement) driver.findElement(By.id("loginButton"));

org.openqa.selenium.Point point = elem.getCenter();
int centerX = point.getX();
int centerY = point.getY();

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

BufferedImage image = ImageIO.read(scrFile);
// Getting pixel color by position x and y 
int clr = image.getRGB(centerX,centerY); 
int red   = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue  =  clr & 0x000000ff;
System.out.println("Red Color value = "+ red);
System.out.println("Green Color value = "+ green);
System.out.println("Blue Color value = "+ blue);