Page tree
Skip to end of metadata
Go to start of metadata

Last updated: May 26, 2020 15:21

Sample Java code generated by Object Spy for an instrumented Hybrid application

The following code sample was recorded by the Object Spy tool of the Perfecto plugin for Eclipse. This demonstrates the plugin support for Hybrid applications and the switching of context between the Native and Webview objects of the application.
Notes on recorded code:
  • Application must be instrumented when installing on the device. Note the use of the "autoInstrument" capability in the sample.
  • Use More->Settings to verify that the Application Framework is set to Appium and the Driver is set to Appium Driver. Go here for more information on the Settings.
  • Consider refactoring the code recorded by Object Spy to eliminate superfluous context switching calls, variable declarations, and correct the tabbing of the code.
  • Supply proper XPath specifications within Object Spy to avoid using the default XPath used by the Object Spy. Otherwise, you should refactor the code to use more robust specifications.
public class AppiumTest {
    public static void main(String[] args) throws MalformedURLException, IOException {
        System.out.println("Run started");
        String browserName = "mobileOS";
        DesiredCapabilities capabilities = new DesiredCapabilities(browserName, "", Platform.ANY);
        String host = "myHost.perfectomobile.com";
        capabilities.setCapability("user", "myUser");
        capabilities.setCapability("password", "myPassword");
        capabilities.setCapability("deviceName", "12345");
        capabilities.setCapability("automationName", "Appium");
        // Call this method if you want the script to share the devices with the Perfecto Lab plugin.
        setExecutionIdCapability(capabilities, host);
        // Application settings examples.
        capabilities.setCapability("app", "PRIVATE:applications/com.voyagesoftech.myexpensemanager.apk");
        capabilities.setCapability("autoInstrument", true);
        // For Android:
        capabilities.setCapability("appPackage", "com.voyagesoftech.myexpensemanager");
        // capabilities.setCapability("appActivity", ".activities.BrowseActivity");
        // For iOS:
        // capabilities.setCapability("bundleId", "com.yoctoville.errands");
        AndroidDriver driver = new AndroidDriver(new URL("https://" + host + "/nexperience/perfectomobile/wd/hub"), capabilities);
        // IOSDriver driver = new IOSDriver(new URL("https://" + host + "/nexperience/perfectomobile/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        try {
            // write your code here
            // following code was recorded by the Object Spy recorder 
            // comments describe the steps
            // Click on "Expense" to enter a new expense (DOM object)
            driver.context("WEBVIEW");
            driver.findElementByXPath("//*[text()='Expense']").click();
            // Enter a category for the expense (DOM object)
            driver.context("WEBVIEW");
            driver.findElementByXPath("(//select[@id=\"CategoryExpense\"])[1]").click();
            // Select the  category from the menu of radio buttons (Native object)
            driver.context("NATIVE_APP");
            driver.findElementByXPath("//android.widget.CheckedTextView[@text='Miscellaneous']").click();
            // Enter a sub-category for the expense (DOM object)
            driver.context("WEBVIEW");
            driver.findElementByXPath("(//select[@id=\"SubCategoryExpense\"])[1]").click();
            // Select the sub-category from the menu of radio buttons (Native object)
            driver.context("NATIVE_APP");
            driver.findElementByXPath("//android.widget.CheckedTextView[@text='Office']").click();
            // type in the description of the expense (DOM object)
            driver.context("WEBVIEW");
            driver.findElementByXPath("(//input[@id=\"EDescription\"])[1]").sendKeys("Notebooks");
            // type in the amount of the expense (DOM object)
            driver.context("WEBVIEW");
            driver.findElementByXPath("(//input[@id=\"Amountforex\"])[1]").sendKeys("3.75");
            // click on Save button (DOM object)
            driver.context("WEBVIEW");
            driver.findElementByXPath("//*[text()='Save']").click();
            // verify that showing the notification window (Native object)
            driver.context("NATIVE_APP");
            List<WebElement> elements21 = driver.findElementsByXPath("//android.widget.TextView[@text='Expense added succesfully']");
            // click ok on notification window (Native object)
            driver.context("NATIVE_APP");
            driver.findElementByXPath("//android.widget.Button[@text='OK']").click();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                driver.close();
                // In case you want to down the report or the report attachments, do it here.
                // PerfectoLabUtils.downloadReport(driver, "pdf", "C:\\test\\report");
                // PerfectoLabUtils.downloadAttachment(driver, "video", "C:\\test\\report\\video", "flv");
                // PerfectoLabUtils.downloadAttachment(driver, "image", "C:\\test\\report\\images", "jpg");
            } catch (Exception e) {
                e.printStackTrace();
            }
            driver.quit();
        }
        System.out.println("Run ended");
    }