Make UI Testing possible with TYGenerator

Aytuğ
Trendyol Tech
Published in
3 min readNov 9, 2021

--

UI testing has become an important part of our testing phase. It gives great advantages in terms of both automation and speed. However, to obtain these advantages adjusting unique accessibility identifiers for each element and preparing page object codes for them is one of the repetitive and hardest parts of this job. This really wastes a lot of time make UI testable for each element of each page.

In this article, I will talk about TYGenerator makes our views UI testable and automatically generate the necessary page codes for the Trendyol iOS team.. But first, I am sharing the article explaining how we write UI test. It would be great if you read this first.

We have been generating codes to make the UI testable. How? Let me introduce you the XCode Source Editor Extension.

You build extensions to the source editor in Xcode using XcodeKit. Source editor extensions can read and modify the contents of a source file, as well as read and modify the current text selection within the editor.

Using the Xcode Source Editor Extension, we read the codes of our view controller, view, cell classes. By parsing these codes, we determine the class name, what kind of class it is(cell or view controller) and the UI elements used in class. Thanks to the data we obtained after the read codes, we make the elements UI testable and at the same time automatically generate the UI test class of class we are parsing.

Making View Controller UI Testable

For View Controller Class

Making Cell UI Testable

For View Cell Class

Let’s take a look at the implementation of this extensions. As seen in the gifs, we have 4 extensions.

  • Generate Accessibility Identifiers
  • Generate All ( this generate accessibility identifiers and UI testable page class)
  • Generate Mark
  • Sort Import

Today we have 4 extension. But tomorrow it could be 10 or 20. We used chain of responsibility pattern to manage this. You can read about chain of responsibility pattern by Ahmet Keskin.

Thanks to GenerateEngine, the selected extension is determined with isSatisfied and necessary action is taken with execute.

Let’s take a look at the UITestablePageGenerator, which is the generable item we’re really interested in. This is the class that does all our UI test preparations.

Of course, I will not explain each method. Actual event happens “execute()” method. Here we do 3 main operations.

conformAccessibilityIdentifiableToView

This method import AccessibilityKit and conform AccessibilityIdentifiable to the view interface.

conformUITestablePageToView

This method makes UI elements testable by writing an extension conforming the UITestablePage.

generateUIElementClass

This method generates the actual class that checks each element on the page.

Each method is linked to each other. That’s why we make sure that the previous operation was successful by using an optional chain. If you want to take a look, you can find extension’s source codes here.

Conclusion

As you can see, we read and modified the codes by adapting them to own UI test structure. Considering our classes are bigger and we have 100s of view classes, this approach has saved us invaluable time. We always continue to look for new ways and keep our aproach updated. Thank you very much for reading.

--

--