Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Using Selenic with Appium

benken_parasoft
benken_parasoft Posts: 1,302 ✭✭✭

I am happy to share some tips about using Parasoft Selenic with Appium tests for mobile web apps.

First, let's look at how to record actions that Selenic can use to automatically generate an Appium test. The Parasoft Recorder extension for the Chrome desktop browser can be used to record actions against a mobile web app. Chrome must first be switched into a special mode to simulate a mobile device. To do this, open Chrome Dev Tools, click the "Toggle Device Toolbar" button then select your desired type of mobile device. Now, click the Start Recording button in the Parasoft Recorder then simply navigate to your mobile web app to begin recording the desired actions. When finished, click the Stop Recording button in the Parasoft Recorder then save your recording file to disk.

Next, the recording can be provided to Selenic to generate an Appium test. To do this, open Selenic (IDE plugin) then select "File > New > Selenium Java Project from Recording". Provide Selenic with the path to your recording and other requested information then click Finish. The project is setup and ready to use for Appium except for two things. :) We need to add Appium java client libraries to the project's build path and then need to reconfigure the test to connect to the desired Appium server.

The new java project has the Selenium java library and dependencies added to the project's build path. Now, we just need to add the java libraries for the Appium Java client. I was able to do this by downloading these extra jars then adding them to the project's build path (right-click project > Properties > Java Build Path):

https://search.maven.org/remotecontent?filepath=io/appium/java-client/7.2.0/java-client-7.2.0.jar
https://search.maven.org/remotecontent?filepath=org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar
https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar
https://search.maven.org/remotecontent?filepath=commons-io/commons-io/2.6/commons-io-2.6.jar

After updating the build path we need to reconfigure the test to connect to an Appium server. In the main test class, locate the block of code that is instantiating ChromeDriver. It should look something like this:

driver = new ChromeDriver();
driver.manage().window().maximize();

To use an Appium server, we need to change this code to instead instantiate the AndroidDriver or IOSDriver from the Appium java client, passing in the capabilities expected by the Appium server. For example, this is what I used to connect to an Appium server on my local machine for playback in Chrome on an emulated Android device:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("w3c", false);
capabilities.merge(chromeOptions);
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

The capabilities being configured depends on what Appium server is being used and what mobile device and mobile browser will be used to execute the steps in the test. The Appium docs describe the capabilities that are available. However, if you are not using your own Appium server and are instead needing to connect to a mobile device (cloud) provider, then the capabilities often need to be setup in a way that is very specific to that provider.

Once you have your Appium test you can run it with Selenic no different than any other Selenium Test. Selenic can generate recommendations, heal bad locators, etc. no different than if the test were being executed using a desktop browser.

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,302 ✭✭✭
    edited November 12

    To use an Appium server, we need to change this code to instead instantiate the AndroidDriver or IOSDriver from the Appium java client, passing in the capabilities expected by the Appium server.

    Below are some examples for Appium 2 and the current Appium java-client.

    Chrome on Android:

    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.android.options.UiAutomator2Options;
    import io.appium.java_client.remote.MobileBrowserType;
    
    UiAutomator2Options options = new UiAutomator2Options()
            .setAvd("Pixel_8_API_35")
            .withBrowserName(MobileBrowserType.CHROME);
    options.setCapability("appium:hideKeyboard", true);
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
    

    I recommend starting the Appium server so that ChromeDriver is downloaded automatically:
    appium server --allow-insecure chromedriver_autodownload

    Safari on iOS:

    import io.appium.java_client.ios.IOSDriver;
    import io.appium.java_client.ios.options.XCUITestOptions;
    import io.appium.java_client.remote.MobileBrowserType;
    
    XCUITestOptions options = new XCUITestOptions()
            .setDeviceName("iPhone 16 Pro")
            .setPlatformVersion("18.1")
            .withBrowserName(MobileBrowserType.SAFARI)
            .autoAcceptAlerts();
    driver = new IOSDriver(new URL("http://127.0.0.1:4723"), options);