Tips for UI/Scenario Tests: Recording screens and getting view hierarchy

When we run UI related tests, debugging failing test is one of the most difficult and need a bunch of time task. To make it easier, we usually take screenshot, capture videos and make error report helpful.

Taking screenshot is very famous, and I skip it.

Recording

Android

Android provide screenrecord command to record a video for Android 4.4+.
I’ve implemented a bit helpful library, named droid_adbs

In the library, you can record video like the following. record.start call the screenrecord command in forked process. So, you can also start recording in setup and finish it in teardown.

require 'test_helper'

class DroidAdbsCommonsRecordTest < Minitest::Test
  def test_record
    record = ::DroidAdbs::ScreenRecord::Recording.new
    record.start

    sleep 10

    record.stop

    # Should wait to finish exporting recorded file
    sleep 1

    record.pull
    assert File.exist? "./droid_adbs_video.mp4"

    record.delete
    File.delete "./droid_adbs_video.mp4"
  end
end

iOS

For Simulator, we can capture video easily with the following method.

xcrun simctl io booted recordVideo capture.mov

Tips

More or less, recording video increases devices/machines load. So, I recommend recording screens only you need. For example, once run your test suite and retry failed tests with enable recording the screen then.

Capture view hierarchy when test failed

With ruby_lib and Appium x RSpec

RSpec.configure do |c|
  c.after(:example, type: :feature) do |example|
    if example.exception
      # capture screen
      view_hierarchy = source # source is getting view hierarchy command
      # Save view_hierarchy in particular files.
    end
  end
end

Espresso

  • We can get hint to finding elements.

EarlGrey

  • We can see view hierarchy by default.

XCUITest

  • I’m not sure we can get such information from error result.

Conclusion

Helpful error results decrease a bunch of time to debug and detect the cause.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.