How to get visible elements with WebDriverIO - testing

I my tests I am using WebDriverIO. I want to get all elements with class form_error that are visible.
I try something like this:
$$('.form_error:visible')
but the :visible filter is not a valid webdriverio selector construct.
How to achieve that?

WebdriverIO uses CSS selectors to find elements, and :visible is not a valid CSS selector.
Instead, you could go two ways:
Get all elements with a class of .form_error, then run isVisible() on them and filter the hidden ones out
Use a custom execute function to run JavaScript on the page to check for visibility of elements. Something similar to this answer.

Related

selenium + capybara: find selector anywhere within element

Assume we have a <div class='whatever'> and somewhere deep inside there is an element <div class='inside-whatever'>
What i need is a way to access that particular inside-whatever-div using Capybara's and/or Selenium's methods.
Problem is, there is another <div class='inside-whatever'> on the page not inside <div class='whatever'>, so
within(:xpath,'//div[#class="whatever"]') do
find(:xpath,'//div[#class="inside-whatever"])
end
returns an error basically saying that there are multiple inside-whatever divs on the page.
What works is to build the xpath from whatever like
'//div[#class="whatever"]/div/div[3]/div/div[5]'
but that is pure madness.
So, is there any better way to look for selector anywhere inside any given element without having to specify a direct path?
You can merge your xpaths like this:
//div[#class="whatever"]//div[#class="inside-whatever"]
The real issue here is that you've fallen into the XPath // trap
find(:xpath,'//div[#class="inside-whatever"])
searches globally rather than from the context node. Instead you should get used to starting your XPaths with .// which will search from the current context node
within(:xpath,'.//div[#class="whatever"]') do
find(:xpath,'.//div[#class="inside-whatever"])
end
and do what you expect. This is mentioned in the Capybara README - https://github.com/teamcapybara/capybara#beware-the-xpath--trap
Note: CSS selectors don't have this issue and for most elements people are selecting read cleaner, which is why Capybara defaults to the :css selector
within('div.whatever') do
find('div.inside-whatever")
end

Access Selenium IDE output in Selenium WebDriver

I'm having this requirement in which i need to access an element on my page and want to get all the properties of the element. I have already written a webdriver script to get the id,name,css,linktext but i'm not getting the idea how to get the xpath and css selector for that element.
One thing which i'm having in my mind is select the element using Selenium Ide and in the Select tab it will get all the attribute value which is very useful for me, but how to get this result in my selenium webdriver.
I dont know whether this is possible or not but if someone can give me some reference that will be very useful.
Hope for some positive answers.
You can generate absolute xpath. Please take a look at this: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5520
Css and xpath selectors are based on the element attributes and/or tags.
You can write a method to generate some selectors but this does not guarantee that your selector would be efficient and it can break even when a slight change in the page is done.
Even if you are using a method like #arun-prakash suggested for xpath is the same thing. I don't see the reason behind this.
You will have to use a selector to identify the element so why get the selector again? You should ask the reason and how these selectors would be used.

Why CSS contains() fail to work with Protractor?

In Protractor, the xpath contains[.,''] works all time instead of CSS's contains() feature; is there any cause for such broken functionality? or any tweak to implement it?
The pseudo class contains is not a CSS feature. It's implemented by external libraries like JQuery or Mootools. To get an element by text, use the locator by.cssContainingText:
var elem = element(by.cssContainingText('div.left', 'my text'));

Selecting element with multiple classes in Capybara

I'm writing automation code in Capybara with Selenium.
I have the following element in my HTML, and I wanna click this element in Capybara.
click me
At the moment, the way worked is something like following.
find('.classA', :text=>"click me").click
But I wanna select the element from the names of the two classes like this
find('a.classA.classB').click
click_on('a.classA.classB')
I know we can get javascript code fired, but this is not smart.
page.execute_script('$("a.classA.classB").click()')
You can search an element by xpath
based on your example, seems like the following should work
//div[contains(#class, 'classA') and contains(#class, 'classB')]
You could also use css
(:css, ".classA.classB")

Selenium how to select an object by class

I have a web page with a form and has a field that uses the jquery autocomplete function.
This is how the HTML renders after a user name returns 1 or more results.
However I cannot figure out how to make Selenium "click" a result.
Can I do a jQuery type of selector.
e.g.
$(".ul.ui-autocomplete li:first a")
Use XPath selector in Selenium:
xpath=//li[contains(#class, 'ui-autocomplete')]/li[1]/a
not checked, might require some corrections.
in response to "Can I do a jQuery type of selector," jQuery uses CSS selectors. Selenium can also use CSS selectors; just prefix the selector with "css=". so:
css=.ul.ui-autocomplete li:first a
Next way to use xpath like this
xpath=/html/body/ul[2]/li[1]/a
Suppose you have a dynamic XPATH then you can point to an element like this
driver.findElement(By.className(""));