Skip to content

fix: set_value and set_text sent incorrect data#831

Merged
KazuCocoa merged 1 commit intoappium:masterfrom
eyJhb:fix-text-set
Feb 21, 2023
Merged

fix: set_value and set_text sent incorrect data#831
KazuCocoa merged 1 commit intoappium:masterfrom
eyJhb:fix-text-set

Conversation

@eyJhb
Copy link
Copy Markdown
Contributor

@eyJhb eyJhb commented Feb 17, 2023

set_text and set_value would not work, without these changes, on the latest beta56 of Appium.

These are the values that Appium corrected me with, that I have replaced in the code.

Is there any place where one would validate the parameters that should be sent? Either some documentation, or a reference library?

EDIT: is this the reference to use? https://w3c.github.io/webdriver/#element-send-keys

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Feb 17, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

set_text and set_value would not work, without these changes, on the latest beta56 of Appium.

What is the actual error you get from the server?

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 17, 2023

set_text and set_value would not work, without these changes, on the latest beta56 of Appium.

What is the actual error you get from the server?

Thanks for the quick reply, highly appreciated!
The error is the following (I've snipped out the trace):

>>> elem = driver.find_element(by=AppiumBy.IOS_CLASS_CHAIN, value='**/XCUIElementTypeTextView[`label == "‎Compose message"`]')
>>> elem.set_value("test")
....
selenium.common.exceptions.InvalidArgumentException: Message: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value"]
Stacktrace:
BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value"]
...
>>> elem.set_text("test")
...
selenium.common.exceptions.InvalidArgumentException: Message: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value"]
Stacktrace:
BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value"]
...

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 17, 2023

CLA should be fixed now.

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

I think you could just add text item there without removing the existing value and id ones to be on the safe side. We can leave a deprecation comment there though to remove them in the future.

@eyJhb eyJhb force-pushed the fix-text-set branch 2 times, most recently from 5db66bd to 57fb835 Compare February 17, 2023 12:42
@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 17, 2023

That sadly does not seem to be possible

selenium.common.exceptions.InvalidArgumentException: Message: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value","text"]
Stacktrace:
BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value","text"]

EDIT: I tried it with the following patch

From 5db66bd9e9bd747ff2a4802eec1552bc2154a64e Mon Sep 17 00:00:00 2001
From: eyJhb <eyjhbb@gmail.com>
Date: Fri, 17 Feb 2023 08:30:27 +0100
Subject: [PATCH] fix: set_value and set_text sent incorrect data

---
 appium/webdriver/webdriver.py  | 1 +
 appium/webdriver/webelement.py | 5 +++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py
index 2866b9e9..430ab17b 100644
--- a/appium/webdriver/webdriver.py
+++ b/appium/webdriver/webdriver.py
@@ -445,6 +445,7 @@ def set_value(self: T, element: MobileWebElement, value: str) -> T:
         data = {
             'id': element.id,
             'value': [value],
+            'text': value,
         }
         self.execute(Command.SET_IMMEDIATE_VALUE, data)
         return self
diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py
index 128ceb0c..a8133d5a 100644
--- a/appium/webdriver/webelement.py
+++ b/appium/webdriver/webelement.py
@@ -170,7 +170,7 @@ def set_text(self, keys: str = '') -> 'WebElement':
         Returns:
             `appium.webdriver.webelement.WebElement`
         """
-        data = {'id': self._id, 'value': [keys]}
+        data = {'id': self._id, 'value': [keys], 'text': keys}
         self._execute(Command.REPLACE_KEYS, data)
         return self
 
@@ -198,8 +198,9 @@ def set_value(self, value: str) -> 'WebElement':
             `appium.webdriver.webelement.WebElement`
         """
         data = {
             'id': self.id,
             'value': [value],
+            'text': value,
         }
         self._execute(Command.SET_IMMEDIATE_VALUE, data)
         return self

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

@eyJhb could also also provide the full server log for your patch. This seems weird to me as the validation algorithm looks fine to me:

  '/session/:sessionId/element/:elementId/value': {
    POST: {
      command: 'setValue',
      payloadParams: {
        validate: (jsonObj) =>
          !util.hasValue(jsonObj.value) &&
          !util.hasValue(jsonObj.text) &&
          'we require one of "text" or "value" params',
        optional: ['value', 'text'],
        // override the default argument constructor because of the special
        // logic here. Basically we want to accept either a value (old JSONWP)
        // or a text (new W3C) parameter, but only send one of them to the
        // command (not both). Prefer 'value' since it's more
        // backward-compatible.
        makeArgs: (jsonObj) => [jsonObj.value || jsonObj.text],
      },
    },
  },

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 17, 2023

Keep in mind, there are the two methods set_value and set_text. Here is the appium logs for both.

set_text

[HTTP] --> POST /session/4dd31bb1-4d29-4473-b665-3cde71590be7/appium/element/05000000-0000-0000-0509-000000000000/replace_value
[HTTP] {"id":"05000000-0000-0000-0509-000000000000","value":["test-text"],"text":"test-text"}
[debug] [XCUITestDriver@fa44 (4dd31bb1)] Encountered internal error running command: BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value","text"]
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at checkParams (/home/eyjhb/projects/appium-server/node_modules/@appium/base-driver/lib/protocol/protocol.js:158:9)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at asyncHandler (/home/eyjhb/projects/appium-server/node_modules/@appium/base-driver/lib/protocol/protocol.js:362:7)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at /home/eyjhb/projects/appium-server/node_modules/@appium/base-driver/lib/protocol/protocol.js:503:15
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at Layer.handle [as handle_request] (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/layer.js:95:5)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at next (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/route.js:144:13)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at Route.dispatch (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/route.js:114:3)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at Layer.handle [as handle_request] (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/layer.js:95:5)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at /home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:284:15
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at param (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:365:14)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at param (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:376:14)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at param (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:376:14)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at Function.process_params (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:421:3)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at next (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:280:10)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at logger (/home/eyjhb/projects/appium-server/node_modules/morgan/index.js:144:5)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at Layer.handle [as handle_request] (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/layer.js:95:5)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at trim_prefix (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:328:13)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at /home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:286:9
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at Function.process_params (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:346:12)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at next (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:280:10)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at /home/eyjhb/projects/appium-server/node_modules/body-parser/lib/read.js:137:5
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at invokeCallback (/home/eyjhb/projects/appium-server/node_modules/raw-body/index.js:231:16)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at done (/home/eyjhb/projects/appium-server/node_modules/raw-body/index.js:220:7)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at IncomingMessage.onEnd (/home/eyjhb/projects/appium-server/node_modules/raw-body/index.js:280:7)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at IncomingMessage.emit (node:events:513:28)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at endReadableNT (node:internal/streams/readable:1359:12)
[debug] [XCUITestDriver@fa44 (4dd31bb1)]     at processTicksAndRejections (node:internal/process/task_queues:82:21)
[debug] [W3C] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value","text"]
[HTTP] <-- POST /session/4dd31bb1-4d29-4473-b665-3cde71590be7/appium/element/05000000-0000-0000-0509-000000000000/replace_value 400 1 ms - 3361

set_value

[HTTP] --> POST /session/1337fe0b-b253-41b6-9289-eb38b8af3f33/appium/element/05000000-0000-0000-0509-000000000000/value
[HTTP] {"id":"05000000-0000-0000-0509-000000000000","value":["test-text"],"text":"test-text"}
[debug] [XCUITestDriver@05d4 (1337fe0b)] Encountered internal error running command: BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value","text"]
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at checkParams (/home/eyjhb/projects/appium-server/node_modules/@appium/base-driver/lib/protocol/protocol.js:158:9)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at asyncHandler (/home/eyjhb/projects/appium-server/node_modules/@appium/base-driver/lib/protocol/protocol.js:362:7)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at /home/eyjhb/projects/appium-server/node_modules/@appium/base-driver/lib/protocol/protocol.js:503:15
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at Layer.handle [as handle_request] (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/layer.js:95:5)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at next (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/route.js:144:13)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at Route.dispatch (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/route.js:114:3)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at Layer.handle [as handle_request] (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/layer.js:95:5)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at /home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:284:15
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at param (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:365:14)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at param (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:376:14)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at param (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:376:14)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at Function.process_params (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:421:3)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at next (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:280:10)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at logger (/home/eyjhb/projects/appium-server/node_modules/morgan/index.js:144:5)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at Layer.handle [as handle_request] (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/layer.js:95:5)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at trim_prefix (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:328:13)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at /home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:286:9
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at Function.process_params (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:346:12)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at next (/home/eyjhb/projects/appium-server/node_modules/express/lib/router/index.js:280:10)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at /home/eyjhb/projects/appium-server/node_modules/body-parser/lib/read.js:137:5
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at invokeCallback (/home/eyjhb/projects/appium-server/node_modules/raw-body/index.js:231:16)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at done (/home/eyjhb/projects/appium-server/node_modules/raw-body/index.js:220:7)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at IncomingMessage.onEnd (/home/eyjhb/projects/appium-server/node_modules/raw-body/index.js:280:7)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at IncomingMessage.emit (node:events:513:28)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at endReadableNT (node:internal/streams/readable:1359:12)
[debug] [XCUITestDriver@05d4 (1337fe0b)]     at processTicksAndRejections (node:internal/process/task_queues:82:21)
[debug] [W3C] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {"required":["text"]} and you sent ["id","value","text"]
[HTTP] <-- POST /session/1337fe0b-b253-41b6-9289-eb38b8af3f33/appium/element/05000000-0000-0000-0509-000000000000/value 400 1 ms - 3361

EDIT: I hope this was what was meant by the logs for the request. Is there anything more that I should send?

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

ah, now I see which endpoint is used. Both endpoints were deprecated and they are not recommended for use anymore:

  '/session/:sessionId/appium/element/:elementId/value': {
    POST: {
      command: 'setValueImmediate',
      payloadParams: {required: ['text']},
      deprecated: true,
    },
  },
  '/session/:sessionId/appium/element/:elementId/replace_value': {
    POST: {
      command: 'replaceValue',
      payloadParams: {required: ['text']},
      deprecated: true,
    },
  },

I think the current change is ok, however both APIs should be marked as deprecated.

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 17, 2023

Which endpoint should be used instead? Also, what is the best way to indicate the deprecation? I am unsure of the best formatting, based on the other deprecation I can see.

        warnings.warn(
            'The "launchApp" API is deprecated and will be removed in future versions. '
            'See https://github.com/appium/python-client/pull/831',
            DeprecationWarning,
        )

Or what link would make sense?

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

yes the warning above makes sense. Also the deprecation must be mentioned in the docstring.

regarding what to use instead - just a regular send_keys. Internally xcuitest driver still redirects to the same POST /value call:
https://github.com/appium/appium-xcuitest-driver/blob/166d4e3425c92b041f835df988a071e55a0f2862/lib/commands/element.js#L185

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 20, 2023

Should be fixed not, unsure about the deprecated version, so I've just put the current one :)

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 20, 2023

I am unsure how the unit tests should be fixed :)

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

I am unsure how the unit tests should be fixed :)

I think it would probably make sense to remove this test for good as we don't really need to verify deprecated entries

@KazuCocoa
Copy link
Copy Markdown
Member

assert d['value'] == [value]
should be assert d['text'] == value

@eyJhb
Copy link
Copy Markdown
Contributor Author

eyJhb commented Feb 21, 2023

assert d['value'] == [value]

should be assert d['text'] == value

Should be fixed now :) Thanks!

Copy link
Copy Markdown
Member

@KazuCocoa KazuCocoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@KazuCocoa KazuCocoa merged commit 91dc04b into appium:master Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants