在appium iOS中切换两个应用程序

时间:2018-05-09 13:17:32

标签: automation automated-tests appium ios-ui-automation appium-ios

我有一个测试用例,我需要在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)

3 个答案:

答案 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. 无论您在哪里使用驱动程序实例,都要传递最新的驱动程序实例。
  2. 在这种情况下,请使用RemoteWebElement而不是MobileElement。

答案 1 :(得分:0)

由于 XCode 9 Appium支持多个会话,我建议使用 2个并发会话:app和safari:

  1. 为本机应用创建驱动程序(例如driver1对象)
  2. 在app中想要你需要,让驱动程序实例保持活着
  3. 提示:你可以把它放在后台: driver.runAppInBackground(Duration.ofMinutes(1));

    1. 为safari创建驱动程序(例如driver2对象)
    2. 如果您不需要,请在safair中关注并关闭driver2
    3. 继续使用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     
}