Mobile Web Automation with Appium

26 / Oct / 2015 by Kimi Agarwal 12 comments

Appium is an open source tool for mobile application. It is a bridge or we can say in simple terms it is a mediator between PC and mobile.

It supports three types of mobile application i.e. native, web application and hybrid.

Native Applications are developed for use on a particular device or platform. It is installed directly on a mobile device and developers create a separate app version for each mobile device.

Mobile web application are called mobile website and can be accessed through browser by hitting the URL.

Hybrid application combines elements of both native and web applications. Like mobile native apps, you can find and download mobile hybrid apps using the Google Play Store or the Apple’s App Store. Similar to mobile web app, a mobile hybrid app would look alike to a mobile website that can be accessed by hitting an URL in the web browser.

For example Web apps, such as online banking services or locally stored native apps, such as Microsoft Excel.

To install an Appium we need .net framework

What are the desired capabilities?

Desired Capabilities are a set of keys and values sent to the Appium server to tell the server what kind of automation session we are interested in starting up.

 

KEYS DESCRIPTION
browserName Name of mobile web browser
appium-version Version of Appium
platform Version Mobile OS version
deviceName Kind of mobile devices to use
platformName Kind of mobile OS platform to use

Automation Test

  • Attach Android device with your system and type adb device command on cmd prompt to verify that the device is attached or not
  • Launch Appium tool on your machine
  • Once the Appium is launched run the script written on the Eclipse

Use case we are going to automate:

  1. To check the title of the web page
  2. Enter all the details in the Contact Us form except message detail
  3. Click on the Submit button
  4. To verify the error message.


appium_image

//setup the desired capability and pass the android activity

DesiredCapabilities cap = new DesiredCapabilities();

cap.setCapability(“browserName”, “chrome”);

cap.setCapability(“appium-version”, “1.4.0”);

cap.setCapability(“VERSION”,“5.1.1”);

cap.setCapability(“deviceName”,“ANDROID”);

cap.setCapability(“platformName”,“ANDROID”);

RemoteWebDriver driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”), cap);

// Navigate to the page and interact with the elements

driver.get(“http://www.tothenew.com/contact-us”);

System.out.println(“Android Title is: “ + driver.getTitle());

driver.findElement(By.id(“firstname”)).sendKeys(“kimi”);

driver.findElement(By.id(“lastname”)).sendKeys(“agarwal”);

driver.findElement(By.id(“email”)).sendKeys(“kimiagarwal@yahoo.com”);

driver.findElement(By.id(“phone”)).sendKeys(“9999123456”);

driver.findElement(By.id(“company”)).sendKeys(“ABC Company”);

driver.findElement(By.xpath(“.//*[@type=’checkbox’][@value=’Job’]”)).click();

driver.findElement(By.xpath(“.//*[@type=’submit’]”)).click();

WebElement msg = driver.findElement(By.id(“message-error”));

System.out.println(msg.getText());

Assert.assertEquals(“Please enter the message”, msg.getText());

Tell me what you think of this in the comments section. Happy testing :)

FOUND THIS USEFUL? SHARE IT

comments (12)

  1. Ritesh

    I am new to Appium…I am successfully able to launch the app successfully through the code as mentioned below :
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, “”);
    capabilities.setCapability(“deviceName”, “My Device”);
    capabilities.setCapability(“platformVersion”, “6.0”);
    capabilities.setCapability(“platformName”, “Android”);
    // capabilities.setCapability(“app”, app.getAbsolutePath());
    capabilities.setCapability(“app”, “C:\\RITESH\\SELENIUM\\APPIUM\\Appium Setup\\Setup\\SGMA PREPROD_Build_026.apk”);
    capabilities.setCapability(“appPackage”, “com.sgma.preprod”);
    capabilities.setCapability(“appActivity”, “com.sgma.preprod.SGMAPREPROD”);
    // For url check your Appium setting
    driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
    WebDriverWait wait=new WebDriverWait(driver, 20);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    // Clear the user id
    //driver.findElement(By.xpath(“//android.widget.EditText[@index=’0′]”)).clear();
    driver.findElement(By.xpath(“//android.widget.EditText[@index=’0′]”)).sendKeys(“8378034″);
    //Enter Password
    driver.findElementByName(“Enter password”).click();

    Every time I run this code in selenium every time it un-install and install the app.
    I wanted the scenario, where on my 2nd run the code just launch the app without any un-installing ?
    Please help me what changes I should do in my code?
    I wanted to

    Reply
  2. balaji wanole

    Hi I am new to appium n I want to do Automation for hybrid app developed using javascript/html/css running on Amazon fire tv .So I need help if you have anything to share as of now I am stucked with not able to launch UI automator

    Reply
  3. Datta

    I already written test for desktop,
    I wanted to reuse whatever maximum I can reuse
    few methods like driver.getCurrentUrl(); not not working,
    What should be my approach to write test?

    Reply
  4. Neethu

    Can u please show this code in TestNG framework .. am using the same code but when it runs gives an error of “A new session could not be created”INFO: Attempting bi-dialect session, assuming Postel’s Law holds true on the remote end
    I have done the updation mentioned above but it not works for me..please help me to solve this .. Thanks in advnace

    Reply
  5. Divya

    While running the above code I am getting org.openqa.selenium.remote.SessionNotFoundException: no such session. Can you please guide how to resolve it?

    Reply
    1. Kimi Agarwal

      Kindly download and install the latest version of Appium in your system and add the latest version of java-client.jar in the project

      Reply
    1. Kimi Agarwal

      Step-by-Step Installation:
      1: Install and setup Java on your system.
      2. Install ANDROID SDK, by default ANDROID SDK is installed in C drive, copy the folder and paste into another drive.
      3. Now Setup ANDROID_HOME
      D:\SDK
      4. Now Setup Path
      Give Tools and Platform Tools path in the User Variable
      D:\SDK\platform-tools;D:\SDK\tools;
      Restart the system
      5. Now check android is installed or not on your system, type adb command in cmd prompt window
      6. Setup Eclipse, launch it and create a new project.
      7. Add Selenium standalone jar, Java-client.jar. in the project
      8. Install device PC suite on your system, enable developer option on your device and allow USB debugging
      9. Connect your device into your system
      10. Type adb devices command in cmd prompt
      adb devices command tell whether your device is connected to your system or not
      11. Install Appium, click on Appium.exe and launch it by clicking on the Play button
      12. Once the Appium is launched run the script written on the Eclipse

      Reply
      1. vikrant

        im getting such type of error while launching this code.

        Exception in thread “main” org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not proxy command to remote server. Original error: Invalid attempt to destructure non-iterable instance) (WARNING: The server did not provide any stacktrace information)
        Command duration or timeout: 0 milliseconds
        Build info: version: ‘unknown’, revision: ’86a5d70′, time: ‘2017-02-16 07:47:51 -0800′
        System info: host: ‘HT13-PC’, ip: ‘192.168.1.4’, os.name: ‘Windows 7′, os.arch: ‘amd64′, os.version: ‘6.1’, java.version: ‘1.8.0_121′
        Driver info: driver.version: RemoteWebDriver

        Reply

Leave a comment -