In the Mobile world, we see multiple types of apps namely Native App, Hybrid App & the browser based application, Web App. Also, we see multiple types of operating systems like Android, iOS, Windows, Firefox OS etc.
Automated testing of these many variation of apps, is very tricky & difficult task. Not all of them are supported by single automation tool. Having said that, Appium which is built over Selenium webdriver supports most of the OS & apps with various configurations.
In this article, you would know a way to automate the Hybrid App using AndroidDriver.
Prerequisites:
- Android SDK is downloaded and installed. (ANDROID_HOME env. variable should be set)
- Either Emulator or Real device (USB debugging enabled) is available.
- Appium desktop app or non GUI package is installed.
Steps:
- Create a java project in eclipse 'AppiumMobileTesting'.
- Create a class 'TestHybridApp.java'.
- Connect your device(USB debugging enabled), to system's USB port & accept if any confirmation pop ups.
- Start Appium Server. Once the device is connected, open the 'Appium Inspector Session' providing the suitable capabilities.
- Open the Hybrid app in the device and you see the same screen on inspector session.
- Click on the required control in inspector session & then the related locator details are shown on the right hand side in appium inspector.
- Add the below code snippet to the 'TestHybridApp' class which performs the below actions:
- Open the Hybrid App using the Appium capabilities.
- Click on 'WebView Demo' list item.
- Enter any url (www.amazon.com) and click on Go.
- Accept the alert.
- Click on clear button
- Enter 'appiumpro.com' & click go.
- Capture the screenshot
Appium Inspector |
public static void main(String[] args){
AppiumServer server = new AppiumServer();
TestHybridApp testapp = new TestHybridApp();
String realDevicename = "MyDevice";
String avdName = "Pixel_2_API_28";
String appName = "TheApp-v1.10.0.apk";
try {
//Start AVD OR Connect Real device
//server.launchEmulator(avdName);
File cr = new File(System.getProperty("user.dir"));
File appDir = new File(cr , "/apps");
File app = new File(appDir.getCanonicalPath(), appName);
//Start Appium Server
server.startAppium();
//Set Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("deviceName", realDevicename);
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("noReset", false);
caps.setCapability("fullReset", false);
caps.setJavascriptEnabled(true);
//this capability install the app if not installed
caps.setCapability("app", app.getAbsolutePath());
//To unlock automatically if the device is locked
caps.setCapability("unlockType", "pin");
caps.setCapability("unlockKey", "0000");
AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL(server.getServiceUrl()),caps);
//Test Application
testapp.testSampleApp(driver);
} catch (Exception e) {
server.stopAppium();
e.printStackTrace();
}finally {
server.stopAppium();
}
}
public void testSampleApp(AppiumDriver driver) {
By hybridScreen = MobileBy.AccessibilityId("Webview Demo");
By urlInput = MobileBy.AccessibilityId("urlInput");
By navigateBtn = MobileBy.AccessibilityId("navigateBtn");
By clearBtn = MobileBy.AccessibilityId("clearBtn");
try {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(hybridScreen)).click();
//Get to native context
driver.context("NATIVE_APP");
MobileElement input = (MobileElement) wait.until(ExpectedConditions.presenceOfElementLocated(urlInput));
input.sendKeys("https:///www.amazon.com");
driver.findElement(navigateBtn).click();
Thread.sleep(1000); //Wait for alert
driver.switchTo().alert().accept();
// Now try to go to Appium Pro
driver.findElement(clearBtn).click();
input.sendKeys("https://appiumpro.com");
driver.findElement(navigateBtn).click();
Thread.sleep(1000);
//Take the screenshot
File file = ((TakesScreenshot)driver).
getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("./output/appiumpro.jpg"));
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
NOTE: You can download the sample hybrid app from the below location:
Great job
ReplyDeleteReally you have done a good job. Thanks for sharing this valuable information....
ReplyDeleteAppium Training In Chennai
Appium Training Online
kuşadası
ReplyDeletemilas
çeşme
bağcılar
siirt
6S5SP