Author: Mao TAKAHASHI
Published in: June 1, 2023
I usually use Appium to create automated tests, and Appium's github shows that Appium 2.0 is a beta version, but the following is noted.
Appium core team does not maintain Appium 1.x anymore since the 1st of January 2022. All recent versions of officially supported platform drivers are not compatible to Appium 1.x anymore, and require Appium 2 to run. Please read the migration guide from 1.x to 2.0 to manage the Appium server.
I can still run the app test with Appium 1.x, but I will describe how to run the test after updating from Appium 1.x to Appium 2.0 without significant impact.
Environment
Environment | Version |
OS | macOS Ventura 13.4 |
Programing language | Java 17 |
Test framework | JUnit 5.8.1 |
Appium | 1.22.3 to 2.0.0-beta.71 |
Node | v16.15.1 |
npm | 9.6.6 |
Xcode | 14.3 |
iOS | 16.3 |
io.appium:java-client | 8.5.0 |
Migration Methods
The migration method to Appium 2.0 is also described in detail in the official documentation. Basically, you can follow the changes here.
The actual command I operated is as follows.
** I had previously thought of trying a flutter-driver, which I had already installed. Because of this, some parts of the procedure did not work as expected.
# upgrade to appium2.0
$ npm install -g appium@next
# install uiautomator2(for android) and xcuitest(for ios)
$ npm install --global appium --drivers=xcuitest,uiautomator2
# check installed driver
$ appium driver list --installed
✔ Listing installed drivers
- flutter@1.12.0 [installed (npm)]
- xcuitest@4.30.1 [installed (npm)]
- uiautomator2@2.25.1 [installed (npm)]
This completes the update. It's an easy update!
If you were unable to install Appium with just this command, uninstall Appium 1.x. However, npm's uninstall command alone may not completely remove it; make sure that Appium in node_module is completely removed before installing Appium 2.0 again.
Building WebDriverAgent
When running iOS app tests, a certificate must be set up. Once you build WebDriverAgent and if it succeeds, the app test will work reliably, so build it manually.
** Another way is to run the command. However, there were several times when it did not work, so I will describe how to build it manually.
- Open
WebDriverAgent.xcodeproj
in Xcode - Select the Team you want to use under Team in Signing & Capabilities in Xcode
- Connect and build your iPhone.
In my case, I installed Node with nodebrew, so the xcode project was saved in the following path.
/Users/(username)/.nodebrew/node/v16.15.1/lib/node_modules/appium-flutter-driver/node_modules/appium-xcuitest-driver/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj
Appium’s website shows it as $APPIUM_HOME/node_modules/appium-xcuitest-driver/node_modules/appium-webdriveragent
.I have not set $APPIUM_HOME
in the environment variables myself. But $APPIUM_HOME
was somehow recognized as /Users/(username)/.nodebrew/node/v16.15.1/lib/node_modules/appium-flutter-driver/
.
If you did not find WebDriverAgent.xcodeproj
in the location you expected, it may be in an unexpected location, so please look for it.
Select it for all objects in TARGETS to avoid rework.
Developer mode on the iPhone must be turned on beforehand.
Find out the bundleID of the app
Find out the bundleID of the app you want to test. There are two ways to find out the bundleID: use (1) if the app was installed from the Apple Store, otherwise use (2).
Method 1
- Search for the app you want to get the bundleID from App Store.
- Note down the id part of the URL.
- Open the following URL. https://itunes.apple.com/lookup?id=[noted down id]
- Find the bundleID in the downloaded file.
a text file wil be downloaded.
Method 2
- Connect iPhone to PC.
- Launch "Console," a utility that is installed as standard on the Mac.
- Launch the application you want to get the bundleID for on the iPhone.
- Get bundleID from console.
It is easier to find them by searching for the name of the application, for example.
Prepare and execute source code
First, start Appium in a terminal.
$ appium server
Next, write and execute the source code. Here is the code we used this time.
The iOS Settings app will launch, click on Bluetooth, and the back operation should now work.
import io.appium.java_client.AppiumBy;
import io.appium.java_client.ios.IOSDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class iOSAppSettings {
private IOSDriver driver;
@BeforeEach
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "app");
desiredCapabilities.setCapability("appium:automationName", "XCUITest");
desiredCapabilities.setCapability("appium:platformVersion", "16.3.1");
desiredCapabilities.setCapability("appium:bundleId", "com.apple.Preferences");
desiredCapabilities.setCapability("appium:noReset", true);
desiredCapabilities.setCapability("appium:udid", "YourUDID");
driver = new IOSDriver(new URL("http://0.0.0.0:4723/"), desiredCapabilities);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@Test
public void TestIOSSettings() throws InterruptedException {
driver.findElement(AppiumBy.xpath("//XCUIElementTypeStaticText[@name='Bluetooth']")).click();
driver.findElement(AppiumBy.xpath("//XCUIElementTypeButton[@name='Settings']")).click();
}
@AfterEach
public void tearDown() {
driver.quit();
}
}
Incidentally, you can find the source code required for launching in the Session Information
tab of Appium Inspector's 2023.5.2
.
You may want to use that source code after you have the app ready to launch in Appium Inspector.
Note, however, that the latest version of the library, java_client, does not support MobileElement
.
Conclusion
I described how to run iOS app tests with Appium 2.0. Although I did not touch on features unique to Appium 2.0, I hope you have found it easy to migrate from Appium 1.x to Appium 2.0.
Appium 1.x will not be updated much in the future, so it is recommended to migrate to Appium 2.0 when you have time!