appium-如何在本机android应用中获取元素的背景色

时间:2018-08-20 05:48:50

标签: appium appium-android

正在尝试使用appium使应用程序自动化。如何在Android应用中获取元素的背景色。

我尝试使用

element.getCssValue("background-color")

但我面临以下异常:

  

java.lang.ClassCastException:com.google.common.collect.Maps $ TransformedEntriesMap无法转换为java.lang.String       在org.openqa.selenium.remote.RemoteWebElement.getCssValue(RemoteWebElement.java:167)       在io.appium.java_client.DefaultGenericMobileElement.getCssValue(DefaultGenericMobileElement.java:177)       在io.appium.java_client.MobileElement.getCssValue(MobileElement.java:1)       在io.appium.java_client.android.AndroidElement.getCssValue(AndroidElement.java:1)       在com.mahindracomviva.digibanktest.tests.corecontrollers.DynamicControlsTest.validateThemeColor(DynamicControlsTest.java:130)上       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)       在java.lang.reflect.Method.invoke(未知来源)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)       在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)       在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

2 个答案:

答案 0 :(得分:4)

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

  2. 仅当上下文为Webview时,才可以使用此方法getCssValue()。

答案 1 :(得分:0)

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);