2015-09-14 86 views
6

使用appium在android模擬器上執行自動化測試。模擬器中的瀏覽器在通過代碼實現自動化時不會打開。我已經在下面複製了我的代碼,請親切看看並幫助我。在此先感謝使用Appium腳本自動化android模擬器和瀏覽器

package report; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

import javax.swing.JOptionPane; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.firefox.FirefoxProfile; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.remote.RemoteWebDriver; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 


public class emulator { 
WebDriver driver; 

public void setUp() throws MalformedURLException 
{ 
DesiredCapabilities capabilities= new DesiredCapabilities(); 
capabilities.setCapability(CapabilityType.BROWSER_NAME,"browser"); 
capabilities.setCapability(CapabilityType.VERSION,"4.4"); 
capabilities.setCapability(CapabilityType.PLATFORM,"windows"); 
capabilities.setCapability("platformName","Android"); 
capabilities.setCapability("devices","Android"); 
capabilities.setCapability("avd","nexus"); 
capabilities.setCapability("deviceName",""); 
capabilities.setCapability("appPackage", "com.android.browser"); 
capabilities.setCapability("appActivity", "com.android.browser.BrowserActivity"); 
driver=new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
} 



public void cal(){ 
driver.get("http://www.google.com"); 

} 
public static void main(String[] args) throws MalformedURLException 
{ 
emulator a=new emulator(); 
a.setUp(); 
a.cal();   
} 
} 
+0

您遇到什麼錯誤或異常? –

+0

我得到這個錯誤「一個操作沒有完成之前,其超時過期(原始錯誤:超時:30秒後超時Adb命令」 – bcrajkumar

+0

是的,這是因爲默認的Android模擬器需要太多的時間來打開和加載,請嘗試真正的設備或Genymotion模擬器.. –

回答

5

鎖定屏幕將禁用我們的程序的其他腳本。所以我嘗試手動打開模擬器並禁用鎖定屏幕(設置 - >安全 - >無)。然後關閉模擬器。現在自動打開模擬器並運行腳本。

public void setUp(int p) throws MalformedURLException { 
    DesiredCapabilities capabilities = new DesiredCapabilities(); 
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "browser"); 
    capabilities.setCapability(CapabilityType.VERSION, ""); 
    capabilities.setCapability(CapabilityType.PLATFORM, "windows"); 
    capabilities.setCapability("platformName", "Android"); 
    capabilities.setCapability("udid", "emulator-" + p); 
    capabilities.setCapability("devices", "Android"); 
    capabilities.setCapability("avd", "Nexus7"); 
    capabilities.setCapability("deviceName", ""); 
    capabilities.setCapability("appPackage", "com.android.browser"); 
    capabilities.setCapability("appActivity", "com.android.browser.BrowserActivity"); 
    driver = new RemoteWebDriver(new URL("http://127.0.0.1:" + this.port + "/wd/hub"), 
      capabilities); 
}