Testing the validation of a text box using Selenium - selenium

I am trying to test a webpage using Selenium and NUnit. One of my test cases entails the validation of text boxes. Using Selenium and C#, I am able to retrieve the value entered in the text box. But when the validation of the text box fails, an error message is displayed next to the text box.
So, here are my questions:
1. How can I test if an error was raised due to validation failure.
2. Can I get the text of that error.
3. Or, am I way off the mark and what I am trying to do is not at all possible.
I have tried reading the value of the element, but it always seems to be an empty string.
Say, for example, I am trying to test the webpage https://edit.yahoo.com/registration . When I enter "**myname&&" in the First Name field, an error appears stating "Only letters, spaces, hyphens, and apostrophes are allowed". I want to be able to test that this error was raised.
Also, I noticed that when Selenium opens the webpage and enters an incorrect value in the text box, the error message does not get displayed next to this text box. Whereas, when I open the webpage myself and enter an incorrect text, the error message is displayed
Thanks!!

You will have to use thread.sleep, but in a better way. It's better to write a function like this (I am writing this in JAVA, you should be able to write it for C#). This method will wait for the specified number of seconds for the element to be visible. If the element is not visible even after the specified number of seconds, then the method will return false. If it becomes visible then the method will return true.
Alternatively, you can use an assertion instead of returning a false condition so that your test fails.
public boolean waitForErrorMessage(String elementToWaitFor, int waitTimeInSeconds)
{
int timeOut=0;
while(!selenium.isVisible(elementToWaitFor))
{
if(timeOut<waitTimeInSeconds){
#sleep for one second
Thread.Sleep(1000);
}
else {
return false;
}
timeOut=timeOut+1;
}
return true;
}

Related

Check if an input field is empty or not is not working properly in Cypress tests

I got 2 step definitions in Cypress that check if an input field is empty or not (depends on how I build the sentence I setup with RegEx).
First my problem was, that cypress said the test failed because the input field is empty while it was not.
My defined steps:
/** We check if the input field with the given name is empty */
Given(/^The input field "(.*)" is (not )?empty$/, (inputFieldName, negation) => {
if (negation === 'not ') {
CypressTools.getByName(inputFieldName).should('not.be.empty');
} else {
CypressTools.getByName(inputFieldName).should('be.empty');
}
});
/** We check if the input field with the given name is visible and empty */
Given(/^The input field "(.*)" is visible and empty$/, (inputFieldName) => {
CypressTools.getByName(inputFieldName).should('be.visible').should('be.empty');
});
In my specific test cypress should check a value filled input field and the step is defined like that:
The input field "XYZ" is not empty
I can see, that the if-condition is working fine, so no problems on the definition or RegEx site.
But the test fails because Cypress say the input field is empty but it's not.
I suspect, that Cypress test the input fields for values between the input tags, but doesn't check for a value attribute in the input tag.
At least, I tried to add an invoke('val') in the step definition:
CypressTools.getByName(inputFieldName).invoke('val').should('not.be.empty');
and it works for the first step definition, but when I do that for the 2nd one aswell, cypress tests fail and tell me this:
Timed out retrying: You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object.
The chai-jQuery assertion you used was:
> visible
The invalid subject you asserted on was:
>
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.
I don't understand the problem here. Is this method valid with invoke() or is there a better solution to cover all cases?
Thanks a lot for your help.
The problem your error message is pointing to is that the subject being passed along the command chain in not appropriate for the next step,
CypressTools.getByName(inputFieldName)
.invoke('val') // changes subject to the text of the input
// (not a DOM element)
.should('be.visible') // needs a DOM element
.should('not.be.empty');
The surest way around it is to break the testing into two steps
CypressTools.getByName(inputFieldName).should('be.visible');
CypressTools.getByName(inputFieldName)
.invoke('val')
.should('not.be.empty');
but I think a simple reordering will also work
CypressTools.getByName(inputFieldName)
.should('be.visible') // check the DOM element, passes it on as subject
.invoke('val') // changes subject to the text of the input
.should('not.be.empty'); // check the text is not empty

How to input the value of a hidden field into a textbox with Test Cafe Studio?

I am attempting to input the value of a hidden field into a textbox in Testcafe, ideally in some sort of manner that simulates typing. Is there a way to do that? Every time I try to do it via javascript it just throws a javascript error.
Essentially I am testing a pretty standard web app - I fill out a form, go page to page, and then must type in a value that is kept in a hidden html input field on the page. I honestly have no idea where to start - every time I've tried to do this with javascript via the "Run Test Cafe Script" it has thrown a javascript error - I really don't know where to start if javascript can't be used.
TestCafe cannot type text in a zero-size input element. I suggest you try the Run TestCafe Script action with ClientFunction that puts a value to the input element directly:
const setValue = ClientFunction(() => {
document.querySelector('input[type="hidden"]').value = 'John Smith';
});
await setValue();

