有没有办法使用 Appium 在 iOS 自动化中执行以下过程?
- 按主页按钮。
- 将应用程序置于后台。
- 打开其他一些应用程序(比如 Gmail)-> 在那里进行操作。
- 然后重新启动我们的应用程序并恢复方法检查。
我试过这些:
RemoteWebDriver wd = null;
wd.close();
但它只是退出了我正在自动化的应用程序(就像wd.quit()
),然后当我尝试重新启动时 - >它从头开始。我在 iPhone 模拟器上运行它。
有没有办法使用 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.在菜单中找到您的应用程序并单击它。IE
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
注意:确保在您的代码中转义“(带 \”)。还要确保在每行之后插入 \n,因为 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 设备),按 Enter,youtube 将在您的设备中自动打开谢谢我来自 KMS Technology Vietnam
以下是使用 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"));