judy_xia 发表于 2016-3-22 17:57:55

求助:初学appium, 运行脚本模拟器上无法正常打开浏览器

1.模拟器表面现象:


2. eclipse 报错:
错误1: FAILED CONFIGURATION: @AfterClass afterTestStopDriver
org.openqa.selenium.remote.SessionNotFoundException:
Command duration or timeout: 5 milliseconds
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: '本机名', ip: '本机IP', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_05'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=39.0.0.0, platform=ANDROID, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 9c31a71dedea66f57122e5ea098a9d71
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
错误2:
FAILED: runChromeWebBrowser
org.openqa.selenium.remote.SessionNotFoundException: no such session
(Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 581 milliseconds
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: '本机名', ip: '本机IP', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_05'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=39.0.0.0, platform=ANDROID, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 9c31a71dedea66f57122e5ea098a9d71
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)




judy_xia 发表于 2016-3-22 18:00:04

3.appium 日志:
> info: Found emulator nexusS_intelAtom in port 5554
> info: Setting device id to emulator-5554
> info: Did not launch AVD because it was already running.
> info: Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 wait-for-device
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 shell "echo 'ready'"
> info: Starting logcat capture
> info: Pushing unlock helper app to device...
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 install "C:\Program Files (x86)\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 shell "dumpsys window"
> info: Screen already unlocked, continuing.
> info: Forwarding system:4724 to device:4724
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 forward tcp:4724 tcp:4724
> info: Pushing appium bootstrap to device...
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 push "C:\\Program Files (x86)\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
> info: Starting App
> info: Attempting to kill all 'uiautomator' processes
> info: Getting all processes with 'uiautomator'
> info: executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 shell "ps 'uiautomator'"
> info: No matching processes found
> info: Running bootstrap
> info: spawning: D:\android-sdk-windows\platform-tools\adb.exe -s emulator-5554 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.android.browser -e disableAndroidWatchers false
> info: INSTRUMENTATION_STATUS: numtests=1
> info: INSTRUMENTATION_STATUS: stream=
> info: io.appium.android.bootstrap.Bootstrap:
> info: INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: INSTRUMENTATION_STATUS: test=testRunServer
> info: INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: INSTRUMENTATION_STATUS: current=1
> info: INSTRUMENTATION_STATUS_CODE: 1
> info: Socket opened on port 4724
> info: Appium Socket Server Ready
> info: Pushing command to appium work queue: ["getDataDir",{}]
> info: Loading json...
> info: Registered crash watchers.
> info: dataDir set to: /data
> info: Chromedriver: Changed state to 'starting'
> info: Chromedriver: Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
> info: Chromedriver: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"9515 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %a))

judy_xia 发表于 2016-3-22 18:00:32

4.我的脚本就是一个目的,打开百度首页。
脚本如下:
import io.appium.java_client.android.AndroidDriver;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class OpenChrom {
    AndroidDriver<WebElement> driver;

    /**
   * @author Young
   * @throws IOException
   */
    public void startRecord() throws IOException {
      Runtime rt = Runtime.getRuntime();
      // this code for record the screen of your device
      rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");

    }

    @BeforeClass(alwaysRun = true)
    public void setUp() throws Exception {
      // set up appium

      DesiredCapabilities capabilities = new DesiredCapabilities();
      capabilities.setCapability(CapabilityType.BROWSER_NAME, "browser");
      capabilities.setCapability("platformName", "Android");
      capabilities.setCapability("deviceName", "Android Emulator");
      capabilities.setCapability("platformVersion", "5.1.1");
      driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
      startRecord();

    }

    @Test
    public void runChromeWebBrowser() {
      driver.get("http://www.baidu.com");
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
      System.out.println(driver.getTitle());
      snapshot((TakesScreenshot) driver, "before_search.png");
      driver.findElementByXPath("//textarea[@id='index-kw']").sendKeys(
                "appium");
      driver.findElement(By.xpath("//button[@id='index-bn']")).click();
      driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
      System.out.println(driver.getTitle());
      snapshot((TakesScreenshot) driver, "after_search.png");
      Assert.assertTrue(driver.getTitle().contains("appium"));

    }

   
   
    /**
   * This Method create for take screenshot
   *
   * @author Young
   * @param drivername
   * @param filename
   */
    public static void snapshot(TakesScreenshot drivername, String filename) {
      // this method will take screen shot ,require two parameters ,one is
      // driver name, another is file name

      String currentPath = System.getProperty("user.dir"); // get current work
                                                                // folder
      File scrFile = drivername.getScreenshotAs(OutputType.FILE);
      // Now you can do whatever you need to do with it, for example copy
      // somewhere
      try {
            System.out.println("save snapshot path is:" + currentPath + "/"
                  + filename);
            FileUtils
                  .copyFile(scrFile, new File(currentPath + "\\" + filename));
      } catch (IOException e) {
            System.out.println("Can't save screenshot");
            e.printStackTrace();
      } finally {
            System.out.println("screen shot finished, it's in " + currentPath
                  + " folder");
      }
    }

    @AfterClass
    public void afterTestStopDriver() {
      driver.quit();
    }
}

judy_xia 发表于 2016-3-22 18:01:37

我手动打开浏览器很快,不会有这个问题。 我用的是intel x86

lin0731032 发表于 2016-3-22 18:49:49

你没加延时等待
页: [1]
查看完整版本: 求助:初学appium, 运行脚本模拟器上无法正常打开浏览器