For test automation, how can we know if the webpage is using a dynamic id. Here, I don't mean how can we get the dynamic id. Instead, I want to know how to use API, e.g. Selenium API, to know if the web element is using dynamic id.
For example, some ComboBox's id will not change when we select its option. But for other ComboBox's id, it may change as selecting its option.
So, do we have a way to detect, or to know, if the web element in the webpage is using a dynamic id?
No, unfortunately you have no way to discover if and id will change using Selenium.
The only way an HTML element could have its id attribute changed is through JavaScript code. And the only way to know if a piece of JavaScript code changes an id is to execute it. (In other words, you can't, ahead of time, know the id will change. You can only know that JS code changes an id when it actually changes the id, and by this time, of course, the changing already happened.)
If, on the other hand, you want to know if an element had its id changed, you can, before the change takes place, get a hold of it through Selenium (using driver.findElement()) and then keep checking if its attribute changed. (If the WebElement instance becomes stale in the process, you could, as a workaround, add a class with a unique name to it, and then find it back using by.className() - since you may not find it again by its id like before, as it could have changed.)
Related
A web element that has a unique id/name can be identified using xpath as well. Then what is the advantage in using By.id/By.name over By.xpath. Consider the following example. Below is a web element.
<input id="abc" name="abc" xpath="abc">
This web element can be identified as:
By.id("abc");
By.name("abc");
By.xpath(".//*[#id='abc']); or By.xpath(".//*[#name='abc']);
I want to know what is the advantage of using 1. or 2. over 3.?
As Gilles Quenot mentioned, you cannot assume that an ID attribute is unique, but it was and is meant to be, unless we're talking about mobile apps. In web app, ID is the ideal search method because it is supposed to be unique.
The name attribute is often also unique, but it is not necessarily expected to be, and often is not.
One disadvantage of using xpath is that an element could be moved in the document, or other items added, and it might break your xpath depending on how it is crafted. In theory, at least, a locator that uses ID would be "more" immune to such breakage, but as was hinted at, nothing is guaranteed.
As a rule, I will use xpath if a unique ID is not available, and I attempt to write it such that it is generic enough to resist "breakage."
I don't think Xpath is less preferred then ID and Name.
ID might not be unique always, in current websites DOM, IDs are mostly changes by time. You cant stay with one ID and run the script again, if you need to try you can check with Amazon Site as well.
Name may be or may not be unique. You are right in some pages you might find elements might have unique names but let's take an example if you have a page with first name and last name sometime dev team just copy and paste those first name lines to last name as well. So now you will find same name attribute in a single page. So you can't rely on name as well.
Xpath is unique, because you are the one who is designing the xpath and and you always want an element with unique identity. So now in Name example you can get the first name and last name separately ny xpath.
I believe Xpath is better choice over the Name and ID attribute.
Your question is very valid and every possible option you mention will work but why we have preference of ID and Name locators over XPath?
Instead of giving direct answer Let me explain few other concept which will help you to understand it.
The way selenium interacts with web controls through HTML DOM and HTML DOM is W3C standard which is followed by every browser and it's Java script engine such as V8 for chrome, Chakra for Edge etc.
HTML DOM expose method for GetElementById() , GetElementsByName() which are all supported by all browser as per W3C standard. So when you use locator such as Id, Name and others which are already implemented and will always work for you.
Refer https://www.w3schools.com/jsref/dom_obj_document.asp
XPath is actually used for navigating through elements and attributes in an XML document. Since is HTML DOM is also form of XML then Xpath will also work but XPath was not designed for HTML DOM. It has other purpose.
Conclusion is when you have already methods supported by HTML DOM then preference should be given to those over XPath which is generic way to navigate and There was some instances of XPath with Internet Explorer browser where it was not behaving as expected.
Currently the record function in Katalon studio uses all possibilities to identify an element (i.e. tag, id, name, class, text and full XPath). For some reason the full XPath always fail to identify an element in actual testing, and text on element is something that changes a lot and does not matter to the testing in most case. Currently I need to manually unset the unnecessary identifier after each record. Is there a way I can tell the record function to use only tag and id to identify an element?
I found this answer from Katalon Forum. So as they said, this features will be implemented in later releases.
https://forum.katalon.com/discussion/4368/how-does-katalon-studio-decides-which-identifier-to-be-used-while-automating-tests#latest
I've been on the quest for the past week, to find out how to automate some way, to fetch ID's, classes and so on, from HTML source code of a page.
My objective is to create a database, from where my java program will get data, to create automated tests, depending on the elements found on the page.
To populate that Database manualy would take me ages, since the website is huge. I would have to inspect all elements by hand, and get the xpath from every single one of them.
So, what i was looking for on the web, is a way to actualy detect all elements present on a page, and then get all the information about them. Therefore, save that information on a database, after it beeing filtered and treated.
Do you guys know any way of doing so?
Thanks!
WebElement body = driver.findElement(By.tagName("body"));
List<WebElement> childs = body.findElements(By.xpath(".//*"));
for(WebElement child : childs){
// Get data for each element
}
I would like to write web pages that have links to Rally issues (Test Cases, Defects, etc). I would like to be able to generate a URL with the FormattedID. Is this possible? Or do I really need the objectID? For example:
http://rally1.rallydev.com/363953481d/detail/testcase/TC1665
(or something like that, instead of the cryptic object id)
The following allows users to go directly to the detail page of a work product without having to know the Object ID:
https://rally1.rallydev.com/#/search?keywords=US1234
This relies on a feature of Rally's search functionality and isn't officially supported - so the above URL isn't guaranteed to work forever. However it's a decent way to use Formatted ID's as a workaround.
Searching by just keyword will give you the item with that ID and related items (e.g. other items that mention the desired ID in their name). Sometimes this is fine, sometimes not.
To search for DE75700 and DE72760 only, and no others use
https://rally1.rallydev.com/#/search?keywords=FormattedID%3ADE75700%20FormattedID%3ADE72760
This is equivalent to manually typing
FormattedID:DE75700 FormattedID:DE72760
in the Rally search box.
As a corollary to the main answer I have defined a Chrome browser bookmark which will take me right to any Rally item by its ID.
The URL for this bookmark in full is:
javascript:(function(){window.location = "https://rally1.rallydev.com/#/search?keywords=" + prompt("Enter ID:");})();
When this bookmark is activated, it prompts you like so:
I find this to be a huge time saver.
Thanks to Displaying a prompt from javascript Chrome bookmark.
I want to know the process how to store Dynamic Id in my test while using Selenium commands.
Problem:
I have actually on the page where the textarea I want to focus within a test. It has the id like this: id="txt_00092" and it's generated by db auto increment.
It's a comments textarea. Let's say in my test I add a comment over some post. So if I run this test again this becomes 00093. This is where I have a problem as to how to store this dynamic handling in my test.
I hope you understand where I am stuck.
I didn't understand fully your problem. Put down the problem with the ID for know and tell us what is the purpes of the test: Check if the text was fully submited? if so you can type and check if the last(or first, depend on your query oredeing, i.e Asc/DESC) div/td/whatever contains the text.
If you need to chek the ID it's a littel problem becuse here we need to breack the string and do that with JS it's tricky. Hope i help you.
When the Xpaths are dynamically changing, using Xpath this way might work for you:
[starts-with(#id,'txt_0009')]