使用Appium访问iOS控制中心

13

我正在尝试使用Appium打开控制中心,代码如下:

    int halfWidth = driver.manage().window().getSize().width / 2;
    int screenHeight = driver.manage().window().getSize().height;
    driver.swipe(halfWidth, screenHeight-5, halfWidth, screenHeight-300, 500);  // driver is instance of IOSDriver

这个应用程序不是打开控制中心,而是从底部向上绘制屏幕(使用坐标输入)。 有人知道如何使用appium和滑动(或任何其他方法)打开控制中心吗?

谢谢,查理


看了一下swipe()方法的细节,我怀疑可能是因为我们先按下再移动,而不是直接从起点开始移动...但我会等待其他人确认这一点。 - Charlie S
7个回答

5
我们可以做到这一点。我在Appium 1.4.13中尝试过,并且成功更改了设置。
我使用以下代码在我的iPadAir2上更改了设置。
int height = driver.findElementByClassName("UIAWindow").getSize().getHeight();
int width = driver.findElementByClassName("UIAWindow").getSize().getWidth();
driver.swipe(width-100, height, width-100, height-200, 500);
driver.findElementByAccessibilityId("Wi-Fi").click();

iOS版本和/或设备之间的wifi可访问性ID是否更改?您在控制中心中找到wifi的可访问性ID值在哪里?它有文档记录吗?我已经尝试使用代码片段打开控制中心,但无法通过可访问性ID找到wifi按钮/图标。我在运行iOS 8.4.1、Appium 1.4.16和XCode 7.2的iPod Touch上进行了测试。 - David
我认为 accessibilityId 在不同的操作系统版本之间不会改变。我使用 Appium Inspector 来识别 Wifi 按钮的 accessibilityID。 - Shiva Krishna Chippa
嗨,我遇到了以下错误。有什么建议吗? 将[POST /element]代理到[POST http://localhost:8100/session/340CCDB4-E673-42F7-8767-FE27934B2917/element],并使用以下正文:{"using":"xpath","value":"//XCUIElementTypeOther[@label='Wi-Fi']"},得到状态码为200的响应:{"value":"*** setObjectForKey: object cannot be nil (key: top)\n\n(\n\t0
HTTP] <-- POST /wd/hub/session/26cbc5b8-0260-409b-b9b0-3d6855c97fbb/element 500 442 ms - 164
- Vinay

1

Appium 1.6.5版本,你可以使用下面的Python代码来调用滑动方法:

window_size = self.driver.get_window_size()  # this returns dictionary
el = self.driver.find_element(*self.configuration.CommonScreen.WEB_VIEW)
action = TouchAction(self.driver)
start_x = window_size["width"] * 0.5
start_y = window_size["height"]
end_x = window_size["width"] * 0.5
end_y = window_size["height"] * 0.5
action.press(el, start_x, start_y).wait(100).move_to(el, end_x, end_y).release().perform() 

0

我能够使用Appium 1.6.4-beta for iOS切换Wifi关闭或打开飞行模式

从屏幕底部向上滑动 点击继续链接 点击Wifi或飞行按钮 从屏幕中间向下滑动

但是在模拟器中似乎没有任何作用。我必须实际关闭计算机的互联网连接才能禁用模拟器上的互联网。

@iOSFindBy(xpath = "//XCUIElementTypeSwitch[@name='Wi-Fi']")
private MobileElement WIFI_MODE_BUTTON;

public void disableWifi()  {
    openToolBarMenu();
    //if wifi is on/true then turn it off
   if (WIFI_MODE_BUTTON.getAttribute("value") == "true" ) {
       Autoscope.tap(WIFI_MODE_BUTTON);
   }
    closeToolBarMenu();
}


@iOSFindBy(xpath = "//XCUIElementTypeButton[@name='Continue']")
private MobileElement CONTINUE_BUTTON; //continue button on control center

public void openToolBarMenu() {
    Autoscope.scrollFromBottomOfScreen();
    if (Autoscope.isElementDisplayed(CONTINUE_BUTTON)) {
        Autoscope.tap(CONTINUE_BUTTON);
    }
}


static public void scrollFromBottomOfScreen() {
    TouchAction touchAction = new TouchAction(autoscopeDriver);

    int xStartPoint = Math.round(pixelWidth() / 2);
    int yStartPoint = pixelHeight();
    int yEndPoint = 0 - yStartPoint;
    touchAction.press(xStartPoint, yStartPoint).moveTo(0, yEndPoint).release().perform();
}

0

C#: iOS 13.x

//Opening control center

var size = Driver.Manage().Window.Size;

var height = size.Height;

var width = size.Width;

var touchAction = new TouchAction(Driver);

touchAction.Press(width - 100, height).Wait(1000).MoveTo(width - 100, height - 200).Release().Perform();

//Clicking the WiFi button

Driver.FindElementByAccessibilityId("wifi-button").Click();

//Checking if WiFi enabled or not

var myElement = Driver.FindElementByAccessibilityId("wifi-button");

var result = myElement.GetAttribute("label");

if(!result.Contains("Wi-Fi, Not Connected") && !result.Equals("Wi-Fi"))
{
    // WiFi connected
}
else
{
    // WiFi Not connected
}

0

这段代码将帮助您在应用程序中启动控制中心,您可以执行控制中心中可用的所有操作。

new TouchAction(DriverConfig.getInstance().getDriver()).press(point(250, 735)).waitAction(waitOptions(Duration.ofSeconds(3))).moveTo(point(250, -460)).release()
                        .perform();

@Alessio 很高兴代码起作用了,我会编辑我的回答并提供更多细节,谢谢。 - Subhist Subedar

0

这个想法是模拟在相应的iOS设备上打开控制中心时使用的滑动操作。我的设备是iPhone 11,因此从右上方(在刘海屏右侧)向下滑动。我的代码是从位置(x,y) (80%宽度, 0) 滑动到 (80%宽度, 50%高度)

    Dimension size = getScreenSize();

    int x = (size.getWidth() / 5) * 4;
    int startY = 0;
    int endY = size.getHeight() / 2;

    new TouchAction(driver).press(PointOption.point(x, startY))
            .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
            .moveTo(PointOption.point(x, endY))
            .release().perform();

-1

好的,经过相当多的调查,我认为这是不可能的。如果你真的需要这个功能,那么我认为像茄子这样的工具可能是合适的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接