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

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();

Related

TestCafe does not write in text input field

I'm using TestCafe for test automation of a web application based on the Wicket framework. I try to type text into a text input field ... well, actually it is a dropdown list, where a text input field appears, so that the user can search for certain codes. The HTML fragment is as follows:
HTML fragment
And here is the corresponding screenshot (text field above "001"):
Text input field with dropdown
The user can type some characters and the list below is automatically filtered (I did this manually):
Text input field with some text
My TestCafe test tries this:
.click( productcodeList )
.expect( productcodeInputField.visible ).ok()
.click( productcodeInputField )
.typeText( productcodeInputField, 'ABW' )
i.e.
Click on the drop down list.
Assume that the text input field is now visible (works fine).
Click on the text input field (this should not be necessary, since typeText() is supposed to do this anyway).
Type the text "ABW" into the text input field ==> This does not work.
I'm sure that my Selector works, since the assertion (expect) is successful and when I debug the test run after the second click (on the text input field), I see the following:
TestCafe screenshot
I.e. the cursor is directly on the text field, but somehow TestCafe cannot write the text into the field.
Some additional information: The Selector for the input field is created as follows:
productcodeInputField = Selector('span').withAttribute('class', /select2-dropdown.*/ ).child('span').withAttribute('class', /select2-search.*/ ).child('input').withAttribute('class', 'select2-search__field' );
More information: I'm using the same logic on the same page:
kurzbezeichnungField = Selector('input').withAttribute('name', /.*aeAbbreviation.*/);
...
await t.click( kurzbezeichnungField )
.typeText( kurzbezeichnungField, 'xxxWWW' )
and this works fine.
Node.js version: v10.16.3
Testcafe version: 1.5.0
This issue looks like a bug. However, I cannot say it precisely without an example that demonstrates the problem.
My team would really appreciate it if you share your project or sample to demonstrate the issue.
Please create a separate issue in the TestCafe github repository using the following template and provide as much additional information as possible.

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 instantly set/send_keys a textarea in capybara

I need to instantly fill a textarea with a very long string for testing purposes.
set/send_keys simulates typing and is too slow for Sauce Labs causing time outs.
Is there a way to instantly fill a textarea in Capybara?
The only way to instantly do it would be using execute_script to set the value via JS
element = find('textarea') # however you locate the element
execute_script('arguments[0].value = arguments[1]', element, text_to_set)
Note: this won't trigger all the events a user would generate when inputting into the textarea
TL;DR: Use Javascript/JQuery (.val() function) to set the field's value via the .execute_script()/.evaluate_script() function. It will automatically send the full string. You have more details bellow.
Example:
def execute_script(script)
browser.execute(function() {
$('<yourSelectorHere>').val("blablabla");
})
nil
end
Selenium team decided a LOOOONG way back to make it work this way, because it will actually simulate the real way a user would fill that input/textarea/field/etc.
Note: I wrote the command in WebdriverIO, but now have updated to Capybara as well.
Indeed, using the .setValue()/.set(), or the .keys()/.send_keys() methods will issue a POST action on the target element (presumably an <input>) in the form of an array of characters. See example:
Command: browser.setValue('input[connectqa-input="rename-device"]','stackoverflowstackoverflowstack');
Output:
> browser.setValue('input[connectqa-input="rename-device"]','stackoverflowstackoverflowstack')
{ state: 'pending' }
> [21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/elements"
[21:52:25] DATA {"using":"css selector","value":"input[connectqa-input=\"rename-device\"]"}
[21:52:25] RESULT [{"ELEMENT":"6"}]
[21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/element/6/clear"
[21:52:25] DATA {}
[21:52:25] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/element/6/value"
[21:52:25] DATA {"value":["s","t","a","c","k","o","v","e","r","f","(21 more items)"],"text":"stackoverflowstackoverflowstack"}
One quick and easy way to escape this is to go ahead and abuse the .val() JQuery function via the .execute()/.executeScript() methods:
Command: browser.execute(function() { $('input[connectqa-input="rename-device"]').val("dwadawdawdawdawdawdwadawawdadawdawdaw"); }) (WebdriverIO)
For Capybara syntax, see this question. It covers both .execute_script() & .evaluate_script(). (I don't want to mooch-off their views). Also you should document on the methods before-hand here.
Output:
> [21:59:38] COMMAND POST "/wd/hub/session/3d830ffd-21c6-4e5f-a6b3-4f8a03821991/execute"
[21:59:38] DATA {"script":"return (function () { $('input[connectqa-input=\"rename-device\"]').val(\"dwadawdawdawdawdawdwadawawdadawdawdaw\"); }).apply(null, arguments)","args":[]}
Hope it helped!

Testing the validation of a text box using 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;
}