将Application1切换到Application2。成功切换到Application2,但是在Application2上未执行单击操作

时间:2018-10-01 11:27:55

标签: appium appium-android

  

我的appium版本是1.9.0,而Java客户端是1.6.0和android sdk 27​​。

这是我的混合应用程序示例代码。我的app1和app2具有相同的按钮,即signin,并且两个按钮的ID都相同。 但是如果尝试单击app2登录按钮,则会在app1 signin button上单击它。

public class sample {
    AndroidDriver driver;   
    @BeforeTest
    public void beforeTest() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "xyz");
        capabilities.setCapability("platformVersion", "6.0");
        capabilities.setCapability("platformName", Platform.ANDROID);
        capabilities.setCapability("appPackage", "com.app1.pkgname");
        capabilities.setCapability("appActivity","com.app1.activityname");
        capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 6000);    capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS,true);
        try {
            driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        }
        // provide implicit wait
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void Test() {    
        Activity activity = new Activity("com.app2.pkgname", "com.app2.MainActivityName");
        activity.setStopApp(true);
        ((AndroidDriver) driver).startActivity(activity);

        System.out.println("current context it will return native "+((AndroidDriver) driver).getContext()); 

        ((AndroidDriver) driver).context("WEBVIEW_com.app2");

        WebElement xx = driver.findElement(By.id("SA_btn_SignIn"));
        xx.isEnabled();   // returning true
        xx.click(); // but not get click on element of app2
    }
}

1 个答案:

答案 0 :(得分:0)

您做错了。

  

确保您使用的是Java客户端6.1.0而不是1.6.0

首先,您需要对AndroidDriver使用泛型,例如

AndroidDriver <AndroidElement> 

所以您的代码应该看起来像

public class sample {
  AndroidDriver <AndroidElement> driver;
  @BeforeTest
  public void beforeTest() {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "xyz");
    capabilities.setCapability("platformVersion", "6.0");
    capabilities.setCapability("platformName", Platform.ANDROID);
    capabilities.setCapability("appPackage", "com.app1.pkgname");
    capabilities.setCapability("appActivity","com.app1.activityname");
    capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 6000);
    capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS,true);
    try {
      driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    } catch (MalformedURLException e) {
      System.out.println(e.getMessage());
    }
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void Test() {
    Activity activity = new Activity("com.app2.pkgname", "com.app2.MainActivityName");
    activity.setStopApp(true);
    driver.startActivity(activity);
    System.out.println("current context it will return native " + driver.getContext());
    driver.context("WEBVIEW_com.app2");
    AndroidElement xx = driver.findElement(By.id("SA_btn_SignIn"));
    if (xx.isEnabled()) {
      AndroidTouchAction action = new AndroidTouchAction (driver);
      action.tap(TapOptions.tapOptions().withElement(ElementOption.element(xx))).perform();
    }
  }
}