Unable to send the TAB key during a Selenium integration test - ruby-on-rails

As part of a Selenium integration test, I wrote the two following lines in Ruby to simulate entering an e-mail address into a text field and "moving the focus away":
#driver.find_element(:id, "user_email").send_keys "user#example.com"
#driver.find_element(:id, "some_other_element").click
The second line is not generic enough, because some_other_element might be unknown or non-existent. Thus, I wanted to replace the action of clicking another element with a TAB keystroke:
#driver.find_element(:id, "user_email").send_keys :tab
However, this does not seem to work, the element user_email does not lose its focus as expected. Also replacing :tab with "\xEE\x80\x84" does not help. Does anyone know what could be wrong here? How can I move the focus away of an element without simulating a click somewhere else?
Thanks for any help,
Dominik

I don't know how to do it without simulating a click, but clicking on <body> worked for me. This solution may be generic enough.

I think this should works well. Give it a try.
find('.myselector_name').native.send_keys(:tab)
Reference to answer is How do I simulate hitting enter in an input field with Capybara and ChromeDriver?

Related

Not able to click on POST button with xpath code

driver.findElement(By.xpath("//*[#id=\"ember558\"]")).click();
I am doing testing for linkedin post. I entered this code for POST button but that does not work. Can you please suggest about this?
It seems that #id attribute value is dynamic so, it will not be able to interact with the Element. You can try with dynamic X-path techniques. Generally, a set of data keeps on changing and rest data remains same.
e.g. <button "id":"emns1233">
here starting part of id will remain same every time but last part that is 1233 will change (may be each time). So, one can try to write X-path as
driver.findElement(By.xpath("//button[starts-with(#id,'emn')])).
Hope it helps. Thanks

Capybara: Finding a second checkbox which is identical to the first

I am writing tests for view files. I have a page with two checkboxes which allows the user to Select All items in two different lists. However, the checkboxes are part of a partial so are identical. I have managed to check the first one using:
first(:checkbox, "Select all").click
But I am unable to check the second. I have tried replacing first with last and with second but to no avail.
I think I may need to use the find selector but am struggling with that also. Any help much appreciated.
Thanks
UPDATE
It appears that this in fact another issue. I think that any checkbox that impacts on other checkboxes does not work as expected when "checked" by Capybara. So the current answers below work in the sense that they do check the checkbox, but the expected behaviour does not occur i.e. the checkboxes linked to them do not get "checked". I may be wrong though.
try this
# find the second checkbox
find('input[type="checkbox"]:nth-child(2)').click
You can use find all
all('input[type="checkbox"]', :text => 'Select all')[1].click
Whoops. Turns out it was a JS issue and not Capybara macthers! Thanks for the help though!

How to fill tinymce-rails editor with capybara and selenium?

I have trouble with using capybara to test tinymce form. I'm using tinymce-rails and have 7 editors in my form. Also I'm using asciimath plugin with tinymce.
Everything works fine, but I'm unable to write tests to fill in tinymce editor.
Here is how my step definition code looks like, very similar to what is described here:
within_frame("content_ifr") do
editor = page.find_by_id('tinymce')
editor.native.send_keys 'test'
end
The problem is when I run the following:
editor.native.clear # works, clear the editor area, I'm testing this with pry
editor.native.send_keys :tab # works, moves focus to next input
editor.native.send_keys 'test' # returns "", nothing happens, nothing in editor
So clear and send_keys :tab work as expected. But I can't send any string. send_keys function is always returning empty string, and nothing happens when I do test using pry.
What is going wrong here? and how can I debug / investigate the problem?
Thanks.
I know that this is an old question but I just found it while trying to solve this issue as well.
Although the original question said that he has 7 tinymce's on the same page I think that my solution might work for him too but I do know it will work if there is one tinymce as was my case.
In my request spec I used this:
page.execute_script('$(tinymce.editors[0].setContent("my content here"))')
The page.execute_script with tell it to run the jQuery function. It then finds the first tincymce editor and sets the content.
Worked like a charm for me. I think if there are more than one tinymce it can be called by its position.
Switching to chrome as described here solved my problem.
Obviously the problem is related with a bug in firefox driver.
Still i think it is a valid question for firefox.
Try to switch to an iframe that contains tinymce textarea input, and than send_keys:
# +session+ is an instance of Capybara::Session class
browser = session.driver.browser
browser.switch_to.frame(iframe_id)
editor.native.send_keys(text)
browser.switch_to.default_content
I had the same issue. After a day fighting, my tests finally passed.
The code that I am using is:
within_frame("producto_condiciones_ifr") do
editor = page.find_by_id('tinymce')
editor.native.send_keys 'filling text'
end
The first line is a method of capybara. The parameter passed is the ID of the iframe.
Line #2 is a must.
In line #3 goes the text that you wish to place inside TinyMCE
just came across this problem with RoR and rspec
I managed to solve by doing this:
within_frame { page.find_by_id("tinymce").set("new content here") }
the set method will replace any existing content by the new one
if you want to keep the current content and add things to it, use the send_keys method

Moodle - Quotes and Backslashes when submitting

I have a sinister problem in one Moodle 1.9.9+ server.
On the startup page (logged in or not), I have the Summary, and if I make a link with the HTML Editor, it makes correctly, but when I submit, the link becomes with some backslashes. See my examples above:
1) On the HTML Editor, I write a text: http://www.example.com
2) Choose the link button
3) In the field "URL" I repeated the URL: http://www.example.com
4) Click on OK button.
5) Click on Save Changes button.
And in the summary, the link is shown as: http://www.example.com
and when I click him the browser goes to http://mymoodledomain.com/\"http://www.example.com\"
If I reedit the same Summary and simply press Save Changes, the link becomes with more backslashes:
http://mymoodledomain.com/\"\\\"http://www.example.com\\\"\"
And again:
http://mymoodledomain.com/\"\\\"\\\\\\\"http://www.example.com\\\\\\\"\\\"\"
I don't know what are happening with my Moodle, or my PHP.
I've tried to disable the magic_quotes_gpc, and them defined the constant MOODLE_SANE_INPUT with some value, but works parcially, because the same problem occurs too with Front Page Settings, and disabling the magic_quotes and enabling the constant, for this case not works. If I've a title with quotes, on each submit more backslashes are saved, for example:
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"example\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
Please guys, I need to solve this problem more quickly is possible.
Any sugestions?
Tks for all.
I've encountered the problem and solved it. Was on PostgreSQL.
The 9.1 version becomes with 2 options enabled by default, and the 8.3 was off. These options are called "standard_conforming_strings" and "escape_string_warning" and are encountered on postgresql.conf.
When enabled, these options saves an escape for an escape char, that comes of PHP by the "magic_quotes_gpc" resource when submitting a form.
Tks for all!

