How do I make a Selenium test for a page with a textbox whose name constantly changes? - selenium

I am trying to create a test that can enter a username and password into a login form on a page. However, the textbox name changes on every load of the page, and so the test stops each time I run it.
How can I set up Selenium IDE so that it can identify the text box across refreshes?

I am assuming that the textbox only changes part of the ID and not the entire ID each time.
To work with this I would recommend using xpath like people have put above but make it more robust in that it only find that element each time.
e.g.
//input[contains(#id,'thePartOfTheIdThatNeverChanges')]
or
//input[starts-with(#id,'thePartOfTheIdThatNeverChanges')]
or
//input[ends-with(#id,'thePartOfTheIdThatNeverChanges')]

//form1 (3) - First form element in the HTML
Pulled from here as an example This is making use of XPath. In theory you can make use of XPath to focus on parsing the structure of the XHTML, however this is fairly fragile, so it's not necessarily a wise thing to do but it should take care of your issue.

Does the location of the text box change, or just the name?
You could use a css or xpath selector to get the text box if it is in the same place.

I would recommend CSS because -
a. for the readability
b. for the ease of access (and, in your case, maintainability).
I use firebug for accessing/formulating the CSS identifier.

Related

How to develop inspect element tab for my Web Browser(Using VS2015 Community) in VB.NET

How to Develop inspect element tab section for my Web Browser which i've developed in VS2015 Community. Please help I'm searching this from last few days...
A suggestion for this could be using the TreeView Class which could potentially provide you with what you are looking for (If you're looking to create the 'Inspect Element' in it's entirety). If you just want the code then simply using the DocumentText WebBrowser Property would do that. Just having a TextBox and writing the document text into it, then you could make changes and then on a 'onFocus' Event on the WebBrowser you could have it write the edited text back to the browsers html code.
(I have done the second way before out of curiosity and it's obviously much simpler than the other way that i have suggested)
-
But with the TreeView :
You can dynamically add Nodes to that object.
Roots are 'Base' Nodes (Parents) and the and the Child, well, is obviously the Child of the Root Node.
(A node consisting of an actual name to call it by, a display name (text), and a tag for any extra info.)
But anyways, the TreeView would be the most work to do.
A few other things potentially worth your time to look at, if you chose to do either option, I've written below in a comment.

How to test google search box (software testing)

Today I got this from an interviewer. I am not sure if anyone cell tell me what should be the right way. Thanks
Below is the question
Consider www.google.com's search box and the Google Search button.
Q: How would you test this functionality?
How would you design test automation for 1) above?
When you're testing something, you want to find the points where the function, webpage, application, etc. break. It's unlikely that the interviewer is asking you to test search box portion of the Google homepage itself, as that is fairly simple (it's little more than a text input and a couple submit inputs inside a form), but rather find potential edge cases in the code which takes that input and produces the search results.
So, consider edge cases for a text input and a submit button in a form using a GET action:
What happens if you submit the form with no input?
What happens if you submit the form with too much input? (Both server and browser may impose limits on the URL generated by the GET, and those limits may be different.)
What happens if you include characters which have special meaning in a GET query string such as & and =?
What happens if you circumvent the page entirely, creating a different <form> which submits to the same page as Google's search box? What if you do this with some input values missing?

Selenium IDE wait for button to be enabled

I'm testing a reasonably complex web application. I can get selenium to get things to the point where a button appears on the screen.
I want selenium to click that button after it appears. But it's not enough to use waitForElementPresent because when the button appears, it is disabled for a split second.
I need selenium to actually wait until the button is clickable before trying to click it.
I'm having no luck even trying to find out if this is possible within the IDE.
Thanks for any assistance!
It's worth re-iterating, right now I am using the IDE only. For now....
I had the same issue with Selenium IDE. I was looking for an equivalent to the ExpectedConditions.elementToBeClickable method in Selenium.
Using Selenium IDE, the following seems to work for testing an form submit input which is disabled using the disabled attribute. YMMV. Adapted as needed.
Add this Selenium IDE command before the click/clickAndWait/submit/submitAndWait command:
Command: waitForCssCount
Target: #submit-button[disabled="disabled"]
Value: 0
This css selector matches an element with id submit-button which has a disabled attribute set to 'disabled'. This command will wait until there 0 occurrences, i.e. the button is no longer disabled.
There is also a waitForXpathCount command available if you prefer to use a Xpath expression rather than a CSS selector.
Note: During testing i've noticed Selenium IDE being a little flaky and doesn't always reliably wait even with this waitForCssCount. YMMV.
I would really recommend you moving over to using webdriver 'proper', automating a complex app via just the IDE is going to cause you to end up in a mess eventually. However that is not your question and I have preached enough...
You have a few options, one might be that you get away with changing from waitForElementPresent to waitForVisible as element present just checks that the element exists on the page.
The next simplest change of that does not work is to hard code a wait into your script, hard coded waits are typically poor practice but you may get away with it if this is just a quick and dirty thing, just use the pause command (remember this takes time in milliseconds not seconds).
The last option is to use the more advanced feature waitForCondition which takes in a piece of javascript to evaluate, with this can you do extra checks on the element in question that can check for any property that identified it as ready to click.
I have seen that there is a waitForVisible command. You might want to try it.
Waiting for the DOM to load the element (waitForElementPresent) and the loaded element actually becoming visible (waitForVisible) could be two different things.
You could just use waitForElementNotPresent and give the button CSS with the disabled attribute. If the disabled attribute does not exist, the button is active, so there you have your code.
Here is the example that I used for Selenium IDE:
Command | Target | Value
waitForElementNotPresent | css= input[disabled=""]|
Your CSS can differ from your code, like having disabled="disabled" as a state, but the principle remains the same.
Using a CSS selector, I was able to use the not pseudo-selector in combination with wait for element visible.
wait for element visible
css=.btn.btn-primary:not([disabled=disabled])

How to control or edit text that is copied from a web page (for good, not evil reasons)

My web application allows members of the university to lookup and display their university ID number. It displays that number in an agreed upon format 99999-9999 which was picked to help distinguish it from, say, a Social Security Number, and to make it a bit more readable and easier to memorize. Unfortunately there are a few downstream applications that, for whatever reason, do not cope with the dash and expect a 9 digit integer. This becomes a problem when users, quite reasonably, use cut and paste to capture their ID number from my app and plug it into the afore mentioned brain dead app. I am unable to fix, nor can I apply pressure to have fixed, the brain dead apps, while I am being pressured to remove the hyphen. But before I do that, is there some straightforward, reliable technique that would somehow allow the user to see the ID number with the hyphen, but cause the number without the hyphen to be captured with a browser copy/cut operation?
Of course you can, now are you wanting to copy it from your web browser control or from an external internet explorer window? If external IE window, then you will need to get a reference to this window, which can be done using the windows title, then using that reference (or, if in a web browser control, directly using your web browser control), you can search for the element where that id resides in either by tag / element name, or tag / element id, or other means (ie: you will need to figure out if its in a div tag or span tag etc)... Once you find it, get a reference to it, probably using IHTMLElement to declare your reference type... And you'll be able to either copy it and do some string manipulation on it and store it wherever you want, or you can just update the live value on the page with the new value (live editing on the page the user is seeing, to change the elements value by copying id, removing hyphen from copy in a string variable, and then assigning it back to the element we got it from).
You can use the elements .value property to read from and assign to the element, depending on the element, but most elements use .value or innertext, once again, depending on the tag/element :).
Let me know if u need some code you will need to tell us what element we are dealing with, or the HTML sample of what surrounds the student Id you want to play with.

