有没有办法使用Appium在iOS自动化中完成以下过程?
我尝试过这些:
RemoteWebDriver wd = null;
wd.close();
但它只是退出应用程序(就像
wd.quit()
),我正在自动化它,然后当我尝试重新启动时 - >它从头开始。我正在 iPhone 模拟器上运行它。
iOS 版
Xcode 不支持发送密钥:https://github.com/appium/appium/issues/4479 因此,可能的解决方法是:https://stackoverflow.com/a/24408831/2302437
适用于安卓
将应用程序置于后台,即按主页按钮 -
((AppiumDriver) driver).sendKeyEvent(AndroidKeyCode.HOME);
要重新启动应用程序,我执行了以下操作- 1.首先使用Monitor.bat识别菜单按钮,即我得到它为
"android.widget.TextView" at index 5.
2.点击它,即
driver.findElementsByClassName("android.widget.TextView").get(5).click();
3.在菜单中找到您的应用程序并单击它。即
driver.findElementsByName(AppName).get(0).click();
退出您正在测试的应用程序确实不是那么容易。
我正在使用 Appium(Selenium for Apps)来测试 iOS 应用程序。 我想做的是按主页按钮,不幸的是 Appium 没有这样的功能(以及很多其他测试工具)。
所以我要做的是模拟按键CMD+SHIFT+H,这相当于模拟器的主页按钮。 对于大多数测试工具来说这也是不可能的,因为它们通过 UIAutomation 在模拟器的“内部”进行交互。
最后我找到了以下解决方案(Java):
Runtime runtime = Runtime.getRuntime();
String[] args = { "osascript", "-e", "tell application \"System Events\" \n tell application \"Simulator\" to activate \n tell application \"System Events\" to keystroke \"h\" using {command down, shift down} \n end tell" };
runtime.exec(args);
这真的很简单:执行 Applescript,然后将击键发送到模拟器。
为了提供一些可读性,这里又是简单的 Applescript:
tell application "System Events"
tell application "Simulator" to activate
tell application "System Events" to keystroke "h" using {command down, shift down}
end tell
注意: 确保在代码中转义“(带有\”)。还要确保插入 在每一行之后,因为 Applescript 基于行。
想出这个解决方案花了我很多时间。 我还没有找到任何其他可行的解决方案来退出应用程序,而不终止整个测试并能够在 iOS 内部进行测试。
编辑:然后您就可以重新启动应用程序,这就是窍门!
是的,没有什么不可能 1.按主页按钮 + 2.将应用程序置于后台:
AppiumDriver dr = MobileDriverFactory.getDriver()
try {
dr.runAppInBackground(2)
} catch (WebDriverException e) {
if (e.getMessage().contains("An error occurred while executing user supplied JavaScript")) {
} else {
throw new RuntimeException(e);
}
}
此功能会欺骗 iOS 并将您的应用程序带到后台
3.打开其他应用程序 + 4.然后重新启动我们的应用程序并恢复方法检查。
ProcessBuilder pb = new ProcessBuilder("idevicedebug", "run", "YourAppNameHere")
Thread.sleep(5000)
Process p=pb.start()
Thread.sleep(5000)
p.destroy()
此函数将调用您的iOs设备中设置的任何应用程序,如果您想知道您的应用程序名称,请打开终端,输入:
ideviceinstaller -l
它将列出您 iOS 设备中的所有应用程序,例如: com.google.ios.youtube、“11.11.8”、“YouTube” com.google.ios.gmail、“11.11.8”、“Gmail” 将样式为 com.xxx.xxx.xxx 的行复制到上面的代码中,您也可以使用终端播放此代码,输入
"idevicedebug run com.google.ios.youtube"
(请先插入您的ios设备),按回车键,youtube将在您的设备中自动打开 谢谢 我来自越南KMS科技
以下是使用 ruby 库的方法:
background_app 2
其中 2 是您希望应用程序在后台运行的秒数。
您是否尝试过使用
driver.execute_script "au.backgroundApp(5)"
作为应用程序的后台?
这种方法的问题是,在这 5 秒内你无法与设备交互,backgroundApp 正在阻塞。
我找到了一种将 Android Appium 与 Ruby 结合使用的更简单的解决方案:
def switch_back_to_app
# For Android
#
# To Put App in Background Press recent apps to close
$driver.press_keycode(187)
sleep(1)
# To bring the same app back i.e Press recent apps to open
$driver.press_keycode(187)
rescue => e
p e.exception
end
module_function :switch_back_to_app
刷新代码,以便能够再次找到所有元素。
对于安卓:
driver.pressKeyCode(AndroidKeyCode.HOME);
对于IOS:
driver.executeScript("mobile: pressButton", ImmutableMap.of("name", "home"));
“Hacci56”的解决方案对我有用,谢谢!