How to click on the second link with the same text using Capybara in Rails 3?

In a view file I have:
= link_to 'View', post
= link_to 'View', comment
In a spec file (I'm using Capybara):
click_on 'View'
It clicks on the first link, but I want it to click on the second one. How can I do it?
You could try to find all entries and deal with an array:
page.all('a')[1].click
Would help to have a class or use within to scope your search ;)
There's probably a few ways but I usually scope something like this.
within(".comment") do
click_on("View")
end
There's quite possibly/probably alternatives as well. I usually do my acceptance testing from cucumber, so my steps typically look like
When I follow "View" within the comment element
Where I have a step that translates within the comment element to a scoped call to the step itself (which I think is built into the latest capybara web_steps)
The worst thing about "the second" link is that it can become the third or the first or even the twenty fifth someday. So, scoping with a within block is the best way. Example:
within(".comment") do
click_on("View")
end
But if it is difficult to specify the link with a within scope (which sometimes it really is), I guess the way to click the second link with a certain text is:
find(:xpath, "(//a[text()='View'])[2]").click
In later versions of capybara (2.0.2, for example) both click_on 'View' and click_link 'View' will raise an ambiguous match error:
Failure/Error: click_on 'View'
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link or button "View"
So this won't do even if you want to click the first link (or if any link would be ok, which is my case).
As far as I understand this is made to force people write more specific tests where particular links are clicked.
It definitely could be tricky to debug the code if you accidentally placed two or more links with identical text and try to see what is happening. It's good to rely on something that is unlikely to change and specifying a link with a within block is a nice way to do this.
There are many ways for solving this type of problems.
Do it like this
if(page.find("a")[:href] == "comment")
click_on("View")
or
page.find("a:eq(2)").click
Remember javascript indexing starts with 0 while In Capybara, indexing starts with 1. So use a:eq(2) here for second href.
For capybara 2 solution:
within(".comment") do
click_on("View")
end
would not help if you have a few .comment. So simple use: page.first(:link, "View").click
This works for me if you have several rows of identical classes and you want to find the second row. Like a previous author mentioned, capybara indexing starts at 1.
within all(".trip-row")[2] do
assert page.has_content?("content")
end
If you use capybara-ui you could define the widget, or reusable DOM reference, for each widget.
# define your widget. in this case,
# we're defining it in a role
widget :view_post, ['.post', text: 'View']
widget :view_comment, ['.comment', text: 'View']
# then click that widget in the test
role.click :view_post
role.click :view_comment

Resources