Launching a web browser in your Android device using Appium


From the previous article you know what are Desired capabilities. Now we will quickly launch a browser using in your device. Before diving in, you need to connect your device to the system (Debugging – On). And get the device name using the command ‘adb devices’. By default, Appium will run on port 4723. You can change it as per your need. We will see that later. Just copy and paste the below code, I will explain code after the execution.

Things to do:
Connect your device or launch emulator
Get the device name – type ‘adb devices
Set the capabilities – refer article
Launch Appium server – type ‘appium

EmulatorStarted

Just open your IDE (Eclipse) and add the required jars to your project buildpath (selenium server, appium javaclient). You may use Maven dependencies as well. Here i’m using jars.
Create a class in your project, let’s say ‘LaunchMobileBrowser‘ and paste the below code in that class. Now, open your Terminal and type ‘appium‘ command. A session should be started by showing it in your terminal
private AndroidDriver driver;

@BeforeTest
public void setUp() throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.BROWSER_NAME, “Browser“);
//set the platform version
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, “4.4.2“);
caps.setCapability(“platformName”, “Android“);
//set your Device name. My device name was Charan007
caps.setCapability(“deviceName”, “Charan007“);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”),caps);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void launchApplication() throws Exception {
driver.get(“http://appium.io/”);
}

So, a browser has been launched and navigated to ‘Appium‘ official page right! Okay, what did we code actually.

Explanation:
As you know, we need to set desired capabilities (key and value pair) in order to start a session. Here, we need to set Browser capability, your device name, your platform version. So all these values will go as a ‘POST’ request to Appium server saying that, Hey! Charan007 Android mobile having 4.4.2 version want to invoke a URL ‘http://appium.io’. Then UIAutomator/Selendroid/Appium will talk to bootstrap.jar which is running your device to perform an action. Then bootstrap.jar acts a TCP server to perform an action. That’s how appium works

Hope this is clear. You can download the code from here.

Advertisements

4 thoughts on “Launching a web browser in your Android device using Appium

  1. Anonymous April 14, 2017 / 1:47 am

    Hi have tried the above code, but it does not work for me. The Chrome opens up 4 times and shuts down. Here is the log:
    info: [debug] Responding to client with error: {“status”:33,”value”:{“message”:”A new session could not be created. (Original error: A new session could not be created. (Original error: session not created exception\nfrom unknown error: Runtime.executionContextCreated has invalid ‘context’: {\”auxData\”:{\”frameId\”:\”2054.1\”,\”isDefault\”:true},\”id\”:1,\”name\”:\”\”,\”origin\”:\”://\”}\n (Session info: chrome=57.0.2987.132)\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64)))”,”status”:33,”value”:{“message”:”session not created exception\nfrom unknown error: Runtime.executionContextCreated has invalid ‘context’: {\”auxData\”:{\”frameId\”:\”2054.1\”,\”isDefault\”:true},\”id\”:1,\”name\”:\”\”,\”origin\”:\”://\”}\n (Session info: chrome=57.0.2987.132)\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64)”},”httpCode”:200,”origValue”:”A new session could not be created. (Original error: session not created exception\nfrom unknown error: Runtime.executionContextCreated has invalid ‘context’: {\”auxData\”:{\”frameId\”:\”2054.1\”,\”isDefault\”:true},\”id\”:1,\”name\”:\”\”,\”origin\”:\”://\”}\n (Session info: chrome=57.0.2987.132)\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64))”},”sessionId”:null}
    > info: info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”shutdown”}
    > info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
    > info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:”OK, shutting down”}
    > info: [debug] [BOOTSTRAP] [debug] Closed client connection
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
    > info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
    > info: [debug] [UIAUTOMATOR STDOUT] Time: 21.081
    > info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
    > info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
    > info: [debug] Sent shutdown command, waiting for UiAutomator to stop…
    > info: [debug] UiAutomator shut down normally
    > info: [debug] executing cmd: “C:\SDK file\platform-tools\adb.exe” -s 05157df5bdf60b19 shell “am force-stop com.android.chrome”
    > info: [debug] Stopping logcat capture
    > info: [debug] Logcat terminated with code null, signal SIGTERM
    > info: [debug] Cleaning up appium session
    Please assist!
    Thanks

    Like

    • Hari Charan April 14, 2017 / 1:05 pm

      Are you using emulator or real device? Are you using Appium version 1.6?
      Can you try changing ‘BROWSER_NAME, “Browser“’ to CHROME
      Also add chrome application launch activity and package name in Desired capabilities. This article was published when Appium version was 1.4

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s