I would love to do something like this:
div = find '#some-div'
copy_to_clipboard(div)
input = find '#my-input'
paste_from_clipboard(input)
I do not want to simulate this with send_keys and using Ctrl+C and Ctrl+V; I want this to work cross-browser (especially on mobile).
Does this API exist?
The most simple way I've found:
element.send_keys [:control, 'c']
element.send_keys [:control, 'v']
There is no Capybara copy/paste API - If all you want to do is copy the visible text into an input then you could do
div_text = find('#some-div').text()
find('#my-input').set(div_text)
If that's not correct for what you want, then you could use #execute_script to create a selection range like
var range = document.createRange();
range.setStart( <start node>, <start node character offset> );
range.setEnd( <end node>, <end node character offset> );
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
then find your target element and set it's value to window.getSelection().toString(). Note that's not really emulating what a user would do, so if you are actually using this for testing an app I would still recommend using the ctrl/cmd-c/v after setting the selection range for browsers that support it since it emulates user behavior better.
It's an old one, however you don't need to use capybara however the workaround would to use this incredibly simple gem:
https://github.com/janlelis/clipboard
There is no API to do it.
You can get element from one browser
div = page.find('#some-div')
Then you can pass it to another browser
fill_in '#some-other-div' with => div
You can read more about capybara here: https://github.com/jnicklas/capybara
Related
Is there any other way to find out the count of webelements present in a webpage without using "findElements()" method?
This Question is asked in Interview. So I would like to know whether it is possible to get the count of Webelements without using findElements().
Update to #mate and #custom answer,
using xpath, to extract the number.
driver.executeScript(() => $x('count(//div)'));
if there is 25 div elements, will return 25 as a number.
Here's one way:
Go to DevTools
Enter $x('//*') to console
You will get the number of HTML elements
You could use WebDriver.prototype.executeScript which lets you execute some JavaScript code. That code can either be a function or a string:
driver.executeScript(() => document.querySelectorAll('*').length);
PS: I'm using the JS flavour of selenium-webdriver.
I need to instantly fill a textarea with a very long string for testing purposes.
set/send_keys simulates typing and is too slow for Sauce Labs causing time outs.
Is there a way to instantly fill a textarea in Capybara?
The only way to instantly do it would be using execute_script to set the value via JS
element = find('textarea') # however you locate the element
execute_script('arguments[0].value = arguments[1]', element, text_to_set)
Note: this won't trigger all the events a user would generate when inputting into the textarea
TL;DR: Use Javascript/JQuery (.val() function) to set the field's value via the .execute_script()/.evaluate_script() function. It will automatically send the full string. You have more details bellow.
Example:
def execute_script(script)
browser.execute(function() {
$('<yourSelectorHere>').val("blablabla");
})
nil
end
Selenium team decided a LOOOONG way back to make it work this way, because it will actually simulate the real way a user would fill that input/textarea/field/etc.
Note: I wrote the command in WebdriverIO, but now have updated to Capybara as well.
Indeed, using the .setValue()/.set(), or the .keys()/.send_keys() methods will issue a POST action on the target element (presumably an <input>) in the form of an array of characters. See example:
Command: browser.setValue('input[connectqa-input="rename-device"]','stackoverflowstackoverflowstack');
Output:
> browser.setValue('input[connectqa-input="rename-device"]','stackoverflowstackoverflowstack')
{ state: 'pending' }
> [21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/elements"
[21:52:25] DATA {"using":"css selector","value":"input[connectqa-input=\"rename-device\"]"}
[21:52:25] RESULT [{"ELEMENT":"6"}]
[21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/element/6/clear"
[21:52:25] DATA {}
[21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/element/6/value"
[21:52:25] DATA {"value":["s","t","a","c","k","o","v","e","r","f","(21 more items)"],"text":"stackoverflowstackoverflowstack"}
One quick and easy way to escape this is to go ahead and abuse the .val() JQuery function via the .execute()/.executeScript() methods:
Command: browser.execute(function() { $('input[connectqa-input="rename-device"]').val("dwadawdawdawdawdawdwadawawdadawdawdaw"); }) (WebdriverIO)
For Capybara syntax, see this question. It covers both .execute_script() & .evaluate_script(). (I don't want to mooch-off their views). Also you should document on the methods before-hand here.
Output:
> [21:59:38] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/execute"
[21:59:38] DATA {"script":"return (function () { $('input[connectqa-input=\"rename-device\"]').val(\"dwadawdawdawdawdawdwadawawdadawdawdaw\"); }).apply(null, arguments)","args":[]}
Hope it helped!
When we use selenium command at that time command not find and attribute not get? See below command.
<table>
<tr><td>open</td><td>http://www.wikipedia.org/</td><td></td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>assertAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>language</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>search1</td></tr>
<tr><td>assertAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
</table>
I am using Selenium IDE 2.5.0 in Mozilla Firefox and Ubuntu.
Xpath //div[2]#class central-featured is invalid. Try changing it to //div[#class='central-featured']/#class if you mean to select a class.
You could also use assertElementPresent function instead of selecting attribute, if the whole point is to check that element exists, i.e.:
<tr><td>assertElementPresent</td><td>xpath=//div[#class='central-featured']</td><td></td></tr>
Much simpler that way.
Use xPaths in this case.
Use google chrome's built in developer tool for this
Place your cursor on the element
Press Ctrl+Shift+C
Click the Element
That clicked Element's code is highlighted in the short window on the bottom
Right-Click on highlighted code
Select Copy > Copy XPath
Here it is you have copied the xPath for that specific element. This is shown in the image:
Click to see how to copy xPath
The Xpath you have used in Invalid.
You can use xpath as follows and through this you can fins xpath of any object - just need to study the concept:
Here as we can see we want to search Google Search just by writing its xpath in console
So to find the Google Search button we have to write xpath like this
//span[#id='gbqfsa']
Once we hit enter it would bring
[ gbqfsa">Google Search ],
It shows that xpath for Google Search Button is correctly written
Now suppose we want to search Google Search button if we are just familiar that id attributes start with gbqfs
then we have to use function starts-with like this
//span[starts-with(#id,'gbqfs')]
and once when we hit enter on console it would reflect two button one is Google Searchand Second one is I’m Feeling Lucky
[
gbqfsa">Google Search
,
I'm Feeling Lucky
]
So to find out the Google Search uniquely we need to complete id attribute to gbqfsa
“//span[starts-with(#id,'gbqfsa')]
and hit to enter and now it would reflect only
[
Google Search
],
It proves that we have written right xpath for Google Search
In the same fashion we can use Contains function to find the Google Search button like this
here I have taken fsa from gbqfsa
//span[contains(#id,'fsa')]
hit enter and hopefully it will return
[
Google Search
],
if there are multiple attributes then we can use:
//span[contains(#id,'fsa') and contains(#class, 'xyz')] hit enter and hopefully it will return
Information Source: Sumit Mittal Blog
You can use CssSelector as below
webDriver.findElements(By.cssSelector("div.central-featured")) // for more than 1 elements with same class
webDriver.findElement(By.cssSelector("div.central-featured")) // for 1 element
How can I check whether a given text string is present on the current page using Selenium?
The code is this:
def elem = driver.findElement(By.xpath("//*[contains(.,'search_text')]"));
if (elem == null) println("The text is not found on the page!");
If your searching the whole page for some text , then providing an xpath or selector to find an element is not necessary. The following code might help..
Assert.assertEquals(driver.getPageSource().contains("text_to_search"), true);
For some reason, certain elements don't seem to respond to the "generic" search listed in the other answer. At least not in Selenium2library under Robot Framework which is where I needed this incantation to find the particular element:
xpath=//script[contains(#src, 'super-sekret-url.example.com')]
A simpler (but probably less efficient) alternative to XPaths is to just get all the visible text in the page body like so:
def pageText = browser.findElement(By.tagName("body")).getText();
Then if you're using JUnit or something, you can use an assertion to check that the string you are searching for is contained in it.
assertThat("Text not found on page", pageText, containsString(searchText));
Using an XPath is perhaps more efficient, but this way is simpler to understand for those unfamiliar with it. Also, an AssertionError generated by assertThat will include the text that does exist on the page, which may be desirable for debugging as anybody looking at the logs can clearly see what text is on the page if what we are looking for isn't.
In a functional test in symfony, sfTestBrowser provides methods
click() "Simulates a click on a link or button."
select() "Simulates selecting a checkbox or radiobutton."
and unselect().
But I have not found a way to simulate making a selection from a <select> element.
Does anybody know a way to do this?
This has troubled me too. I'm assuming you just want to set the value for form submission? If you know the value, you can of course just do
$browser->click('Save', array(
'theselectfield' => 'desired_value'
));
But usually I don't know the value I want posted, because it's from a database-driven select box. So my solution is
$theOption = $browser->getResponseDomCssSelector()->matchAll('select[name*=name_of_select_field] option:contains(TheOptionTextYouWant)')->getNode();
$browser->setField('theselectfield', $theOption->getAttribute('value'));
... or use $browser->click() instead ...
Frustrating because you have to break out of the $browser call chain, in order to use getResponseDomCssSelector(), but I haven't found an easier way.