appium (iv) using the appium-server

1 Introduction

  appium-server is the former, now has not been updated, the start of APP than appium-desktop is to use a little script, do not start appium-desktop services;

  appium-server is nodejs write, so you want to download appium-server need to install nodejs;

2, appium-server installation

  (1) install nodejs

  Download the official website address: http: //nodejs.cn/download/, check whether the environment variable path is automatically added nodejs where the Road King, if not manually added; the command line window with node -v and npm -v command to verify whether the installation success.

  See npm settings: npm config ls, the lower figure is the installation path prefix

  Set npm install path: npm config set prefix "installation path (be careful not to bring Chinese and spaces)."

  (2) appium-server installation

 Installation 1: npm mounted Appium-server: npm --registry http://registry.npm.taobao.org install appium -g; Taobao suggested the following image installation NPM

 Installation 2: Use Taobao CNPM command to install: install CNPM command: npm --registry http://registry.npm.taobao.org install cnpm -g; CNPM then mounted appium-server: cnpm install appium -g

 After installation, the installation path configured to npm path, and then enter appium verify successful installation in cmd

3, start appium-server

  (1) appium-server parameters

  Enter appium -h command can get to the command line argument list, you can specify when appium start appium-server command-line parameters, and many desired capabilities parameters.

  Recommended specify --log parameter and --local-timezone parameter configuration appium log storage path.

  (2) using the Java startup and shutdown

    // start and shut-Server appium 
    @Test
     public  void startAndKillAppium () throws IOException, InterruptedException {
         // Start Server-appium 
        String startAppium = "cmd / C Start appium -a 127.0.0.1 -p 4723 -g E: \\ appium --log---local-TimeZone .log timestamp " ;
         // . Runtime.getRuntime () Exec () execute cmd command 
        Runtime.getRuntime () exec (startAppium). ;

        Thread.sleep ( 2000 );
         // close appium-server and command-line window, because appium-server is nodejs written, you can turn off node directly shut down appium-server 
        String KillNode = "taskkill / F / IM node.exe " ;
        String killCmd="taskkill /F /IM cmd.exe";
        Runtime.getRuntime().exec(KillNode);
        Runtime.getRuntime().exec(killCmd);
    }

  Stepped pit when using Java startup appium-server

  (1) If the error can not find appium command, use the admin to open IDEA

  (2) If you start with a Java appium-server, appium log the following error appears:

"Closing session, cause was 'Unexpected shutdown',Encountered internal error running command: NoSuchDriverError: A session is either terminated or not started";

  404, but it does not appear when using Java resulting in new AndroidDriver start appium-desktop this case, because the version appium-desktop and appium-server is not unified, online update is recommended cnpm appium-server can resolve ask

Question, but I'm using this method does not solve, try the following is out of their own way, effective pro-test

  - 1. First find the installation path appium-server, that npm install path set above, locate and open the batch file appium.cmd

 

  --2. Can be used to start to see the actual appium-server is a main.js file, the file may be launched into the main.js appium-desktop file exists in

   C:\Users\HJY\AppData\Local\Programs\Appium\resources\app\node_modules\appium\build\lib\main.js  ,替换后

 

   - 3. After replacing startup path, using Java to start appium-server no longer being given

Then we start using Java appium-server and execute automation, first to use adb connection simulator

public class AppTest {
    //使用appium-server启动APP
    public static void main(String[] args) throws IOException, InterruptedException {
        //启动appium-server
        String startAppium="cmd /c start appium -a 127.0.0.1 -p 4723 -g E:\\appium.log --local-timezone --log-timestamp";
        Runtime.getRuntime().exec(startAppium);
        // Start appium-server will take some time, this wait time to set 
        the Thread.sleep (5000 );

        //设置Capabilities参数
        DesiredCapabilities desiredCapabilities=new DesiredCapabilities();
        //必要参数
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:7555");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("platformVersion","6.0.1");
        desiredCapabilities.setCapability("appPackage","com.tencent.mobileqq");
        desiredCapabilities.setCapability ( "appActivity", "activity.LoginActivity." );
         // optional parameter
         // Do not reset signature 
        desiredCapabilities.setCapability ( "nosign", to true );
         // not clear the cache 
        desiredCapabilities.setCapability ( "noReset" , to true );
         // set to use unicode keyboard, so that the implementation process automation scripts can input Chinese, used in pairs, alone following a certain attribute the ignore 
        desiredCapabilities.setCapability ( "unicodeKeyboard", to true );
        desiredCapabilities.setCapability("resetKeyboard",true);

        // When a plurality of devices connected to the computer, to specify equipment
         // desiredCapabilities.setCapability ( "UDID", "127.0.0.1:7555");

        // create driver objects 
        AndroidDriver driver = new new AndroidDriver ( new new the URL of ( "http://127.0.0.1:4723/wd/hub" ), desiredCapabilities);
        Thread.sleep ( 3000 );
         // positioning and operating elements 
        driver.findElement (MobileBy.AccessibilityId ( "Please enter the QQ number or mobile or mail.")) SendKeys ( "123456." );
        driver.findElement(By.xpath("//android.widget.EditText[@content-desc='密码 安全']")).sendKeys("132456");
        driver.findElement(By.id("com.tencent.mobileqq:id/login")).click();

        Thread.sleep ( 5000 );
         // Close APP and Driver 
        driver.closeApp ();
        driver.quit();

        // Close appium-server command window and 
        String killCmd = "the taskkill / F. / The IM cmd.exe" ;
        String killNode="taskkill /F /IM node.exe";

        Runtime.getRuntime().exec(killNode);
        Runtime.getRuntime().exec(killCmd);
    }

Guess you like

Origin www.cnblogs.com/yjh1995/p/12147647.html