我有一个测试用例,我需要在iPhone中使用 1.在app A中执行任务。 2.切换到Safari浏览器并执行任务 3.再次切换回App A并继续执行其他任务
目前我可以执行1& 2&然后切换回App A但是从Safari浏览器切换后我无法在App A中执行任何任务。我做了如下。
首先启动应用程序A我使用了以下代码
public void openSafari() throws MalformedURLException {
DesiredCapabilities capabilities2 = new DesiredCapabilities();
capabilities2.setCapability("deviceName", "iPhone 7 Plus");
capabilities2.setCapability("platformName", "iOS");
capabilities2.setCapability("platformVersion", "11.2");
capabilities2.setCapability("browserName", "Safari");
capabilities2.setCapability("autoAcceptAlerts", true);
capabilities2.setCapability("useNewWDA", true);
iosDriver2 = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities2);
iosDriver2.get("https://www.gmail.com");}
要从我的应用切换到Safari,我使用以下代码启动了一个新会话以切换到safari
public void switchToNutrifix() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities3 = new DesiredCapabilities();
capabilities3.setCapability("deviceName", "iPhone 7 Plus");
capabilities3.setCapability("platformName", "iOS");
capabilities3.setCapability("platformVersion", "11.2"); //Replace this with your iOS version
capabilities3.setCapability("autoWebView", true);
capabilities3.setCapability("app", getProperties().getProperty("appURL"));
capabilities3.setCapability("autoAcceptAlerts", true);
capabilities3.setCapability("useNewWDA", true);
capabilities3.setCapability("startIWDP",true);
iosDriver3 = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities3);}
我现在的问题是再次从Safari浏览器切换回App A,以便在我再次创建新会话时执行某些任务,如下所示
May 09, 2018 6:43:42 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.xpath: //div[@class='login-content purpose']/h2)
org.openqa.selenium.remote.SessionNotFoundException: A session is either terminated or not started (WARNING: The server did not provide any stacktrace information)
现在的问题是应用程序A再次从Safari打开,但它无法识别应用程序中的元素。我甚至将上下文正确地更改为webview,但仍然无法在从Safari浏览器切换后在App A中执行任何任务。有人可以告诉我这里我做错了什么或另一种方法来做这个转换。
我通常得到的错误是
debug] [iOS] No key id found. Choosing first id from page array
[debug] [iOS] Page change not referring to currently selected app, ignoring.
和appium日志显示
class Car {}
class RacingCar:Car {}
let raceCar = RacingCar()
func findType(_ obj: Any, parent: AnyClass) {
print(type(of: obj)) // RacingCar
print(parent) // Car
print(type(of: obj) == parent) // false
// if obj is parent {
// print("true")
// }
}
findType(raceCar, parent: Car.self)
答案 0 :(得分:1)
public class SwitchBtnAppsiOS {
public AppiumDriver<RemoteWebElement>iosDriver;
public DesiredCapabilities createAppiumDriver()
throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "iPhone 7 Plus");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "11.2");
capabilities.setCapability("autoWebView", true);
capabilities.setCapability("app", getProperties().getProperty("appURL"));
capabilities.setCapability("autoAcceptAlerts", true);
capabilities.setCapability("useNewWDA", true);
capabilities.setCapability("startIWDP",true);
return capabilities;
}
public DesiredCapabilities openSafari()
throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "iPhone 7 Plus");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "11.2");
capabilities.setCapability("browserName", "Safari");
capabilities.setCapability("autoAcceptAlerts", true);
capabilities.setCapability("useNewWDA", true);
return capabilities;
}
public void switchBetweenApps() throws MalformedURLException, InterruptedException
{
DesiredCapabilities nativeAppCaps = createAppiumDriver();
iosDriver = new IOSDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), nativeAppCaps);
/**
* Perform the actions on native app
*/
DesiredCapabilities webCaps = createAppiumDriver();
iosDriver = new IOSDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), webCaps);
iosDriver.get("https://www.gmail.com");
/**
* Perform the actions on safari browser
*/
// Again switch to native app
iosDriver = new IOSDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), nativeAppCaps);
}
}
答案 1 :(得分:0)
由于 XCode 9 Appium支持多个会话,我建议使用 2个并发会话:app和safari:
driver1
对象)提示:你可以把它放在后台:
driver.runAppInBackground(Duration.ofMinutes(1));
driver2
对象)driver2
driver1
原生应用答案 2 :(得分:0)
有一个更好的方法来切换黑白iOS应用程序,而不是像上面答案中所建议的那样一次又一次地创建驱动程序会话
IOSDriver driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
try {
HashMap<String, Object> args = new HashMap<>();
args.put("bundleId", SWITCH_APP_BUNDLE_ID);
driver.executeScript("mobile: launchApp", args);
/*
TODO code for switch app
*/
// Now reactivate AUT App
args.put("bundleId", AUT_APP_BUNDLE_ID);
driver.executeScript("mobile: activateApp", args);
/*
TODO code for AUT App
*/
}
finally {
// quit driver
}