Writing an Appium Test in Kotlin
Kotlin is a modern programming speech that concenter on conciseness, clarity, and code safety. Google officially adopted Kotlin by adding support into Android Studio in 2017 and since then has declare it as the preferred language for Android developers at Google I/O 2019. Perhaps your Android app team has incrementally added Kotlin code to your project or get elected to start new Android projects in Kotlin displace frontward. Having the app code and UI automation code in the same language helps engineering and QA squad level set on the critical user journeys being tested. I ’ d like to share the basics of get started with in Kotlin. I ’ ll be habituate the Google News Android app as the target for this example and examination on a real Google Pixel 3 XL from the. Kotlin is fully compatible with Java so we can fortunately leverage the Appium Java client and JUnit libraries in addition to writing our code in the IntelliJ IDE. IntelliJ has a complimentary community version telephone IntelliJ IDEA CE, which can be downloaded from theirwebsiteor via your package handler. In IntelliJ IDE, navigate toFile & gt; New Project. Select Gradle and under Extra Libraries and Frameworks select Kotlin/JVM. In the Project file scene on the left, open thebuild.gradlefile and add the following dependencies: testCompile radical: & # x27; junit & # x27;, gens: & # x27; junit & # x27;, version: & # x27; 4.13 & # x27; We will add the Appium examination in thesrc/test/kotlinproject directory. Right chatter theKotlinsubdirectory and selectNew & gt; Kotlin File/Class. Create three new family: We will structure the project in these three course to make the organization clear. TheProjectCapabilitiesclass will regress the Desired Capabilities,TestBasewill be a base class for our test for scaffold the test setup and teardown, and finally theGoogleNewsAndroidTestclass will inherit from ourTestBase, use the Desired Capabilities, and contain our test logic. Let ’ s get started with the ProjectCapabilities class. This grade will have a method to return the Desired Capabilities. significance org.openqa.selenium.remote.DesiredCapabilities Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. Functions and Methods in Kotlin are declared with the “ fun ” keyword. You can likewise bespeak the function return type with a colon postdate by the type. We can make this a static category method by declaring it in a “ companion object. ” You can create read-only variables with “ val ” and variable that can be reassigned with “ var ”. In this case I am targeting a real Pixel 3 XL device in the HeadSpin platform. From the HeadSpin Web UI you can get the minimum Desired Capabilities for the device by chatter on the upright dots menu and take Automation Configuration. For the appPackage and appActivity I am limit the associated values for the Google News Android app. Next, let ’ s work on our TestBase grade. By nonpayment, classes in Kotlin are implicitly final and can ’ t be inherited. To get a class inheritable, we ’ ll mark it with the “ exposed ” keyword. In this form we will make property variables for the: driver, capability, HeadSpin API Token, and WebDriver URL. We ’ ll provide visibility modifiers to our properties, in this case: We ’ ll also label our Desired Capabilities variable as “ exposed ” so it can be overridden from our GoogleNewsAndroidTest grade which inherits from the TestBase class. In addition, you can indicate whether a varying can be null. The ability to declare this upfront is a outstanding lineament of the Kotlin language because it protects against unexpected NullPointerExceptions from our code. In this instance we can bespeak that the driver and caps can be null with “? ” after the type and set them to null. Kotlin also back string interpolation by using the “ $ ” fibre to interpolate a variable or “ $ {} ” to interpolate an verbalism. We ’ ll use this convenient language characteristic to pass our HeadSpin API Token to our WebDriver URL string. Our TestBase will also feature two JUnit annotations: @ Before for our setup function and @ After for our teardown to execute a driver quit. Our setup function will be straightforward and it will instantiate the driver object. In our teardown role we ’ ll check to see if the driver object is not null and if so, resign our session. Otherwise, we will throw an Exception. Kotlin cater a convenience operator for control for a null condition with “?: ”. This is called the Elvis Operator because if you turn your head sideways the question mark character looks like a swoosh of hair and the colon look like eyes. Do you see the resemblance now? Haha either way, the source code for the class can be constitute below and let ’ s carry on. import io.appium.java_client.MobileElement Our last category, GoogleNewsAndroidTest will have our test logic. Here we have one test role footnote with the JUnit Test notation. Our test will be simple and straight, we will tap on the Headlines tab button and then scroll down from the list of tidings articles. Since we will be do driver action we ’ ll need to first ensure the driver is not void. Kotlin provides a way to perform an operation only if the value is not void using the “? ” (the safe call operator) together with “ let. ” import io.appium.java_client.MobileBy Our work hither is done! We can now execute our test and vigil it in action. Congratulations you now feature a taste of what it ’ s like to indite an Appium test in Kotlin! If this article has helped farther your knowledge feel complimentary to Like or Share it. You can download the total project rootage codificationhere. Ans: Yes. Kotlin supports the growth of Android apps. Many covering already use Kotlin, such as Basecamp and Pinterest. Ans: Ans:With the help of an Appium inspector, a GUI-based creature, testers can place ingredient on iOS apps. This GUI-based instrument is similar to that of selenium IDE. Ans:Kotlin supports all major Java IDEs, include IntelliJ IDEA, Android Studio, and Eclipse. Lead, Content Marketing, HeadSpin Inc. Piali is a active and results-driven Content Marketing Specialist with 8+ days of experience in crafting engaging narrative and marketing collateral across diverse industriousness. She excels in collaborating with cross-functional teams to germinate innovative content strategy and deliver compelling, authentic, and impactful content that resonates with target audiences and enhances marque authenticity. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts..png)



