Java Appium-如何在iOS设备上设置地理位置?

Java Appium-如何在iOS设备上设置地理位置?,java,ios,geolocation,appium,Java,Ios,Geolocation,Appium,Meta:- iOS仿真器设备v10.3 Appium java客户端v5.0.0 BETA8 Selenium v3.4.0 实际上,我正在尝试使用Appiumxguitest自动化在iOS设备中设置GeoLocation。我尝试了以下代码,在Android设备上运行良好,而在iOS上抛出异常: import org.openqa.selenium.html5.Location; AppiumServiceBuilder builder = new AppiumServiceBuilde

Meta:-

  • iOS仿真器设备v10.3
  • Appium java客户端v5.0.0 BETA8
  • Selenium v3.4.0
实际上,我正在尝试使用
Appium
xguitest
自动化在
iOS
设备中设置
GeoLocation
。我尝试了以下代码,在
Android
设备上运行良好,而在
iOS
上抛出异常:

import org.openqa.selenium.html5.Location;

AppiumServiceBuilder builder = new AppiumServiceBuilder().usingAnyFreePort().withAppiumJS("path/to/appium/main.js");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName", "XCUITest");
IOSDriver driver= new IOSDriver(builder, capabilities);

//Here this code working fine with AndroidDriver
Location location = new Location(latitude, longitude, altitude);    
driver.setLocation(location);
例外情况:

org.openqa.selenium.WebDriverException:方法尚未实现(警告:服务器未提供任何stacktrace信息)

当我尝试将JavascriptExecutor用作:

