May you help me in this matter, pls?
I would like to be able to delete all my files, but I would like to use a loop structure. How could I do this using the foreach in ruby, using the capybara? I've tried several posts and I did not get the expected result.
enter image description here
Delete File
Assuming there are no other buttons with the the class File__Button, you should be able to do something like:
page.all(:css, 'button.File__Button').each do |button_element|
button_element.click
end
If there is a confirmation, you will need to click that as well.
Related
My application under test has been developed by external suppliers so I have no control over the HTML structure. The application is extremely Javascript and Ajax heavy, with numerous dynamically generated buttons and auto-complete lists.
In other words, the characteristics of the pages are that they are filled with:
Elements with no fixed IDs (IDs are generated on the fly and have
numbers or other text dynamically added to them)
The same happens with some classes
Most of the times the buttons have no text associated with them since they are either custom coded 'down' arrows for lookup lists
(which aren't lookup lists but hidden divs) or '+' and '-' icons to
maximise or minimise portions of the content. -
It is therefore very difficult to identify these elements, especially the buttons.
I am trying to write a generic 'I click on the button near y' type of step so that it is not necessary to hardcode each and every button (assuming I can even get something to identify them with) into each and every test.
The thinking behind this is that normally there is a label of some sort close to the button at least.
What I want to to is to find the text label, then see if there is a button inside the same scope, and if there is not, move 'back' through the parent elements, and check if there is a button inside the scope of each parent level, up to 5 parents.
There might be all sorts of problems with this approach but I am just curious to see if this will work in general. I have run into some problems.
First I tried to use Xpaths, so I got the Xpath of the parent through :
$parentelement = $element->getParent();
$parentXpath->getXpath();
This would give me an Xpath of : (//html//span[text()='Cost center'])[1] and moving up through the parent elements all the time, they would become successively:
(//html//span[text()='Cost center'])[1]/..[1]
(//html//span[text()='Cost center'])[1]/..[1]/..[1]
and so forth.
The actual button is located in: (//html//span[text()='Cost center'])[1]/..[1]/..[1]//button but it has to go through all the parent elements in order to get there, so it will start with (//html//span[text()='Cost center'])[1]//button and should end with (//html//span[text()='Cost center'])[1]/..[1]/..[1]//button where it should find the button.
Trying to use Xpath I used:
$button_element = $session->getPage()->find('xpath',$parentXpath."//button")
I soon saw that the 'find' command appends an //html to the front of your xpath string so the Xpath that it tried to use ended up being (for each parent Xpath, but using this one as an example):
(//html(//html//span[text()='Cost center'])[1]/..[1])
I then stripped out the brackets as well as the //html, leaving me with:
//span[text()='Cost center'][1]/..[1]
but when I tried:
$button_element = $session->getPage()->find('xpath',$strippedParentXpath."//button")
I got the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '(//html//span[text()='Cost center'][1]/..[1]//button)[1]' is not a valid XPath expression
However, Firepath can execute this expression and does not show a syntax error for it, although it does not find the actual button (since the button is actually located one level up, where Firepath DOES find it).
So my question 1 is: What is wrong with my Xpath that I can't use it in the find? It actually looks as if //span[text()='Cost center'][1]//button does not throw the same exception, since as I said, I am looping through the parent Xpaths, and it starts with //span[text()='Cost center'][1]//button. It crashes on //span[text()='Cost center'][1]/..[1]//button.
My second option was to get the parent element each time, starting with finding the text on the page, but then to search for a button inside the scope of the parent element using the findbutton functionality.
Looping through the parent elements (up to a maximum of 5):
$parentelement = $parentelement->getParent();
$butonelement = $parentelement->findbutton('xxx');
In other words, find ANY button in the scope of the parent element. The problem I have is how to specify a generic 'button'.
One has to associate SOME text with the button (depicted by the 'xxx' above).
But this is a typical example of buttons in the application:
<button class="autocomplete_button" type="button" id="button_OM_1"> </button>
Where the class is used more than once, and the ID is auto-generated and not the same number all the time. There is no text associated with the button since the class specifies an image.
Question 2: So how can I use 'findbutton' to generically find a 'button' no specific distinguishing characteristics? Please note that I actually did try findbutton("button"), taking the chance that there might be a 'button' somewhere in a button, but this did not work either. At least, it doesn't work consistently and by that I mean that the same test randomly seems to either find or not find the same button when I run the test a couple of times.
After doing some more investigation on this issue I have found the following:
My method of trying to find the closest button to a piece of text via traversing 'up' through the scope of the divs and spans around the text (using xpath) is actually working.
What is NOT working is SAHI, which I am using as the web driver. In other words, it is not a Behat/Mink problem, it is SAHI specific issue.
I tried the same code using Selenium2 and it executes perfectly.
I still require an answer to question 2 - how can I use findbutton() without a specific parameter such as the ID, name or value but I will see if I can find an answer to that question separately and on the Behat user group since I do think that is a Behat/Mink specific issue.
I normaly use css selector and with that, I use to navigate to the class and ID's that the button is inside. it is easier than xpath I think, like you can use
$this->getSession ()->getPage ()->find ( 'css', '.parrent1 .parrent2 .autocomplete_button ' );
I think this will help you as you know which button your gonna use in each scenario
Does anyone know how to delete an element from the source using Watir? There doesn't seem to be a method for removing elements. Perhaps I'm missing something.
If you know JavaScript, you could execute any JavaScript code on the page.
Example:
browser.execute_script("some javascript code")
I am not a JavaScript ninja, but this question could help you: JavaScript: remove element by id.
Remove elements by css:
browser.execute_script("[...document.querySelectorAll('.some.class')].map(e => {e.parentNode.removeChild(e)})")
We can remove it with javascript. Here's an example to remove a breadcrumbs div element but it's id:
browser.execute_script("bd = document.getElementById('breadcrumbs'); bd.parentNode.removeChild(bd);")
The Purpose for Watir is to do web testing, which is to say drive the browser as if a user was interacting with it. That means doing the things a user could do, clicking on stuff, filling in input fields, etc. It also means being able to verify what is there on the screen that the user can see or interact with.
Since a user cannot delete elements, there is no means by which to do that using the tool.
If the application provides a way for users to 'remove' or 'delete' something, like closing a simulated window, removing a tab etc, then you need to do that by simulating what the user would do (usually clicking on some specific element) in order for that to happen.
I'm using watir-WebDriver to automate a website test automation. I have to get to the Sibling link.
looks like this
Something > Something 2 > current page title
(link1) (link2) (text1)
I have the class id of the text1, and I have to go back to the Something 2 by clicking on the link2.
How do I get this.
I think you can get the sibling link with elements_by_xpath.
example
HTML source:
bar<p class="baz">text</p>
watir-webdriver script:
p(:class,'baz').elements_by_xpath('preceding-sibling::*')[-1]
If you can find a common parent for the two siblings, you can go to that parent, then select the element from that point using something like this:
source html:
<tr>
<th>No. of films</th>
<td>7</th>
</tr>
scrape (#b is the browser object)
#b.th(:text => "No. of films").parent.tds.first.text
#=> 7
If (text1) is a child of (link2) you might try:
(text1).parent
If I understand right you have some kind of container element like a like a div, and inside it are two links and some text. What is not clear is if the text belongs to the outer container, or is inside a container of its own. Given you say you have the 'classid' (is that the class? or the id?) of the text, I'm going to presume that it is in it's own container element.
In that case
browser.element(:class, 'value').parent.link(:index, 2).click
That's the best I can do without an actual sample of HTML, a clearer model of the DOM
As all the previous answer says, you gotta have a reference point above this hierarchy. Then browser.p(:id, 'known').parent.links[1].click would do the job. But if you really really don't, try this hopelessly simple approach:
browser.link(:text => 'Something 2').click
Is there anyway of marking several different rows in a scrollable data table ?
I know how to mark one row
b.div(:id, "listProductsForm:productList:bc_4_0").click
I just want to simulate the "Ctrl"- button is pressed down
How does your "scrollable data table" looks like? Show us the HTML. I see that you are using div tag to access one element.
You probably need to fire some JavaScript event. See How to find out which JavaScript events fired?
The chances are good that you will need to use a combination of Watir::IE.send_keys() and the click actions on the document.
Try something like this:
#browser.send_keys("{CTRLDOWN}")
#browser.div(:id, "listProductsForm:productList:bc_4_0").click
#browser.div(:id, "listProductsForm:productList:bc_5_0").click
#browser.send_keys("{CTRLUP}")
Watir API documentation: http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/IE.html#M000497
The documentation above links to the specific key commands that can be sent. I'm pretty sure this will require that you have AutoIt installed.
How do you add a Hyperlink to a word document using an existing bookmark. I have been testing using IRB but continue to get Command Failed.
I have attached to a running word application have text selected that I want to tie to the hyperlink. For testing I have been trying to just add a google hyperlnk. I figure once I get that then I would be able to figure out the bookmark. This is the command I am using
doc.Hyperlink.add(word.selection, 'http://www.google.com', '','','text to display')
The two blank parms are for SubAddress and ScreenTip respectivly.
Luke-
You're very close...
Change this...
doc.Hyperlink.add(word.selection, 'http://www.google.com', '','','text to display')
...to this...
doc.Hyperlinks.add(word.selection.Range, 'http://www.google.com', '','','text to display')
There were two changes necessary:
(1) You call the Add method on the Hyperlinks (plural) collection, and (2) the first argument needs to be a Range object.
With these changes, your code works for me.