xpath is changing on each automation test run with Selenium - selenium

I have a major issue with Selenium which xpath is changing on each run. For instance, here is the field identifier for a postal code field on a warehouse module (one of the modules that we have in our system):
(.//*[#id='warehouseMaintenance:j_idt137_body']/table/tbody/tr[4]/td[4]))
it changes to
(.//*[#id='warehouseMaintenance:txtZip']))
during the execution.
any idea how to avoid this?

You should assign ID to the postal code field if possible.
Using chain to locate element is not a good practice.
If you have multiple pages and naming convention for each page are different. I suggest you to add a classname to the field, then using cssselector class$='postalcode' to locate the element.
For example,
<input type="text" class="xxx yyy warehouseMaintenance postalcode">

Did U tried using css's? They are not browser dependent.
U could use cssify to change your xpath's to css's or get it from browser.

Related

How to identify an element without using xpath when id name is not available in selenium

There are few elements in an UI page without ID. I will download a particular version and then save all the current element tags and give to script as input, since few elements are not having id, this is causing script failures.
How can I locate the element without using Xpath.
Is there any simple way when there is no fixed id.
The short answer is "no." Sorry. All of the usual ways (by id, class, etc.) are relying on the same css information to locate elements. Xpath just shows all the ugly plumbing out in public. I don't think xpath has ever been described as "simple" but there is usually a way, using xpath, to find any element.
Xpath can be intimidating. Start with a plugin that will generate the xpath for you, once you click on an element. Usually the xpath generated will be extremely long and inefficient, but with practice you can see what can be trimmed and what is crucial. And to do that, also use a plugin that will "check" your xpath to see if it can find the element. Once you can find it (and ONLY the element you want) try trimming it to see if you can still find it with the abbreviated xpath locator.
reference ImageDon't be afraid of Xpaths. It's relatively easy to grab an Xpath using the Google Chrome browser. Navigate to your page and open Developer tools. Right-click on the particular tag for which you need an Xpath. Copy -> Xpath

Automation of test case independent of ui changes

We are automating test cases for hardware devices like tv, raspberrypi etc which require to perform certain number of steps and check expected result at the end of each step. To automate these test cases we are using dom element attributes such as id, class,data-component-id etc to fetch the objects and perform actions.
The problem with this way of automation is every time UI changes we need to change the dom elements id, class etc and hence it is rework of the scripts.
I want to know if there is automation framework or any other way using which we can automate test cases independent of ui changes.
Instead of using the exact path, try to identify the element in a more generic way. Using "contains" will help you to identify the elements with the text attributes with out going into the way it is defined. So even the DOM structure changes but with the text being same, your tests will not fail.
Contains
It is very handy XPath Selenium locator and sometimes it saves the life of a test automation engineer. When an attribute of an element is dynamic, then you can use contains() for the constant part of the web element but also you can use contains() in any condition when you need.
Syntax: //tag[contains(#attribute, ‘value‘)]
Example: //input[contains(#id, ‘er-messa’)]
Examples:
Java
1
2
3
4
5
6
7
8
//*[contains(#name,'btnClk')]
--> It searches "btnClk" for all name attributes in the DOM.
//*[contains(text(),'here')]
--> It searches the text "here" in the DOM.
//*[contains(#href,'swtestacademy.com')]
--> It searches "swtestacademy.com" link in the DOM.
That's why you should use id selectors if possible. They are supposed to be unique and hence immune to UI changes. The name selector should work the same way.
If you are using Selenium, use the following list of object locators in (loosely) this order of preference:
id
name
tag name
class
CSS or Xpath
linktext or partial linktext
And if you really want the elements to be consistently identifiable, you might talk to the developers and ask them nicely to add the ids.

Locating elements in selenium IDE

I have tried to locate button in my web app using xpath but it changes automatically each time I open selenium IDE. Is there any other way to locate it except using xpath or position? can I locate it using class name? If yes then how can I do it?
You can use xpath to find element by class name.
//*[#class='someClass']
where, someClass is the class name of your element.
Since it is your webapp, consider adding an id or a name to uniquely identify the element. It also makes the xpaths easier to write as you don't need to consider the possibility where you might be grabbing too many elements.
Answer - If by default recorded xpath are not working for your application, then you can define your own xpath for those components which should remain same throughout execution.
Please refer below URL which shows ways to develop userdefined xpath :-
http://docs.seleniumhq.org/docs/appendix_locating_techniques.jsp
Use a CSS selector. This site really helped me: http://saucelabs.com/resources/selenium/css-selectors
if it has an id on it you can just say "id=yourid"
for css it could be something like this: "css=button[class*='yourclass']" <-- that says it's a button, and that in class it contains yourclass.

Selenium object identification

I am using Selenium webdriver to test my application & i am facing difficulties in identifiying button on the same. the code snippet is like :
<input type="submit" onclick="return sign(this);" value="Login">
and its xpath is :
html/body/table/tbody/tr[2]/td/center/form/center/table/tbody/tr[3]/td/center/input[1]
Which object property to use and how?
You should not use that XPath.
I would hazard a guess that you used some sort of tool, whether it's Firebug or IDE, to generate that XPath. Stop that now!
XPath is fine to use, and can be used here, just not relying on the tools to generate it for you! That XPath is destined for failure!
You will need to provide more HTML, specifically around that button.
However, you should just be able to use something as simple as:
//input[#value='Login']
You can use the xpath, if that is really stable. I found that it is much easier to define id tags in the html elements and the use a By.id locator. Alternatively you can use css selectors, depending on the "uniqueness" of your button something like this could work:
By.cssSelector("input[value='Login']")

Finding clickon Element using Selenium. (JAVA)

I spend hours already trying to find the way to find the Element using Selenium WebDriver. I assume I need to use driver.findElement(By.xpath("")), but I am not quite sure how.
I somehow need to find and click on "clickon" element. The problem is that part of that element is changing (see screenshot) I need to pick up from the file and putted into the xpath.
I would appreciate any help.
We have been rigorously searching for automated functional testing solutions recently, and we began with Selenium. The entire reason we decided to search for other solutions was that our application also has dynamic IDs with no other obvious XPath mechanism to identify them. Selenium is unable to identify these elements on the page without some additional knowledge, just as you would be unable to identify these elements on the page if you didn't already know what they are.
If you are controlling the DOM creation, consider adding a unique ID or class to this element.
We recently came across eggPlant from testPlant, and it is an interesting approach to functional testing. It's essentially image based. Other viable solutions are Ranorex or HP's QTP or SmartBear's TestComplete.
You can use xpath. If the div class is constant, you can use something like:
driver.findElement(By.xpath("list-row field-item")).click();
To view the xpath, you can install firefox plugin called 'xpath checker' found here and right click on the dom element and click 'View Xpath' option to get the xpath of the element and then you can use that xpath in your code.
Or you can even use regex in the xpath which is suitable for the similar problems. Xpath with regex is really powerful.
It seems that you want to click the div that has the on click attribute that contains certain text that doesn't change, ignoring the part that does. In that case, use an xpath like this:
//div[contains(#onclick, '/challenge/index/rfp_id/')]
This will select the first div with an onclick attribute with a value containing /challenge/index/rfp_id.