Changing the focus of the window on Capybara - ruby

Writing some tests I had to click on a link that leads to another tab then I needed to click on another link, that last step isn't working (Unable to find link "Example" (Capybara::ElementNotFound)).
Should I need to change focus to the new tab first? How do I do that?

Tabs are treated as new windows in Capybara, so you need to move to the new window using Capybaras window api. It will look something like
new_window = window_opened_by do
# here you are in the context of the original window but your actions are expected to open a new tab/window
click_link 'the link that opens the new tab'
end
within_window(new_window) do
# this is in the context of the newly opened tab/window
click_link 'the link inside the new tab'
end
# here you will be back in the context of the first window
new_window.close # if you want to close the tab that was opened

All I had to do was add switch_to_window(windows.last) before the action

Related

macOS Change extension dialog appears as a new window instead of another sheet

I have an app that lets the user input a file, process it, then lets the user save the file. I ran into a small problem with the save dialog. The file type that my app allows the user to save should only be txt files. But I do allows the user to use other types as well so I put true in allowsOtherFileTypes. Here is my code:
let savePanel = NSSavePanel()
savePanel.allowedFileTypes = ["txt"]
savePanel.allowsOtherFileTypes = true
savePanel.beginSheetModal(for: self.window!, completionHandler: { response in
// Save handlers here.
})
The problem is, if the user put another extension other than txt, my app suppose to pop up an alert asking for sure if the user wants to use that extension for the file, but the alert pop up as it's own window instead of a sheet:
What should I add to make this alert appear as a sheet over the save window instead of on a new window?
System: macOS Sierra, Xcode 8.2, Swift 3
Tom,
On macOS you can't have a sheet on top of a sheet. Since macOS is putting the first sheet (the save panel) on top of your window, the second dialog confirming the non standard file extension for your app can't be a sheet.
Hope that helps.

Adding a location bar shortcut for sending a new mail (via Gmail)?

Is there a way that I can type a command in location bar and the browser will open the "new mail interface" in the current tab.
So, we don't have to go to gmail and click on compose button?
This works...
https://mail.google.com/mail/?view=cm&fs=1
You can actually start filling in the email too...
https://mail.google.com/mail/?view=cm&fs=1&to=ToEmailAddress#here.com&su=SubjectURLEscaped&body=BodyTextURLEscaped
Cheers!

Uploading files using ruby / Selenium

I am writing a script where I click on a button (Select Photo) which opens up a file upload (explorer) box. How am I to set my file name?
This is the code I'm using to click on the Select Photo button (ruby)
driver.find_element(:id, "fileUploadButton").click
I've seen some posts that says I do not need to click on the button but to send the path to the file/image I want to upload. So how would I upload a file on c:\temp\mypicture.jpg?
Here's my full and simple code.
driver.navigate.to "http://blah blah" #the real site is an internal site
driver.find_element(:id, "claimGiftButtonDesktop").click
sleep 5
driver.find_element(:id, "fileUploadButton").click
After clicking the fileUploadButton, that's when the explorer window will show. If I manually click on Open or double click on it, then a loading modal shows and the image is shown on the website.
Here is an IDE recording which works. I'm just having problems translating this into ruby.
open /PromoSite
click id=claimGiftButtonDesktop
click id=fileUploadButton
type id=fileInputElem #Value C:\\temp\\file.jpg
click id=viewProductPreviewButton
I've also added a screen shot. I click the button and the File Upload shows up. This should be something easy, so I must not be focusing on the correct id.
As I have no your code, assume we are testing https://encodable.com/uploaddemo/
#driver.navigate.to "https://encodable.com/uploaddemo/"
element = #driver.find_element(:css, 'input[type=file]')
element.send_keys "/full/path/to/file.jpg"
#driver.find_element(:css, 'input[type=button]').click
So, you should send the full path to the input field and press the "submit" button
I know this is coming a year late but I just started writing a script in Selenium/Ruby and it took me some time to figure it out so wanted to post my solution (and it was as simple as 2 lines!):
*the first line inserts the file path WITHOUT clicking on the browse button, key is to separate directories using double back slashes \\
*the second line clicks on the Save/Upload button
driver.find_element(id: "Document_upload").send_keys("C:\\Users\\me\\Desktop\\my_file.txt")
driver.find_element(id: "save").click

How to open mutiple tabs within a browser using Capybara?

