通过 Appium 获取 CSS 属性

Get CSS property via Appium

我只想单击启用“绿色”的按钮。为了找到按钮的颜色,我尝试了以下方法,但 none 有效并给出“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");

异常:

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)

根据 Appium 文档:

Get Element CSS Value command only applies to webview contexts

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

因此它不适用于本机上下文。

检查元素是否启用

还有另一种检查按钮是否启用的方法:

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

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

if(mobileElement.isEnabled()) {
    mobileElement.click();
}

检查元素颜色

如果您还需要验证元素颜色,您应该将页面(或元素)截图,然后获取元素位置,并尝试通过坐标获取屏幕截图上元素区域的像素颜色。

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.