How to capture an injected value in RF using Javascript

I am not sure if this is the correct area for this post , so let me know if I need to move it. I am using RF for testing, Selenium Library and when needed Javascript ( I am beginning to learn).

My goal is to compare two date values. One value is Current Date and the other value is a date that is injected into the DOM when user changes a value. We are using angular for UI. The current code fails because it is comparing current date to //input[@ng-reflect-value which produces an end result of NULL so the method fails.

I do not have to use Javascript but it seems that was the best solution.

Work Date should be today

Click Element    ${WORKDATE}
${date}=         Get Current Date
${workdate}=     Execute Javascript  document.getElementById("class")   **//input[@ng-reflect-value]**;
Should Be Equal   ${date}   ${workdate}

The date underlined below is not static and will change when a user changes another elements (Work Date) value in the UI. I do not want to capture the time, just the date.

Error Message

too drunk to debug anything like this at the moment but …

how about adding “return” to to execute javascript like like:

${workdate}=     Execute Javascript  return document.getElementById("class")   **//input[@ng-reflect-value]**;

that might help…

No. I get the same results. I believe I need to do something in this part of the code so that I can capture the value.

//input[@ng-reflect-value]**;

Hi Barry,

Not being familiar with ng-reflect-value or any ng-reflect type attributes, I duck duck go’ed it (google’d it) to find out what the html element you are hitting would look like, I found an example but also noted there was a warning:

FYI you should not rely on ng-reflect-* since angular add these attributes only for debugging purpose

Based on your test case and the html from the link above, I think what you want is Get Element Attribute

Probably something like this:

${workdate}=     Get Element Attribute     //input[@ng-reflect-value]     ng-reflect-value

I think the problem you’re encountering is that document.getElementById is returning a dom object to the browser and then the browser not being able to return an object and not having a default formatter is returning None rather than a string representation of the object.

You Should Be Equal is failing as expected as ${workdate} is None which isn’t a date or formatted date string.

Hope this is helpful,

Dave.

I will test your suggestion. I do have a related issue that I hope someone can assist, which is why I am using this same post.

In order to get a current date without time I decided to use this code:
Execute Javascript new Date().toLocaleDateString()

When the method is run in console, I get a correct return:
image

I have two issues to resolve: I want to store the returned date in a variable but have not figured out how to do this and when this method is run through Robot Framework the return is NONE and not the expected output of the date as seen in the above image.

Log file:

Hi Barry,

I gather this question relates to the ${date}= Get Current Date line from your original question?

It looks like you are missing the return for the javascript:

${mydate}=    Execute Javascript     return New Date().toLocalDateString()

I would suggest you stick with robot framework to create the date that you want to compare against, it’ll probably be a little faster as it only requires builtin keywords, plus you can then reuse the keyword on other projects that don’t use selenium

Something like this:

*** Keywords ***
Get Current Date
	@{darr}=	Get Time	year month day
	${date}= 	Set Variable    ${darr}[2]/${darr}[1]/${darr}[0]
	[Return] 	${date}

Hope that helps,

Dave.

1 Like

This is perfect and you are correct I can repurpose this across my project.

This resolved the first part of my question, which works well with the code you provided for the solution I desired.

${workdate}= Get Element Attribute //input[@ng-reflect-value] ng-reflect-value
${newworkdate}= Convert Date ${workdate} result_format=%Y/%m/%d