Trouble setting negative value for input field

I’m trying to set a negative value on an input field that always defaults to 0. When using setText to enter 7500, it actually shows as 07500, but is validated as 7500.

For a negative value, -7500, it is actually entered as 0-7500, and gets reverted to 0.

I’ve tried multiple combinations of clearText, setText, and SendKeys to get the negative value to enter correctly and each time the default 0 gets in front and the field is validated as 0.

Anyone run into something similar and got around entering a negative value?

Hi Peter

What is the HTML used to render the input? I’m most interested in the attributes used and any attached event handlers.

How does the field behave when a value is entered by hand?

Have you tried setting the field in the browser console using JavaScript?

Hey Russ,

The HTML for the object is:

<div class="dollars-input">
<input class="form-control text-right" data-decimals="0" data-delimiter="," data-increment="10000" name="seasonalities[66da99f5-7c19-4c4e-943d-f5851a94d4b8]" value="169000.0" style="" type="text">
</div>

Additionally, there is a - and + button on the sides to increment the value by 10,000.

When I enter the value by hand, I normally do a select all, (CTRL-A), and enter -7500, which works fine. I’ve tried:

WebUI.sendKeys(findTestObject('Input'),Keys.chord(Keys.CONTROL,'a','-7500'))

and

WebUI.setText(findTestObject('Input'), "-195000")

But that doesn’t seem to work correctly. I get "Keys ‘CONTROL + a-7500’ in the Console Log, but that value doesn’t show in the field.

For both cases, my value goes behind the 0 which causes the problem.

To enter the value by hand, the 0 needs to be replaced. There is validation on the field, where if you click Tab, and the value is invalid, it will default back to 0.

I have not tried setting the field in the browser console using JavaScript.

Hey Russ,

I’m not sure I’m a fan of what I’ve done here, but taking into account your other method of using SendKeys, I’ve come up with this:



WebDriver driver = DriverFactory.getWebDriver()
Actions act = new Actions(driver)

for (loop = 1; loop <= 12; loop += 2) {
    WebUI.clearText(findTestObject('input', [('row') : loop]))
    WebUI.setText(findTestObject('input', [('row') : loop]),"7500")
    WebElement elem = driver.findElement(By.xpath("//div[@id='adjustSeasonality']/form/table/tbody/tr[" + (loop+1) +"]/td[4]/div/div[2]/input"))
    WebUI.clearText(findTestObject('input', [('row') : loop+1]))
    act.sendKeys(elem, (Keys.chord(Keys.CONTROL, "a")))
    act.sendKeys(elem, (Keys.chord("-7500")))
    act.perform()
}

After a couple of attempts, the CTRL-A and entering -7500 needed to be two lines. Putting them together didn’t seem to work correctly for me. That may have been a syntax error or perhaps they need to be separated.

Is that the correct way to send CTRL-A and then a number? That is part of the problem. I need to select the 0 in the field, then overwrite it with my actual value.

Hey Peter

Thanks for the detailed response - helps a lot.

I assume you read my response here? http://forum.katalon.com/discussion/8699/firefox-pua-character-e000-appears-in-input-field-after-using-settext-sendkeys

You could try that as a solution here.

There is validation on the field, where if you click Tab, and the value is invalid, it will default back to 0.

Right, so there is an event handler attached (either to onblur or onchange). This might prove significant at some point. But for now, one step at a time…

I have not tried setting the field in the browser console using JavaScript.

Well, let’s do that. Type these lines into the browser console. Type them one by one, hitting return after each line:

inp = document.querySelector("[name='seasonalities[66da99f5-7c19-4c4e-943d-f5851a94d4b8]']")

inp.value = "-7500"

inp.onchange()

The last line may issue an error, depending how it’s implemented. The point here, is to see if ANY code is able to correctly modify the field. I’d expect that the inp.value statement will work fine, which would lead me to suggest you use a JavaScript call to modify this particular field. (All my tests use JS APIs to modify fields).

Hey Peter

Thanks for the detailed response - helps a lot.

I assume you read my response here? http://forum.katalon.com/discussion/8699/firefox-pua-character-e000-appears-in-input-field-after-using-settext-sendkeys

You could try that as a solution here.

There is validation on the field, where if you click Tab, and the value is invalid, it will default back to 0.

Right, so there is an event handler attached (either to onblur or onchange). This might prove significant at some point. But for now, one step at a time…

I have not tried setting the field in the browser console using JavaScript.

Well, let’s do that. Type these lines into the browser console. Type them one by one, hitting return after each line:

inp = document.querySelector("[name='seasonalities[66da99f5-7c19-4c4e-943d-f5851a94d4b8]']")

inp.value = "-7500"

inp.onchange()

The last line may issue an error, depending how it’s implemented. The point here, is to see if ANY code is able to correctly modify the field. I’d expect that the inp.value statement will work fine, which would lead me to suggest you use a JavaScript call to modify this particular field. (All my tests use JS APIs to modify fields).

Strange… my last response appeared twice. Shrug…

After a couple of attempts, the CTRL-A and entering -7500 needed to be
two lines. Putting them together didn’t seem to work correctly for me.

Agreed. I’ve seen oddball things like that, too.

I need to select the 0 in the field, then overwrite it with my actual value.

In my cut down version I posted on the other thread, I actually have a forced reset of the field (in JS) setting it to “”.

The other possibility you have is to call the select method (which is what Ctrl-A is using, in effect). In the console that would be:

inp.select()

We’re going to nail this, Peter. Keep at it.

Actually, thinking on this more, this is not such a good idea (keydown and/or keypress events might be required which I think are supplied correctly by the Actions class).

The other possibility you have is to call the select method (which is what Ctrl-A is using, in effect). In the console that would be:

inp.select()

Using the console, I can set both positive and negative values without issue. After using:

inp.value="-12000"

I see -12000 in the input field.

So now you have your solution, Peter. If you were to code your test to use JavaScript, your issues would be behind you… at least, that’s what that little console test would lead me to believe.

It has to be said, WebUI.sendKeys and WebUI.setText should be reported as “broken” when trying to pass a specific but perfectly valid string to an equally valid HTML INPUT control. (I’ll leave that to you).