Selenium Automated Testing and java textboxes

Hey Ive tried looking for how to solve this for a couple hours and I keep coming up blank.
Im using Selenium IDE to try and make a few simple Automated tests (basically the test will access the site, create content and submit it, to ensure that it still works)
But there is a part of the site that is a javascript text box and i need to enter text into it.
However Selenium doesn't recognize any actions I make inside the text editing section of the box. It will recognize if I press something like BOLD or ITALICS.
When I select BOLD it returns: click | //a[#id='mce_editor_1_bold']/img |
But like i said, it wont recognize when I select the text editing part of the box.
I tried using xpath (and firebug) after some research online, but I cant seem to make that work.
Anybody have help or ideas to offer?
You can also use javascript to work with Selenium. So for example, if your rich text box has id="RichText" you can obtain the element via javascript and input your value there.
The code looks something like this:
| verifyEval | javascript{this.browserbot.getCurrentWindow().document.getElementById('RichText').value = 'Foo Bar'} ||
However, I've found that the syntax can vary for the this.browserbot portion. It will take a bit of work to get things going. You can also checkout this article from The Automated Tester which has information on what you're trying to do.
Also this question should help you out further: Selenium: How do I use javascript to clear a value from a form field?
I would recommend persevering with Firebug. Open the page that your testing and use Firebug's 'Inspect' feature to locate the text box in the HTML source. Then you should be able to find a way to locate the box using id, xpath, or css.
For example:
If it has id='textBox' you can locate it using id=textbox.
If it has class='jsTextBox' you can locate it using css=.jsTextBox
Otherwise you can use XPath - I'd recommend getting the XPath Checker extension so you can right click the text box and find out the XPath. You might want to play around with the result to make it a smarter XPath though.
Once you've found out the location you should be able to input data using the type or typeKeys commands. type changes the value, and typeKeys fires key up/down for each character - it's slower but sometimes needed depending on the application under test.
type | id=textBox | myValue
verifyValue | id=textBox | myValue
Hope this helps,
Dave.