Writing an Appium Test in Kotlin
AI-Powered Key Takeaways
Getting started

compile group: & # x27; io.appium & # x27;, gens: & # x27; java-client & # x27;, version: & # x27; 7.3.0 & # x27;

1. ProjectCapabilities Class
class ProjectCapabilities {
companion object {
fun AndroidBaseCapabilities (): DesiredCapabilities {
val cap = DesiredCapabilities ()
caps.setCapability (& quot; autoAcceptAlerts & quot;, true)
caps.setCapability (& quot; platformName & quot;, & quot; Android & quot;)
caps.setCapability (& quot; automationName & quot;, & quot; UiAutomator2 & quot;)
caps.setCapability (& quot; deviceName & quot;, & quot; 8ABY0H6YG & quot;)
caps.setCapability (& quot; udid & quot;, & quot; 8ABY0H6YG & quot;)
caps.setCapability (& quot; appPackage & quot;, & quot; com.google.android.apps.magazines & quot;)
caps.setCapability (& quot; appActivity & quot;, & quot; com.google.apps.dots.android.app.activity.CurrentsStartActivity & quot;)
return caps
}
}
}Check out:
Execute automated Android app testing on real devices worldwide to build high-performing Android apps..
2. TestBase Class
Check out:

importee io.appium.java_client.android.AndroidDriver
import org.junit.After
import org.junit.Before
importation org.openqa.selenium.remote.DesiredCapabilities
import java.lang.Exception
import java.net.URL
open stratum TestBase {
protected var driver: AndroidDriver & lt; MobileElement & gt;? = null
protect exposed var detonator: DesiredCapabilities? = cypher
private val headSpinAPIToken: String = & quot; your-api-token-here & quot;
private val webDriverURL: URL = URL (& quot; https: //appium-dev.headspin.io/v0/ $ headSpinAPIToken/wd/hub & quot;)
@Before
fun setUp () {
this.driver = AndroidDriver (webDriverURL, cap)
}
@After
fun tearDown () {
this.driver? .quit ()?: throw Exception (& quot; Driver instance was unable to quit. & quot;)
}
}Also check:
3. GoogleNewsAndroidTest Class
importee org.junit.Test
import org.openqa.selenium.interactions.Interaction
importation org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
significance org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.support.ui.ExpectedConditions
significance org.openqa.selenium.support.ui.WebDriverWait
importation java.time.Duration
class GoogleNewsAndroidTest: TestBase () {
override var caps: DesiredCapabilities? = ProjectCapabilities.AndroidBaseCapabilities ()
private val headlinesTabButton: String = & quot; com.google.android.apps.magazines: id/tab_headlines & quot;
@Test
fun headlinesScrollTest () {
// Set an denotative wait of 10 seconds
val wait = WebDriverWait (driver? .let {it}, 10)
// Tap on the Headlines tab button
wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.id (headlinesTabButton))) .click ()
// Scroll Down
val finger: PointerInput = PointerInput (PointerInput.Kind.TOUCH, & quot; finger & quot;)
val moveToStart: Interaction = finger.createPointerMove (Duration.ZERO, PointerInput.Origin.viewport (), 726, 2452)
val pressDown: Interaction = finger.createPointerDown (PointerInput.MouseButton.LEFT.asArg ());
val moveToEnd: Interaction = finger.createPointerMove (Duration.ofMillis (1000), PointerInput.Origin.viewport (), 726, 660)
val pressUp: Interaction = finger.createPointerUp (PointerInput.MouseButton.LEFT.asArg ())
val swipe = Sequence (finger, 0)
swipe.addAction (moveToStart)
swipe.addAction (pressDown)
swipe.addAction (moveToEnd)
swipe.addAction (pressUp)
driver? .let {it.perform (arrayListOf (swipe))}
}
}Don ’ t Rely on iOS Emulators & amp; Android Simulators..
Conclusion

FAQs
1. Is Kotlin expend for Android app ontogenesis?
2. What are the important features of Kotlin?
3. How can testers inspect elements on the iOS apps?
4. Which IDEs support Kotlin?
Piali Mazumdar
Writing an Appium Test in Kotlin
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence practical guide for forward-looking users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence hard-nosed guidebook for forward-looking user (Part 4)
Discover how HeadSpin can empower your business with superior testing capabilities







Discover how HeadSpin can empower your business with superior quiz capabilities
Discover how HeadSpin can empower your business with superior testing capabilities
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)











