I am putting together my first batch of automated tests for a android app using Ruby and Appium. I am having problems getting a click on the toolbar, for example we have buttons on the start page which are located and clicked using this example -
$driver.find_element(:id, "com.domain.appname:id/starters").click if menu == "1"
Now this works fine but when I try to click the toolbar icon ->
$driver.find_element(:id, "com.domain.appname:id/toolbar").click
I get this error -
Selenium::WebDriver::Error::NoSuchElementError: An element could not be located on the page using the given search parameters.
The toolbar element is defined in toolbar.xml thus
<?xml version="1.0" encoding="utf-8"?>`
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"`
android:id="@+id/toolbar"`
android:layout_width="match_parent"`
android:layout_height="wrap_content"`
android:minHeight="?android:attr/actionBarSize"`
android:background="@color/rose"`
android:theme="@android:style/ThemeOverlay.Material.ActionBar"/>
Can anyone advise on what I am missing please
Thanks
You are not giving the id properly. So Let's do something. Write the following which will print all the ids on the console which includes the text toolbar
then you pick up one by one and use in your code, one of them will click the correct element. Most probably there must be only one If the toolbar is only one.
require 'watir'
b=Watir::Browser.new
b.goto 'Your URL Here'
b.elements.each do |element|
p element.id[/.*toolbar.*/] unless element.id[/.*toolbar.*/].nil?
end
The above code will print all the ids which has the toolbar
text in it and then copy and paste those ids one after another in your program and one will work for sure.
Remember, Since you used Selenium code, even if there is a little late for the element existence, it would throw the error. So use WATIR code!
And also you can locate the element using partial text, This is another way is to use the regular expression as mentioned below
b.element(id: /toolbar/).click
But if there are more than two elements which have the text toolbar
, the first one will be clicked, If you want to move 2 or 3, then
b.elements(id: /toolbar/)[2].click