Repository navigation
fix(uiautomator2-server): strip trailing escaped newline in SendKeysToElement - #53
Open
hoanle0126 wants to merge 1 commit into
Open
hoanle0126 wants to merge 1 commit into
hoanle0126 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Moved from appium/appium-uiautomator2-server#800 for appium/appium-uiautomator2-server#686.
Cause
In
SendKeysToElement.setText,text.endsWith("\\n")checks for a trailing literal two-character sequence\n('\\'followed by'n'). However,text.replaceFirst("\\n$", "")passes\n$as a regular expression toString.replaceFirst, which matches an actual LF character (0x0A) rather than the literal\nsequence. As a result, the trailing\ncharacters remain intextwhenelement.setText(text)is called, whilepressEnteris also set totrue.Using
text.replaceFirst("\\\\n$", "")escapes the backslash for the regex engine so the trailing literal\nsequence matched bytext.endsWith("\\n")is stripped beforeelement.setText(text)andgetUiDevice().pressEnter()are executed.Before / After Reproduction (
appium/appium-uiautomator2-server#686via Java client)uiautomator2driver, Java client (io.appium:java-client), targetEditTextelement.text.endsWith("\\n")evaluates totrue(pressEnter = true).text.replaceFirst("\\n$", "")does not match literal\n, leavingtextas"aa\\n".element.setText("aa\\n")sets the literal textaa\ninto theEditTextfield, andgetUiDevice().pressEnter()is then invoked on top of that.text.replaceFirst("\\\\n$", "")strips the trailing literal\n, leavingtextas"aa".element.setText("aa")setsaainto theEditTextfield, andgetUiDevice().pressEnter()sends the Enter key event."aa\n") and append paths (replace=falseorreplace=null):model.textends with an actual LF ("aa\n"),text.endsWith("\\n")isfalse, sotextis passed unchanged ("aa\n") toelement.setTextwithout triggering an extragetUiDevice().pressEnter().model.replaceisfalseor omitted (null),texthas its trailing"\\n"stripped first ("aa"), is appended tocurrentText("existing_aa"), written viaelement.setText("existing_aa"), and followed bygetUiDevice().pressEnter().Unit Test Verification (Before vs. After)
Test command:
npm run test:android --workspace appium-uiautomator2-server # (runs ./gradlew :vendor-xpath2:jar && ./gradlew testServerDebugUnitTest)Before fix (
text.replaceFirst("\\n$", "")):After fix (
text.replaceFirst("\\\\n$", "")):