So Im curious about something, is there sort of a hierarchy as far as "good automation test design"? Especially when it comes to finding elements.
Since sometimes "click_button" or "choose" (for radio buttons)....just does not work (What usually causes this btw? Even when an ID/name is clearly shown in the HTML.....it'll throw an element not found?
Afterwards the next thing that I usually try is find. (Is this the wrong way to go about it?)
However with find you have many options, find by CSS, find by ID (if an ID exists) and then find by Xpath. Plus also finding by Elements (IE 'li' :text => "Whatever)
Is there a certain thing you should try before doing find (when an element is unable to be found the normal way, for whatever reason? Im really curious what causes this)?
And when using find should you rely on Xpath last?...first? (I seem to always have to end up using Xpath w/Capybara+Poltergeist. But it feels brittle).
Im just trying to figure out best practices for these things, is there a good guide for this?
find vs page.find or page.should have_content(text) vs just should have_content(text)? Whats the difference? What does appending page. exactly do?
Q.1 Sometimes "click_button" or "choose" (for radio buttons) does not work. Even when an ID/name is clearly shown in the HTML & it'll throw an element not found?
- One basic mistaken we might can do is - if we have seen element on page means that will be identified by program/code too. Don't believe only on eyes, Good if we right code considering needs of actual program.
- Error not thrown means, element is present and identified.
- Then we observed its not working as we are expecting action (Click/Choose/Select) : This happens because We have clicked on element but do we have really confirmed whether page is fully loaded and ready for the action to be taken?
- Mostly few services might be in progress, page may be under loading state. This may be the cause for its action failure and that too without even error
- Solution 1: We should add appropriate methods to check & confirm page is completely loaded.
- Solution 2: We should apply wait for element before taking action on it, to make sure presence of element programatically. This will be consider as good practice.
Q.2 What to use first and last from ID, CSS, Xpath and combinations of those?
Ans - Preferred selector order from Max to min:
- ID or Resource-ID
- Name or TagName or LinkName
- Combination of CSS & Text or ID & Text
Q.3 What does appending page. exactly do?page.find or page.should have_content(text) vs just should have_content(text)? Whats the difference?
- Actually you know that we are providing page object to take further actions on the "page." and "page." is doing exactly same [page. = page object]
- page.find : We are finding the element using this method.
- page.should have_content(text): We are applying assert to condition of text presence. If text is not available then terminate execution is mean and power of Assertion.
To work around element not found errors (when you are sure element should be found) you should add waits for element before you actually click on it.
But if you do it like this:
Wait element to be visible
Wait element to be clickable
Find and Click element
It will be ugly.
So for example you could do something like this :
selenium Wait until Element is visible
selenium Wait until Element is clickable
element = selenium Find element
And in tests its simply:
I think Capybara already does wait for element to be visible before clicking with "click_button", so you can either try to
-modify it to wait 'better'
-add longer timeout or use better locators (are you 100% sure it is found?)
-add your own waits somehow.