Can't pass a Test Execution result to a variable in Robo Framework

I am posting the results of automated tests to an offline forum. It would be nice to include PASS/FAIL in the forum post title but I'm having some difficulties retrieving the ${TEST STATUS} value - (obviously a hard-coded value works fine) .
I've defined the following in common-variables.robot as:
${FORUM_TEST_RESULT}....${TEST STATUS}
then on publish-results.robot
Input Text....//*[#id="title"]....${FORUM_TEST_RESULT}
The error I get is: variable ${FORUM_TEST_RESULT} not found
I can see here: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-interface that ${TEST STATUS} can only be used as part of Teardown.
I'm not sure how to collect the value of ${TEST STATUS} in the context of my RF script.
e.g the very last thing my script does is post to a forum:
Input Text....//*[#id="title"]....${FORUM_TEST_RESULT}
but before that I obviously need to populate ${FORUM_TEST_RESULT} with the value of ${TEST STATUS) which you can only get on Teardown? Hope this makes sense.
Input Text is a keyword of Selenium2Library that types the given text into the text field of a web page. You need to start a browser session first and open the right page an then possibly wait for the element to become visible, for example like this:
Open Browser [URL of your site]
Wait Until Element Is Visible //*[#id="title"]
Input Text //*[#id="title"] ${FORUM_TEST_RESULT}
If you want to retrieve a text from a page (as your coment suggests) then you need to use the keyword Get Text which returns the text of the element identified by locator.
Get Text locator

Protractor sendKeys issue with scripted input fields

I'm automating e2e tests with Protractor on an angular app.
However, I have an issue when sending keys on input fields.
The sendKeys would miss few characters everytime so I found a workaround :
static sendKeys(value, element){
value.split('').forEach((c) => element.sendKeys(c));
}
This works well but it takes more than 3 times the time the original sendKeys function would.
Well no problem my tests are still functionnal right ?
My app now has new fields with scripts behind them.
One of them is a datepicker input, you can either choose from the datePicker or type it manually. However, for today's date you would type 09022018 and the slashes are automatically appended at the right place (like so 09/02/2018). If you were to enter a wrong date the field is cleared.
Now back to the problem : it seems that both my implementation of sendKeys and the original one loose focus after each submitted key. This means that I can't enter a valid date in the input field as it's cleared after each simulated keypress.
I could use browser.executeScript to fix it but I wouldn't be able to test the functionnality adding slashes. Also, as you type, the datepicker is still open and refreshes after each keypress, you can select a date from it at any time and that is also a feature I want to test.
Thanks in advance
Use executeScript to set the date in backgrond, then use sendKeys to enter a space or Tab at the end to trigger the Keyborad event which will check the input and format the input with slash
function enterDate(date) {
var script = 'arguments[0].value=arguments[1]';
// input box for date
var dateBox = element(by.xxx(yyy));
browser.executeScript(script, dateBox, date);
dateBox.sendKeys(" ");
// or try send Tab
dateBox.sendKeys(protractor.Key.TAB);
}
enterDate('09022018');
You can try this solution on other fields you fixed but take 3 more time.

How to create Test cases in Automation tool TestComplete

How can I create test cases according to my requirement.
Example:
I have a form with many fields. There is one field name Father's Name, now I want that the user should insert only string in this field, no numeric values should be accepted.
I wanna carry out such cases and do testing using the tool. How can I do this in TestComplete?
So, you want to validate that the tested application correctly handles the situation when forbidden characters are entered in the field, right? If so, then the exact solution depends on what the application does when a forbidden character is entered:
1) The app shows an error box. In this case, make your test enter a forbidden char and check for the error box existence using the appropriate Wait* method (WaitWindow, WaitNamedChild, etc.). Short example from the top of my head (did not run the code):
var TextToEnter="First 123Name";
EditBox.Keys(TextToEnter);
// As a rule, validationg is performed when the focus changes
EditBox.Keys("[Tab]");
var ErrorBox = MainWnd.WaitNamedChild("wndErrorDlg", 5000);
if (ErrorBox.Exists)
Log.Message("Succeeded - the error box is shown");
else
Log.Error("Failed - no error box detected");
2) The app does not show any error, but just ignores the forbidden chars making them not to appear in the edit box. In this case, just compare the actual text against the expected text. Something like this:
var TextToEnter="First 123Name";
var TextToExpect="First Name";
EditBox.Keys(TextToEnter);
if (EditBox.wText == TextToExpect)
Log.Message("Succeeded");
else
Log.Error("Failed");
I hope this helps.