I need to open multiple tabs within a single browser and i need switch over all the tabs.
Give me your suggestions. Thanks in advance.
I'm not so strong in capybara. so, i'm giving solution to switch over between tabs using selenium.
For a instance you clicks a button in a web page and it will open a new page.
The new page may open in a new tab in the same browser window or in a new browser window. That is not controlled by selenium. It will control by the browser which you using.
For a instance take firefox, goto tools->ptions-> tabs-> open new windows in a new tab instead check the option. For example, if you clicks a button it will opens a page only in the new tab of a same browser window. For a sake if you unchecked mean it will open a page in the new browser window. Likewise every browser has its own settings.
Try this code:
new Actions(driver)
.sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL)
.sendKeys(driver.findElement(By.tagName("html")), Keys.NUMPAD2)
.build().perform();
In above Keys.NUMPAD2 refers that you are gonna move to the second tab in the session.
You can move to Third, Fourth, etc... by giving NUMPAD3, NUMPAD4, etc... respectively. Here i am using windows OS, if you are in some other OS use their shortcuts.
I hope this will help you.
You can use this exact function in Ruby:
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)

Problems running Ruby program with Aptana Studio

The request below is based upon an earlier version posted on the Aptana Tender site, where it didn't receive a response - presumably because the Aptana Tender site has now been deprecated in favour of StackOverflow.
I'm using Eclipse 3.6.2.r362_v20110210 with the Aptana Studio plugin version 3.0.3.201107141410 (i.e. all fully up-to-date, I believe) on Mac OS X 10.6.8 Snow Leopard on a Macbook.
I created a Ruby project, and then created a single file inside it called "item.rb", into which I wrote some Ruby code.
The file is very short: it defines an "Item" class with an initialisation method that asks the user for a value (using a "puts" followed by a "gets.chomp") and a get method that returns the value, and it also contains two more lines: one to create a new Item, and one to call that method on the newly-created Item.
When I run "ruby item.rb" from the Terminal, it works as expected: it asks the user for a value, and returns that value. No errors/warnings.
Now, I don't want to have to use the Terminal every time I want to execute the file, but I'm having trouble getting it running any other way.
Here are the steps to reproduce:
In the main Eclipse menu, go to Window > Open Perspective > Other.
In the resulting dialogue box, click "Ruby" and click "OK".
In the Script Explorer view, click the triangle next to the name of the project containing the file I want to run, so that the project's contents are shown.
In the Script Explorer view, double-click on the name of the file I want to run (item.rb).
This causes item.rb to open in an editor view, and gives that view focus (i.e. the editor view containing item.rb has a blue rather than a grey border, and there is an active cursor inside the view).
Click the "Run" button on the Eclipse toolbar (the one that looks like a green "Play") button.
THE FIRST PROBLEM: a "Run as..." dialogue box pops up (I don't know why) asking me to "Select a way to run 'item.rb'". It gives me two options (again, I don't know why): "Android Application", "Ruby Application".
Expected behaviour: because item.rb is a file ending in ".rb" and containing only valid Ruby code, Eclipse/Aptana should, by default, run it as a Ruby application.
I click "Ruby Application", and then click "OK".
THE SECOND PROBLEM: the dialogue box disappears, but nothing else happens.
Expected behaviour: the Console view should get focus, and item.rb should be executed, with the results showing up in the Console view's textarea.
By this point, it would be reasonable to give up. But programmers are unreasonably persistent, right? So: click the Console tab.
The Console view's header says, "item.rb [Ruby Application] /usr/bin/ruby", and below this is a blank textarea containing only a blinking cursor.
Press the <Return/Enter> key on the Macbook's keyboard once or twice.
THE THIRD PROBLEM: the Console view's textarea suddenly updates with the output of item.rb, but weirdly, it doesn't stop at gets.chomp, it just acts as though the user has entered an empty response at that stage (i.e. as though the user has simply pressed the <Return/Enter> key on the keyboard), and ploughs on until the entire program has executed. The Console view's header then changes to "<terminated> item.rb [Ruby Application] /usr/bin/ruby".
Expected behaviour: item.rb should execute in the Console view just as it does in the Terminal.
Please can you help me troubleshoot these three problems?
Many thanks.
It sounds like you have an older version of Aptana, RDT, RadRails or DLTK Ruby installed, as there is no "Ruby" perspective in Aptana Studio 3.x.
You should be able to right click the file in the Project Explorer, App Explorer, or in the open editor for the file and choose Run As > Ruby Application.
Ideally you'd remove any old installation of ruby plugins to avoid some sort of conflict before doing so. It should then launch the file as expected.
The Enter/Return behavior you're talking about sounds like the program is not flushing STDERR/STDOUT and hitting Enter is proceeding to enter in the input (so, it's buffering the output you should see that forms the prompt, then you hit Enter/Return and it takes that as the input so it proceeds after the gets call). if things printed to STDOUT don't show, you can try and force auto-flushing by adding $stdout.sync = true to the top of your script. Studio 3.x should set that up silently for you on programs launched through it. If not, then you should file a bug: http://jira.appcelerator.org/secure/CreateIssue!default.jspa

Resources