Map<String, String> args = new HashMap<String, String>();
args.put("address", "Address");
((JavascriptExecutor)webDriver).executeScript("mobile:setLocation", args);
Map args=newhashmap();
args.put(“地址”、“地址”);
((JavascriptExecutor)webDriver.executeScript(“mobile:setLocation”,args);
例外情况:

org.openqa.selenium.unsupportedCommand异常:未知移动命令“setLocation”。仅支持滚动、滑动、挤压、双击、双指轻触、触摸并按住、轻触、拖动至存储、选择选择器向左值和警报命令。(警告:服务器未提供任何stacktrace信息)

当我试着成为:

import org.openqa.selenium.remote.DriverCommand;

Map<String, String> args = new HashMap<String, String>();
args.put("location", "Address");
driver.execute(DriverCommand.SET_LOCATION, args);
import org.openqa.selenium.remote.DriverCommand;
Map args=new HashMap();
参数put(“位置”、“地址”);
执行(DriverCommand.SET_位置,args);
例外情况:

org.openqa.selenium.WebDriverException:方法尚未实现(警告:服务器未提供任何stacktrace信息)

是否仍然可以使用appium在
iOS
上设置
GeoLocation

应用日志:

[debug][JSONWP Proxy]获得状态为200的响应:“{\n”value\”:{\n“state\”:“success\”,\n“os\”:{\n“name\”:“iOS\”,\n“version\”:“10.3.1\”,\n“iOS\”:{\n“simulatorVersion\”:“10.3.1\”,\n“ip\”:“192.168.1.17\”,\n\,\n“build\”,\n\”:“time\”:“2017年8月29日09:40\“\n}\n}\n\'sessionId\:\'10A97A93-D13A-4888-A536-0D62E0674A2B\,\n\'status\':0\n}”

[debug][xguitest]WebDriverAgent在ip'192.168.1.17'上运行[debug] [XCUITest]WebDriverAgent在16121ms后成功启动[debug] [BaseDriver]在1504013035278记录了事件“wdaSessionAttempted” (18:53:55 GMT+0530(IST))[debug][xguitest]正在发送createSession WDA[debug][JSONWP Proxy]代理[POST/session]到的命令 [附正文的职位: {“desiredCapabilities”:{“bundleId”:“com.example.apple samplecode.UICatalog”,“arguments”:[],“environment”:{},“shouldWaitForQuiese”:true,“shouldUseTestManagerForVisibilityDetection”:false,“maxTypingFrequency”:120,“shouldUseSingSingletonTestManager”:true}} [debug][JSONWP Proxy]获得状态为200的响应: {“值”:{“会话ID”:“43710C7E-2FDE-4A35-A2E0-4D309EE2CE9C”,“功能”:{“设备”:“iphone”,“浏览器名”:“UICatalog”,“sdkVersion”:“10.3.1”,“CbundleIdentifier”:“com.example.apple samplecode.UICatalog”},“会话ID”:“43710C7E-2FDE-4A35-A2E0-4D309EE2CE9C”,“状态”:0} [debug][BaseDriver]事件“wdaSessionStarted”记录在1504013038184 (18:53:58 GMT+0530(IST))[debug][xguitest]找到了WDA派生的数据 文件夹: “/Users/omprakash.mishra/Library/Developer/Xcode/DerivedData/webdrivergent dikkwtrisltbeobjmfpthwwkvs” [XCUITest]将“555”权限设置为 “/Users/omprakash.mishra/Library/Developer/Xcode/DerivedData/webdrivergent dikkwtrisltbeobjmfvpthwwekvs/Logs/Test/Attachments” 文件夹[debug][xguitest]找到了WDA派生数据文件夹: “/Users/omprakash.mishra/Library/Developer/Xcode/DerivedData/WebDriverAgent folfazwwwukpzfkegdblpnfuwlvfn” [XCUITest]将“555”权限设置为 “/Users/omprakash.mishra/Library/Developer/Xcode/DerivedData/WebDriverAgent folfazwwwukpzfkegdblpnfuwlvfn/Logs/Test/Attachments” 文件夹[debug][BaseDriver]事件“wdaPermsAdjusted”记录在 1504013038192(18:53:58 GMT+0530(IST))[debug][BaseDriver]事件 “wdaStarted”记录于1504013038193(格林尼治标准时间18:53:58+0530(IST))[调试] [XCUITest]将初始方向设置为“纵向”[debug][JSONWP 代理]代理[发布/定向]到[发布] 正文:{“方向”:“纵向”}[debug][JSONWP Proxy]已获取 状态为200的响应: {“值”:{},“会话ID”:“43710C7E-2FDE-4A35-A2E0-4D309EE2CE9C”,“状态”:0} [debug][BaseDriver]事件“开始”记录在1504013038453 (18:53:58 GMT+0530(IST))[Appium]已创建新的XUITestDriver会话 成功地将会话6909c363-12a5-4a21-9298-c7f750ba7e09添加到 主会话列表[debug][BaseDriver]事件“newSessionStarted” 记录时间为1504013038456(格林尼治标准时间18:53:58+0530(IST))[debug][MJSONWP] 使用driver.createSession()结果响应客户端: {“WebStorage Enabled”:false,“locationContextEnabled”:false,“browserName”:“platform”:“MAC”,“javascriptEnabled”:true,“databaseEnabled”:false,“takesScreenshot”:true,“networkConnectionEnabled”:false,“app”:“src/test/resources/executor/UICatalog.app”,“maxTypingFrequency”:“120”,“newCommandTimeout”:0,“platformVersion”:“10.3”,“automationName”:“XUITest”平台名称“:”iOS“,”udid“:”0A41ECE4-6D03-4FEA-A82A-858FDBA6620E“,”设备名称“:”iPhone 6“}[HTTP]GET /wd/hub/session/6909c363-12a5-4a21-9298-c7f750ba7e09{}[调试] [MJSONWP]使用参数调用AppiumDriver.getSession(): [“6909c363-12a5-4a21-9298-c7f750ba7e09”][debug][XCUITest]正在执行 命令“getSession”[debug][JSONWP Proxy]将[GET/]代理到[GET] 没有正文[debug][JSONWP Proxy]得到状态为200的响应:“{\n” \“value\”:{\n\“sessionId\”: \“43710C7E-2FDE-4A35-A2E0-4D309EE2CE9C\”,\n\“能力\”:{\n \“设备\”:“iphone\,\n \“浏览器名称\”:“UICatalog\,\n
\“sdkVersion\”:“10.3.1\”,\n“CbundleIdentifier\”: \“com.example.apple samplecode.UICatalog\”\n}\n}\n \“会话ID\”:\“43710C7E-2FDE-4A35-A2E0-4D309EE2CE9C\”,\n \“状态\”:0\n}“[XCUITest
public static void setLocation(Location loc) {
    try {
        String[] cmd = {"osascript", "-e",
                "on menu_click(mList)\n" +
                        "    local appName, topMenu, r\n" +
                        "\n" +
                        "    -- Validate our input\n" +
                        "    if mList's length < 3 then error \"Menu list is not long enough\"\n" +
                        "\n" +
                        "    -- Set these variables for clarity and brevity later on\n" +
                        "    set {appName, topMenu} to (items 1 through 2 of mList)\n" +
                        "    set r to (items 3 through (mList's length) of mList)\n" +
                        "\n" +
                        "    -- This overly-long line calls the menu_recurse function with\n" +
                        "    -- two arguments: r, and a reference to the top-level menu\n" +
                        "    tell application \"System Events\" to my menu_click_recurse(r, ((process appName)'s ¬\n" +
                        "        (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))\n" +
                        "end menu_click\n" +
                        "\n" +
                        "on menu_click_recurse(mList, parentObject)\n" +
                        "    local f, r\n" +
                        "\n" +
                        "    -- `f` = first item, `r` = rest of items\n" +
                        "    set f to item 1 of mList\n" +
                        "    if mList's length > 1 then set r to (items 2 through (mList's length) of mList)\n" +
                        "\n" +
                        "    -- either actually click the menu item, or recurse again\n" +
                        "    tell application \"System Events\"\n" +
                        "        if mList's length is 1 then\n" +
                        "            click parentObject's menu item f\n" +
                        "        else\n" +
                        "            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))\n" +
                        "        end if\n" +
                        "    end tell\n" +
                        "end menu_click_recurse\n" +
                        "\n" +
                        "application \""+simulatorAppName()+"\" activate    \n" +
                        "delay 0.2\n" +
                        "menu_click({\""+simulatorAppName()+"\",\"Debug\", \"Location\", \"None\"})\n" +
                        "\n" +
                        "delay 0.2\n" +
                        "menu_click({\""+simulatorAppName()+"\",\"Debug\", \"Location\", \"Custom Location…\"})\n" +
                        "\n" +
                        "delay 0.2\n" +
                        "tell application \"System Events\"\n" +
                        "    tell process \""+simulatorAppName()+"\"\n" +
                        "        set value of text field 1 of window \"Custom Location\" to \""+loc.getLatitude()+"\"\n" +
                        "        set value of text field 2 of window \"Custom Location\" to \""+loc.getLongitude()+"\"\n" +
                        "        click UI Element \"OK\" of window \"Custom Location\"\n" +
                        "    end tell\n" +
                        "end tell"
        };
        Process process = Runtime.getRuntime().exec(cmd);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getErrorStream()));
        String lsString;
        while ((lsString = bufferedReader.readLine()) != null) {
            System.out.println(lsString);
        }
        try{Thread.sleep(10000);}catch (Exception e1){}
    } catch (Exception e) {}
}

public static String simulatorAppName() {
    return "Simulator";
}
#!/usr/bin/env bash

osascript -e 'tell application "System Events"
    tell process "Simulator"
        set frontmost to true
        click menu item "Custom Location…" of menu of menu item "Location" of menu "Debug" of menu bar 1
        set popup to window "Custom Location"
        set value of text field 1 of popup to (system attribute "Latitude")
        set value of text field 2 of popup to (system attribute "Longitude")
        click button "OK" of popup
    end tell
end tell'
    def set_geo_location(self, latitude, longitude, altitude):

    logging.info("set geo location")
    try:
        # Currently Apple does not provide any API for XCTest framework to simulate GPS location
        self.driver.set_location(latitude=latitude, longitude=longitude, altitude=altitude)
    except WebDriverException:
        # this will launch Apple Script to automatically set custom GPS location on iOS simulator
        subprocess.call([os.path.join(PROJECT_ROOT, "set_geolocation_for_iOS.sh")],
                        env={"Latitude": latitude, "Longitude": longitude})  # bash cli command for iOS simulator
    sleep(2)
set_geo_location("-77.85", "166.66", "10")