I use a javascript library - clipboardjs - for copying input field values to system clipboard.
In my application.js:
function addressClipboard() {
new Clipboard('.address-copy', {
text: function(trigger) {
var addressString = "";
addressString += $('#addresses_attributes_0_street').val() + "\n" +
$('#addresses_attributes_0_city').val() + "\n";
addressString = addressString.trim();
return addressString;
}
})
};
I like to test the functionality with rspec & capybara.
address_spec.rb:
it "checks the copied values when clicking the copy-to-clipboard link", :js do
new_address = build(:address)
visit new_address_path
fill_in "person_addresses_attributes_0_street", with: new_address.street
fill_in "person_addresses_attributes_0_city", with: new_address.city
click_link(I18n.t('helpers.copy_to_clipboard'))
# Pseudocode:
expect(page.execute_script("addressClipboard()")).to eq([new.address.street,new.address.city].join)
Is there a way to access the javascript variable addressString and compare it to the address attributes (eg. new_address.street) within rspec?