I want to wait when required text will become present.
I use:
#browser.text('required text').when_present
And I get an error:
ArgumentError: wrong number of arguments (given 1, expected 0)
How it can be implemented? I wan't to wait only for text without any dependency to html element.
Using page-object gem without any dependency to html element:
#page.wait_until { #page.text.include? 'required text' }
Reference
The right syntax is #browser.text.include?('required text')
This should help you get rid of the error.
Rather than looking for text for the whole page, it may be more efficient to create an element for the text.
span(:ur_name, :text=>/partial string/)
or however you prefer defining it and then call the element level wait functions for the object. Ofcourse, you can also dynamically declare the element if the text is dynamic.
span(:ur_name, :text => "#{variable_containing_text}")
page.ur_name_element.when_present
A trivial benefit being this ensures your text is not displayed out of place, esp if dups are possible. If the element doesnt support text locator, you can still identify the element, check for text and then use the element wait.
better yet you can do it like this:
Watir::Wait.until(10) { #browser.text.include?('required text') }
the default value of timeout is 60 seconds if not set, its set to 10 seconds in the example above.
Reference
wait_until
As per the latest docs, use .wait_until( &:present? ):
#browser.text( "required text" ).wait_until( &:present? )
Related
When I use:
cy.get('b').contains('xdz') // find 1 element
but when I use:
cy.get('b:contains("xdz")') // find 2 elements
Can someone explain me what is the difference?
cy.get('b').contains('xdz') is invoking a Cypress command, which is designed to only return a single element. This is by design so that you can narrow a search by text content.
cy.get('b:contains("xdz")') is using a jquery pseudo-selector :contains() to test the text inside element <b> and is designed to return all matching elements.
Pseudo-selectors are extensions to the CSS selector syntax that apply jQuery methods during the selection. In this case :contains(sometext) is shorthand for $el.text().contains('sometext'). Becuase it's part of the selector, it returns all matching elements.
It's worth while understanding jquery selector variations, as this example illustrates - it can give you different results for different situations.
contains('xdz') is a cypress command which always yields only the first element containing the text. You can read more about it from this Github Thread.
:contains("xdz") is a Jquery command and it returns all elements containing the text. You can read more about it from the Jquery Docs.
internet.find(:xpath, '/html/body/div[1]/div[10]/div[2]/div[2]/div[1]/div[1]/div[1]/div[5]/div/div[2]/a').text
I am looping through a series of pages and sometimes this xpath will not be available. How do I continue to the next url instead of throwing an error and stopping the program? Thanks.
First, stop using xpaths like that - they're going to be ultra-fragile and horrendous to read. Without seeing the HTML I can't give you a better one - but at least part way along there has to be an element with an id you can target instead.
Next, you could catch the exception returned from find and ignore it or better yet you could check if page has the element first
if internet.has_xpath?(...)
internet.find(:xpath, ...).text
...
else
... whatever you want to do when it's not on the page
end
As an alternative to accepted answer you could consider #first method that accepts count argument as the number of expected matches or null to allow empty results as well
internet.first(:xpath, ..., count: nil)&.text
That returns element's text if one's found and nil otherwise. And there's no need to ignore rescued exception
See Capybara docs
Capybara is not able to find a <p> tag by it's id in my cucumber test. I'm able to see the element when I save_and_open_page. But I can't locate it with has_css? or find:
pry(#<Object>)> page.html.scan(/notice_sent/).count
=> 1
pry(#<Object>)> page.html.scan(/id=\"notice_sent\"/).count
=> 1
pry(#<Object>)> page.find('#notice_sent')
Capybara::ElementNotFound: Unable to find css "#notice_sent"
from /Users/me/.gem/ruby/2.1.7/gems/capybara-2.4.4/lib/capybara/node/finders.rb:41:in 'block in find'
What am I missing?
By default Capybara doesn't find elements that are not visible on the page. You can try
page.find('#notice_sent', visible: :all)
to see if that's the case. If so, and you're testing an app, then you should perform whatever actions a user would perform that would make that element visible, and then check for its presence.
Though I am a newbie to Capybara, I think this might work for you !!
page.find('#notice_sent', :visible => false)
Also, add this code to your env.rb file.
Capybara.ignore_hidden_elements = false
The accepted answer is good in most cases, however, there are times that elements may dynamically present; and not just hidden. When using find and the element is not present we get an error very similar to when it is present, but hidden. How do we handle that case?
Capybara::Node::Finders#all Also known as: find_all, has the same visibility option as find, but will not throw an error when there are no matches.
page.all('#notice_sent', visible: :all)
This is very similar to where vs find in ActiveRecord, and there are uses for both approaches.
if you want to return the matching element or nil instead of an Array / collection, then you can append first and get a result that closer matches the behavior of find without the error.
page.all('#notice_sent', visible: :all).first
I think, it is time to update capybara for the very beginning.
And, I can not test it out now, but try page.html.find('#notice_sent')
I am using the page object Gem with Watir. During testing I found that I have a field that has the same contents that show in the same location but have separate unique ID's. The difference is before you get to the page.
I tried using Xpaths:
select_list(:selectionSpecial, :xpath => "//select[#id='t_id9' OR #id='t_id7']")
But was met with a script error.
They are static ID's but I want to force them into one variable since that would allow me to use "populate_page_with" feature.
I have a long winded way currently, but I am fishing for a more efficient way that works with the page object Features.
Does anyone know of a way to do this?
Your approach of using xpath can work. The problem is the syntax errors in the xpath selector. It should be:
"//select[#id='t_id9' or #id='t_id7']"
Note:
The start should be a // rather than a \
Using or is case-sensitive; it has to be lower case
There was also a missing closing ' for the first id attribute
Personally, I find css and xpath selectors harder to use. I would go with the id locator with a regex. The following gives the same results, but some will find it easier to read.
select_list(:selectionSpecial, :id => /^t_id(7|9)$/)
I am coding with Groovy, however, I don't believe its a language specific set of questions.
I actually have two questions
First Question
I've run into an issue while using HtmlUnit. It is telling me that what I am trying to grab is null.
The page I'm testing it on is:
http://browse.deviantart.com/resources/applications/psbrushes/?order=9&offset=0#/dbwam4
My code:
client = new WebClient(BrowserVersion.FIREFOX_3)
client.javaScriptEnabled = false
page = client.getPage(url)
//coming up as null
title = page.getByXPath("//html/body/div[4]/div/div[3]/div/div/div/div/div/div/div/div/div/div/h1/a")
println title
This simply prints out: []
Is this because the page uses onclick()? If so, how would I get around that? Enabling javascript creates a mess in my cmd prompt.
Second Question
I am wanting to also get the image but am having trouble because when I attempt to get the XPath (via firebug) it shows up as: //*[#id="gmi-ResViewSizer_img"]
How do I handle that?
First Answer:
/html/body/div[3]/div/div[3]/div/div/div/div/div/div/div/div/div/div/h1/a
Your XPATH was off by one in the predicate filter for the 4th div of the body, it should be the 3rd div. It appears the HTML for the site can/does change from when you had origionally snagged the XPATH using Firebug. You may need to adjust your XPATH to accommodate for potential change and be less sensitive to some differences in document structure.
Maybe something like this:
/html/body//div/h1/a
Second Answer: The XPATH that you listed will work. It may look odd/short(and may not be the most efficient), but // starts at the root node and looks throughout every node in the tree, * matches on any element(to include the img) and the [] predicate filter restricts it to those that have an id attribute who's value equals "gmi-ResViewSizer_img".
There are many other options for XPATHs that could work as well. It will also depend on how often the HTML structure changes. This is one that also works for the page referenced to select that img:
/html/body/div/div/div/div/img[1]
I had the same problem, I solved when I realize iframe tags on page, try call
((HtmlPage)current_page.getFrames()[n].getEnclosedPage()).getElementByXPath(...
where n is the position in frame in iframe collection. It's work for me !!!
Thanks a lot.