Tuesday, February 3, 2015

Sample Appium code

Hi Friends, below is the sample code written for opening chrome browser in your Android device. 


import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import io.appium.java_client.AppiumDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Appiumweb {

private WebDriver driver;

@BeforeMethod
public void setup() throws MalformedURLException
{
//File classpathroute= new File(System.getProperty("user.dir"));
//File appdir=new File (classpathroute,"D://automation//AppiumTest//Application");
//File appDir = new File(classpathroute, "Application");
//File app=new File(appDir, "ApiDemos-debug.apk");
  //capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
   
          DesiredCapabilities capabilities = new DesiredCapabilities();
       capabilities.setCapability("deviceName", "Android");
       capabilities.setCapability("platformName", "Android");
       capabilities.setCapability("platformVersion", "4.4.4");
       capabilities.setCapability("browserName", "Chrome");
       driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

//Note Below code is for opening any Android application in Android device. 
/*  capabilities.setCapability("deviceName","Android");
       capabilities.setCapability("platformVersion", "4.4.2");
       capabilities.setCapability("platformName","Android");
       capabilities.setCapability("app", app.getAbsolutePath());
       capabilities.setCapability("appPackage", "io.appium.android.apis");
       capabilities.setCapability("appActivity", ".ApiDemos");
      driver=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);*/
     
}

  @Test
   public void apiDemo() throws InterruptedException{
 
  driver.get("http://jaingourav999.blogspot.com");

String pagetitle=driver.getCurrentUrl();
System.out.println("The current page title is"+ pagetitle);
 
 
Thread.sleep(10000l);
   }
 
  @AfterMethod
   public void tearDown() throws Exception {
       driver.quit();
   }
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

Introduction to Appium

 Purpose:

This blog is written for running the NGE Automation code to Chrome browser of Android device, with the help of Appium.  

About the Appium:

Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. Native apps are those written using the iOS or Android SDKs. Mobile web apps are web apps accessed using a mobile browser (Appium supports Safari on iOS and Chrome or the built-in 'Browser' app on Android).

Importantly, Appium is "cross-platform": it allows you to write tests against multiple platforms (iOS, Android), using the same API. This enables code reuse between iOS and Android testsuites.

It supports:
Mac OSX 10.7+ or Windows 7+ or Linux
Android SDK ≥ 16 (SDK < 16 in Selendroid mode)

Below image shows how Appium works:




Prerequisite before starting:

1.       Install Eclipse
2.       Install JAVA JDK, and set the path to windows preference
3.       Configure the Android SDK, and set the path to windows preference. (Instructions give in the doc)
4.       Download Appium, and configured Appium jar (java-client)+GSON jar+Selenium jar files. 


 Install Eclipse
      Download the eclipse.


Installing and configuring the Android SDK
1.       Download the standalon sdk tool from https://developer.android.com/sdk/index.html#Other
2.       By default, the Android SDK does not include everything you need to start developing. The SDK separates tools, platforms, and other components into packages you can download as needed using the Android SDK Manager. So before you can start, there are a few packages you should add to your Android SDK.
       3. To start adding packages, launch the Android SDK Manager in one of the following ways: (Time        consuming process)
          3.1  Windows: Double-click theSDK Manager.exe file at the root of the Android SDK directory.
Set the path on windows of SDK

1. Add the platform-tools\ directory to your Windows path: Ex: ;C:\Development\Android\android-sdk-windows\platform-tools\

2. Set the Android_Home: Ex
;Development\Android\android-sdk-windows\


Configuing installed Android SDK with Eclipse

Launch Eclipse->Windows->Preference->Android->Click on Browse->Locate the installed Android SDK folder.

Launch Appium

1.       Open the downloaded Appium
2.       Double-click on Appium.exe, and launch the Appium server.  



Write code
Run the application:
1.       Connect the Android device.
2.       Run the test suite file.

Monday, February 2, 2015

Open Google chrome browser by selenium

Hi Friends,

Below is the sample code, which will help you to open chrome browser through selenium.

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;

public class openingChrome {
    public WebDriver driver;

    // Launching Browser
    @BeforeTest
    public void start() {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    // Opening webpage
    @Test
    public void openHomepage() {
        driver.get("http://jaingoruav999.blogspot.com");

    }

    // Closing browser
    @AfterTest
    public void teardown()

    {

        driver.quit();
    }
}