2017-01-10 135 views
1

我想自動測試在Android中運行的Cordova之上構建的基本混合移動應用程序。我爲此使用了Apppium。我跟着tutorial video開始。我下載並將Selenum,selendroid和java客戶端.jar文件添加到應用程序的構建路徑。Appium測試Android混合移動應用程序

下面是我的代碼,

import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Iterator; 
import java.util.Set; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 

import io.appium.java_client.AppiumDriver; 
import io.appium.java_client.MobileElement; 
import io.appium.java_client.android.AndroidDriver; 
import io.appium.java_client.remote.MobileCapabilityType; 

public class AppiumTest { 

    public static void main(String[] args) throws MalformedURLException, InterruptedException { 

     URL serverAddress = new URL("http://0.0.0.0:4723/wd/hub"); 
     WebDriver driver = new AndroidDriver<MobileElement>(serverAddress, getDesiredCapabilities()); 

     try{    
      Set<String> contextNames = ((AppiumDriver<MobileElement>) driver).getContextHandles(); 
      for (final String contextName : contextNames) { 
        System.out.println(contextName); 
        if (contextName.contains("WEBVIEW_0")) { 
         Thread.sleep(3000); 
         driver.switchTo().window("WEBVIEW_0"); 
        } 
       } 

      //Change color to Red 
      driver.findElement(By.cssSelector("p.recieved")).click(); 
      Thread.sleep(2000); 

      //Change color to Red 
      driver.findElement(By.cssSelector("recieved")).click(); 
      Thread.sleep(2000); 

      driver.get("http//appium.io/"); 
      Thread.sleep(2000); 
     } 
     finally { 
      driver.quit(); 
     } 
     System.out.println("Driver "+driver); 
    } 

    public static DesiredCapabilities getDesiredCapabilities() { 

     DesiredCapabilities capability = new DesiredCapabilities(); 

     capability.setCapability(MobileCapabilityType.AUTOMATION_NAME, "selendroid"); 
     capability.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); 
     capability.setCapability("platformVersion", "4.4.4"); 
     capability.setCapability("deviceName", "Android-Dev"); 
     capability.setCapability("app", 
       "C:/Joseph/Appium/HybridProject/AppiumTest/platforms/android/build/outputs/apk/android-debug.apk"); 
     capability.setCapability("appPackage", "com.joseph.appiumtest"); 
     capability.setCapability("appActivity", ".MainActivity"); 

     return capability; 
    } 

} 

我可以能夠得到能力上下文。在切換窗口(driver.switchTo().window("WEBVIEW_0")),我越來越喜歡Exception in thread "main" org.openqa.selenium.WebDriverException: CATCH_ALL: java.lang.VerifyError: io/selendroid/server/model/SelendroidWebDriver

版本錯誤:

  • 安卓4.4.4
  • Appium:1.6.3
  • 硒:3.0.1
  • Selendroid:0.17.0

回答

0

經過大量嘗試後,在混合移動應用程序中使用自動化測試g Appium工作。

基本上在Capabilities中,不需要設置包名稱和活動名稱。而是獲取應用程序包名稱(.apk)的文件路徑。

 File app= new File("project/platforms/android/build/outputs/apk/android-debug.apk"); 
     DesiredCapabilities capabilities= new DesiredCapabilities(); 
     capabilities.setCapability("deviceName", "Android-Dev"); 
     capabilities.setCapability("platformVersion", "4.4.4"); 
     capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "MobilePlatform.ANDROID"); 
     capabilities.setCapability("app", app.getAbsolutePath()); 

然後,不要顯式地將窗口切換到WEBVIEW。獲取上下文併爲應用程序設置Webview特定的動態上下文。

 try { 
      Set<String> contextNames = ((AppiumDriver) driver).getContextHandles(); 
      for (String contextName : contextNames) { 
       System.out.println(contextName); 
       if (contextName.contains("WEBVIEW")) { 
        ((AppiumDriver<MobileElement>) driver).context(contextName); 
       } 
      } 
     } 
     catch(Exception e){ 
      e.printStackTrace();    
     } 

最後用驅動程序設置,執行操作。

driver.findElement(By.xpath("//*[@id='login']")).click(); 
相關問題