appium(12)-The starting of an app
Steps:
-
you have to prepare environment for Android. Details are provided here: http://appium.io/slate/en/master/?java#setup-(android)//准备安卓环境。
-
you have to download the desktop app for Windows or Mac OS X or install it using npm $ npm install -g appium or $ npm install appium@required_version//安装appium server。
-
it needs to launch the appium server. If you use the server installed via npm then
$ node the_path_to_js_file --arg1 value1 --arg2 value2 where the_path_to_js_file is the full path to appium.js file (if the node server version version <= 1.4.16) or main.js (if the node server version version >= 1.5.0). It is not necessary to use arguments. The list of arguments: http://appium.io/slate/en/master/?java#appium-server-arguments//启动appium server。
The starting of an app
It looks like creation of a common RemoteWebDriver instance.
Common capabilities provided by Java client
Android-specific capabilities provided by Java client
//范例一:启动一个app。
1 import java.io.File; 2 import org.openqa.selenium.remote.DesiredCapabilities; 3 import io.appium.java_client.AppiumDriver; 4 import io.appium.java_client.android.AndroidDriver; 5 import io.appium.java_client.MobileElement; 6 import java.net.URL; 7 8 ... 9 File app = new File("The absolute or relative path to an *.apk file"); 10 DesiredCapabilities capabilities = new DesiredCapabilities(); 11 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); 12 capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); 13 //you are free to set additional capabilities 14 AppiumDriver<MobileElement> driver = new AndroidDriver<>( 15 new URL("http://target_ip:used_port/wd/hub"), //if it needs to use locally started server 16 //then the target_ip is 127.0.0.1 or 0.0.0.0 17 //the default port is 4723 18 capabilities);
//范例二:启动手机浏览器。
If it needs to start browser then:
1 import org.openqa.selenium.remote.DesiredCapabilities; 2 import io.appium.java_client.remote.MobileBrowserType; 3 import io.appium.java_client.AppiumDriver; 4 import io.appium.java_client.android.AndroidDriver; 5 import org.openqa.selenium.remote.RemoteWebElement; 6 import java.net.URL; 7 8 9 ... 10 DesiredCapabilities capabilities = new DesiredCapabilities(); 11 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); 12 capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.CHROME);//使用谷歌浏览器 13 //if it is necessary to use the default Android browser then MobileBrowserType.BROWSER//使用自带浏览器 14 //is your choise 15 ... 16 //you are free to set additional capabilities 17 AppiumDriver<MobileElement> driver = new AndroidDriver<>( 18 new URL("http://target_ip:used_port/wd/hub"), capabilities);
or
1 import org.openqa.selenium.remote.DesiredCapabilities; 2 import io.appium.java_client.remote.MobileBrowserType; 3 import io.appium.java_client.remote.MobilePlatform; 4 import org.openqa.selenium.remote.RemoteWebDriver; 5 import java.net.URL; 6 7 8 ... 9 DesiredCapabilities capabilities = new DesiredCapabilities(); 10 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID); 11 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); 12 capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.CHROME); 13 //you are free to set additional capabilities 14 RemoteWebDriver driver = new RemoteWebDriver( 15 new URL("http://target_ip:used_port/wd/hub"), capabilities);