Avoid Wasting Time Using Ruby's binding.pry Feature in Appium Tests
As the complexity of web application testing increases, software developers and test engineers must write and debug test cases more effectively. Selenium is a powerful tool for testing web applications, and Ruby is a useful language choice in this process. However, errors encountered when writing and executing complex test cases can sometimes be difficult to resolve.
This is where pry, Ruby's powerful debugging tool, comes into play. binding.pry allows you to add a breakpoint at a specific point in the code and allows you to examine your code interactively at this point. In this way, you can follow your code step by step, observe the values of variables, and better understand how your application works to understand why your tests fail.
In this article, we will discuss the use of binding.pry in Ruby tests written with Selenium. We will explore how binding.pry works, how you can use it effectively in your tests, and how you can debug your tests more efficiently with this tool. This guide, which will be useful for both beginners and experienced developers, will improve your debugging processes and help you write more reliable test cases.
Here I would like to share with you the gems that contain the gems required for appium. After creating the "Gemfile" in our project, let's add the following gems.
source 'https://rubygems.org'
gem 'rspec'
gem 'appium_lib'
gem 'selenium-webdriver'
gem 'ruby-lsp'
gem 'rubocop'
gem 'pry'
gem 'pry-byebug', require: false, group: :development
gem 'solargraph', require: false, group: :development
gem 'byebug'
Then, to install the relevant gems;
bundle.install
Once the installation is complete, we are ready. Let's place the binding.pry code in the line of code we want to debug .
it 'plays a game of TicTacToe' do
menu_page = Menu_page.new(driver)
sleep 0.1
menu_page.ucluktahtayatikla
sleep 0.1
menu_page.single_player_tikla
x = 370
y = 1400
menu_page.carpiyatiklat(x, y)
binding.pry//our debug point
end
When I run my test and get to the binding.pry point, it looks like this in the terminal.
The situation on my virtual device is as follows;
Now I can connect to the appium inspector and inspect the dom. I am connecting via attach session.
领英推荐
I have done the necessary review in Dom and I will select a locate and click on start game.
I add the relevant code under binding.pry
driver.find_element(:accessibility_id, 'Start game').click
it 'plays a game of TicTacToe' do
menu_page = Menu_page.new(driver)
sleep 0.1
menu_page.ucluktahtayatikla
sleep 0.1
menu_page.single_player_tikla
contexts = driver.available_contexts
puts contexts
x = 370
y = 1400
menu_page.carpiyatiklat(x, y)
binding.pry
driver.find_element(:accessibility_id, 'Start game').click
end
I select the code with the mouse and right click and run it with "Run Selected Text In Active Terminal"
I observe my code running on my virtual device.
If you want, you can continue working in the current session by doing trial and error. You can focus on your tests without being distracted by gaining great efficiency without having to restart the Appium inspector every time or re-running the tests over and over again.
I hope it helped.