Closed Bug 1722021 Opened 4 years ago Closed 4 years ago

textarea.SendKeys(Keys.Home) not working correctly

Categories

(Remote Protocol :: Marionette, defect)

Firefox 90
defect

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 1529540

People

(Reporter: gee_jay_, Unassigned)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0

Steps to reproduce:

I want to modify some text in a textarea by means of a Selenium4/C# test.
Initially, the textarea contains the text "two three four".
The test should change it into "one two three four five" (using the Home and End keys to prepend "one" and append "five").
The test works fine in Edge and Chrome.

== Selenium C# code: =======================
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;

namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
private void PerformTest(WebDriver driver)
{
driver.Navigate().GoToUrl("file:///c:/temp/index.html");

        var textarea = driver.FindElement(By.TagName("textarea"));
        textarea.Click();
        textarea.SendKeys(Keys.End);
        textarea.SendKeys(" five");
        textarea.SendKeys(Keys.Home);
        textarea.SendKeys("one ");

        Assert.AreEqual(textarea.GetAttribute("value"), "one two three four five");

        driver.Close();
    }

    [TestMethod]
    public void TestFirefox()
    {
        FirefoxConfig config = new FirefoxConfig();
        new DriverManager().SetUpDriver(config);
        FirefoxDriver driver = new FirefoxDriver();

        PerformTest(driver);
    }

    [TestMethod]
    public void TestChrome()
    {
        ChromeConfig config = new ChromeConfig();
        new DriverManager().SetUpDriver(config);
        ChromeDriver driver = new ChromeDriver();

        PerformTest(driver);
    }
}

}

== index.html ==============================
<html>
<body>
<textarea>two three four</textarea>
</body>
</html>

Actual results:

The result becomes "two three four fiveone "
The text entered, after the Home key is pressed, doesn't seem to be inserted correctly

Expected results:

The textarea should contain "one two three four five"

Thank you for the report. Do those special keys work when you also have the Shift key pressed? In that case it's a dupe of bug 1529540.

It would be great if you could send a trace level log.

Flags: needinfo?(gee_jay_)
Attached file trace-1722021.log

bug 1529540 is indeed similar.
When textarea.SendKeys(Keys.Home); is executed, the cursor points at the beginning of the line (which is correct).
However, the next statement (textarea.SendKeys("one ");), moves the cursor back to the end of the line and appends the "one " text from there.

If I press the Shift key, my test is different: all the available text ("two three four five") is deleted and replace by "one "

If I replace the Home keypress by multiple LeftArrow keypresses, the "one " text is inserted at the correct position... except when the position is at the very beginning.
For example:
// textarea.SendKeys(Keys.Home); for (int times = 0; times < "two three four five".Length - 1; times++) { textarea.SendKeys(Keys.Left); } textarea.SendKeys("one ");
will change the text into "tone wo three four five" (which is correct)

The following code
// textarea.SendKeys(Keys.Home); for (int times = 0; times < "two three four five".Length; times++) { textarea.SendKeys(Keys.Left); } textarea.SendKeys("one ");
will change the text in "two three four fiveone " (which is incorrect)

Also, if I change the text in the textarea into 2 lines of text and I simulate a Home keypress on the last line, the cursor moves to the beginning of the 2nd line.
The text inserted after this statement, is inserted at the correct position.

It seems that the problem only occurs when the cursor is at the very beginning of the textarea.
I have attached a tracelog for the original issue.

Flags: needinfo?(gee_jay_)

Thanks for the added information!

If I press the Shift key, my test is different: all the available text ("two three four five") is deleted and replace by "one "

From what I can tell, pressing shift+Home should indeed select text, so when you send "one " afterwards, it will replace the existing text. However the fact that the whole text was replaced indicates that shift+Home worked properly.

It's very interesting to see that you get a similar bug by simply simulating an extra Left key when the cursor is at the beginning of the input.
It's probably a duplicate of bug 1529540, so let's close this one. But I'll summarize the info you provided on the other bug.

Status: UNCONFIRMED → RESOLVED
Closed: 4 years ago
Resolution: --- → DUPLICATE
Product: Testing → Remote Protocol
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: