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
}
Related
There is a project where we are going to automate the UI but the Automation team is suggesting that we have to use ID's all over the page so that it will be easy to automate there script.
My Question here is why we will use ID's everywhere ? hampering the Html and Css structure.
The webpage can be automated without ID's in html yes or no ?
Yes, a web page can be automated without ID's. For example, you can play with cssSelectors here https://www.w3schools.com/cssref/trysel.asp (note that example page has elements with and without ids)
Using ids for element's lookup in automation is generally considered as a best practice. If you use ids your automation tests will become independent of html structures which basically will make them more stable.
For example, in the first version of your app you may have some text implemented as
<p id="someTextId" class="someClass">Hello world</p>
but at some point may decide to rewrite it as (change the tag and even apply different class name)
<div id="someTextId" class="anotherClass">Hello world</div>
In case you rely on id #someTextId to locate an element your test will still be able to access necessary element and interact with it properly. If you use p or .someClass your automation test will fail to find an element even though from the ui perspective the same text will be displayed in a browser.
We faced several downsides of using id:
Some frameworks do not recommend using them or generate them automatically. (Potential issues with ids described here https://www.javascriptstuff.com/use-refs-not-ids/, https://www.quora.com/Are-IDs-a-big-no-no-in-the-CSS-world, https://dev.to/claireparker/reasons-not-to-use-ids-in-css-4ni4, https://www.creativebloq.com/css3/avoid-css-mistakes-10135080, https://www.reddit.com/r/webdev/comments/3ge2ma/why_some_people_dont_use_ids_at_all/)
Some other logic may rely on them, so changing/adding them for the need of automation may somehow affect other app logic unexpectedly.
What you can use instead of id is some other attribute. For example in our projects, we have switched from id to a specific attribute named dataSeleniumId. It clearly shows that the attribute is selenium tests usage only. As a next step, you can add a rule in your team when someone changes or removed dataSeleniumId attribute he should inform automation testing team about it. As changing/removing this attribute will lead to test failures and to avoid any false failures it is better to fix it in advance.
For an automation developer its much easier to browse trough the html code and see the id of specific button/text field/etc.. to implement the relevant locator inside the automated test.
In most cases, the project start to receive duplication of classes or complicated nested elements. This make the life of automation dev harder, because of writing xpath or css selectors, verify that they work and this locator finds only 1 unique element.
Its up to the team and code style suggested from the team leader.
Back on the question, yes the website can be written without id's but if the goals is to automate large part of the website, id's would be great helper to the automation dev team.
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.)
Is it possible to find the list of TestSets associated with a particular TestCase using the WSAPI?
When you are looking at a TestCase in Rally, you can simply click on the TestSet tab, so it seems this should be possible. However, according to the WSAPI Doc, I don't see how to do this because there is no TestSet field on a TestCase.
And, you can't seem to go the other way, since though TestSets have a TestCase field, the doc says you can't query against it. So how is this done?
I wish I had a better answer for you, but unfortunately this is a somewhat annoying quirk of the WSAPI data model when it comes to Test Cases. As you've observed, unfortunately there's not a Test Set attribute on Test Cases.
The only way to really handle this is client side, and it's somewhat painful. You have to obtain the TestCases collection off of each TestSet, and then loop through that collection (an array of TestCase ref's) to see if there's a match with your particular TestCase of interest.
In my project we have a web based tool where it collects all the exceptions that occur in log. The list of exceptions will be displayed in table and a icon will be displayed in a column to get the stack trace. Some one needs to check all those exceptions and we need to assign to different teams based on the keywords.
Eg : If the stacktrace contains "DB Connection error" it has to be assigned to DB team, if it contains code error like "classcast exception" in particular file then it has to be assigned to individual owning the module. Currently some one has to open each n every row in the table displayed and open the stack trace and do Ctrl + F and if any of those keywords are found assign it to respective team.
We don't have any access to the tool's database and it's web based. I need some good ideas to automate it. Please suggest me with which technology and idea to automate it.
Sounds like selenium + programming language of your choice is a good fit (even without a selenium).
You need to:
download source code of the page containing exceptions table.
Use any of these: wget, curl, selenium, urllib for python, etc.
put it into variable and extract exceptions list in convenient to you format.
Use any xpath-powered tool, like selenium, lxml for python, etc.
do your thing with the extracted exceptions list: search for keywords, assign tickets, etc.
Use programming language of your choice.
Definitely you can achieve this using selenium.
Following are the steps to follow.
Supply date criteria with type command to the boxes text boxes element.
click search button.
store the required text in variable using storeValue(locator, variableName).
click on image link if you are able to keep track of popup window else you have to use
$second=$sel->getAttribute("//html/body/.../a#href"); and now your $second has link address $sel->openWindow($second,"MyWindow2"); now select that new window $sel->selectWindow("MyWindow2"); and do operation of storing required data from the popup.
Now agian for selection of previous window use `$sel->selectWindow("null"); // hear $sel is object of selenium (i.e. $this).
Select your required option using select(selectLocator, optionLocator).
Use selenium RC for above steps so that you have more manipulation with the data stored and as per requirement operations.
In above steps syntax are of PHPUnit.
Please try the steps may help you.
Regards.
I want to create Selenium test that posts web form, but one field is riddle from the list. Riddle gets randomly. Answers are known. I should fill answer in field.
Is it possible to access in Selenium test to the page source code to define current riddle and place in the input field answer?
Certainly. For example, in Ruby bindings it'd be #browser.page_source