Android Mobile Testing Using Appium(Java)
Below I tried to automate Chess game using Appium, because I saw very few documents for java with Appium so I thought to share the knowledge
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import com.framework.core.Global;
public class AndroidNativeAppTest {
@Test (description = "Automate Chess App")
public void Test01_AndroidNative_Play() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("appium-version", "1.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("deviceName", "192.x.x.x:5555");
capabilities.setCapability("app", Global.getReportsDir() + File.separator + "Mobile_App" + File.separator + "Chess Free.apk");
capabilities.setCapability("appPackage", "uk.co.aifactory.chessfree");
capabilities.setCapability("appActivity", ".ChessFreeActivity");
AppiumDriver wd = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
wd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
Thread.sleep(10000);
// Tap sound Off/on -> Play
try {
wd.findElement(By.xpath("//android.widget.CheckBox[1]")).click();
Thread.sleep(2000);
wd.findElement(By.xpath("//android.widget.CheckBox[1]")).click();
Thread.sleep(2000);
} catch(Exception e) {
System.out.println("No sound");
}
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
// Two player -> Back
wd.findElement(By.xpath("//android.widget.Button[2]")).click();
wd.findElement(By.xpath("//android.widget.Button[2]")).click();
//options -> Green background -> Back
wd.findElement(By.xpath("//android.widget.Button[3]")).click();
wd.findElement(By.xpath("//android.widget.ImageButton[2]")).click();
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
Thread.sleep(5000);
// Single player -> play
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
Thread.sleep(5000);
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
Thread.sleep(5000);
wd.quit();
} finally {
wd.quit();
}
}
}
Chess Free App
Chess Free app is a Native application that works on both tablets and phones, The app details can be found : https://play.google.com/store/apps/details?id=uk.co.aifactory.chessfree&hl=enimport io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import com.framework.core.Global;
public class AndroidNativeAppTest {
@Test (description = "Automate Chess App")
public void Test01_AndroidNative_Play() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("appium-version", "1.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("deviceName", "192.x.x.x:5555");
capabilities.setCapability("app", Global.getReportsDir() + File.separator + "Mobile_App" + File.separator + "Chess Free.apk");
capabilities.setCapability("appPackage", "uk.co.aifactory.chessfree");
capabilities.setCapability("appActivity", ".ChessFreeActivity");
AppiumDriver wd = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
wd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
Thread.sleep(10000);
// Tap sound Off/on -> Play
try {
wd.findElement(By.xpath("//android.widget.CheckBox[1]")).click();
Thread.sleep(2000);
wd.findElement(By.xpath("//android.widget.CheckBox[1]")).click();
Thread.sleep(2000);
} catch(Exception e) {
System.out.println("No sound");
}
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
// Two player -> Back
wd.findElement(By.xpath("//android.widget.Button[2]")).click();
wd.findElement(By.xpath("//android.widget.Button[2]")).click();
//options -> Green background -> Back
wd.findElement(By.xpath("//android.widget.Button[3]")).click();
wd.findElement(By.xpath("//android.widget.ImageButton[2]")).click();
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
Thread.sleep(5000);
// Single player -> play
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
Thread.sleep(5000);
wd.findElement(By.xpath("//android.widget.Button[1]")).click();
Thread.sleep(5000);
wd.quit();
} finally {
wd.quit();
}
}
}
It is really an awesome information with lot of knowledge It is just an fantastic one.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery