Selenium:如何输入空格作为文本字段的值?

发布于 2024-09-02 21:46:46 字数 527 浏览 5 评论 0原文

我有一个表单文本字段,其中填充了默认值。我想清除该值并输入空格以断言发生了预期的验证。我正在使用 Selenium RC 和 PHPUnit Selenium 扩展。我怎样才能实现这个目标?

注意:我已经尝试过执行这些操作,但它们不起作用:

$this->type('FirstName', "  "); //spaces
$this->type('FirstName', "\t"); //tab character

它们清除字段中的值,但不会在字段中输入空格。

更新:我发现如果您使用Selenium在 IDE 中,您可以使用 ${space} 变量 键入空格,但这在使用 Selenium RC 和 PHPUnit Selenium 扩展时不起作用。

I have a form text field that is being populated with a default value. I would like to clear the value and enter white space to assert that the expected validation occurs. I am using Selenium RC and the PHPUnit Selenium extension. How can I achieve this?

Note: I have already tried doing these, but they do not work:

$this->type('FirstName', "  "); //spaces
$this->type('FirstName', "\t"); //tab character

They clear the value from the field, but they do not enter the white space into the field.

Update: I found that if you are using Selenium IDE, you can type white space by using their ${space} variable but this does not work when using Selenium RC and the PHPUnit Selenium extension.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无力看清 2024-09-09 21:46:47

您可以尝试 selenium.typeKeys('FirstName'," ");
请告诉我是否有效。

You can try selenium.typeKeys('FirstName'," ");
Please let me know if it worked.

枯寂 2024-09-09 21:46:47

这应该适用于带有 Selenium 的 phpunit:

class MyTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public function testSomething()
    {
        $textField = $this->byCssSelector('.blah');

        // ...

        $textField->clear();
        $textField->value(" ");

        // test assertion to follow
    }
}

如果您想输入一个句子,则以下内容将不起作用:

$textField->value("this is a sentence.");

以下内容将:

$textField->value("this");
$textField->value(" ");
$textField->value("is");
$textField->value(" ");
$textField->value("a");
$textField->value(" ");
$textField->value("sentence.");

This should work for phpunit with Selenium:

class MyTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public function testSomething()
    {
        $textField = $this->byCssSelector('.blah');

        // ...

        $textField->clear();
        $textField->value(" ");

        // test assertion to follow
    }
}

If you want to enter a sentence, the following won't work:

$textField->value("this is a sentence.");

The following will:

$textField->value("this");
$textField->value(" ");
$textField->value("is");
$textField->value(" ");
$textField->value("a");
$textField->value(" ");
$textField->value("sentence.");
海风掠过北极光 2024-09-09 21:46:46

最近我遇到了和 Andrew 一样的问题,${space} 在 Selenium RC 中不起作用。我发现 Zugwalt 的解决方案很有用。这是我为捕捉空白所做的事情。

selenium.focus("txt_textbox1");
selenium.keyPressNative("32");
strOutput = selenium.getEval("this.browserbot.findElement('txt_textbox1').value = ' ';");

希望这有帮助;)

谢谢@!

recently I am having the same problem just like Andrew where the ${space} isn't work in Selenium RC. I found out that Zugwalt's solution is useful. Here is what I did to capture the whitespace.

selenium.focus("txt_textbox1");
selenium.keyPressNative("32");
strOutput = selenium.getEval("this.browserbot.findElement('txt_textbox1').value = ' ';");

Hope this help ;)

THanks @!

年华零落成诗 2024-09-09 21:46:46

我在 github 上的 phpunit-selenium 代码中修复了这个问题,并向维护者提出了拉取请求。

如果有人关心的话,这是我的要点:

https://github.com/undernewmanagement/phpunit-selenium /提交/1b783ba8cfe4736f525b316a75a991e0a701afd1

I fixed this in the phpunit-selenium code on github and made a pull request to the maintainer.

Here is my gist if anyone cares:

https://github.com/undernewmanagement/phpunit-selenium/commit/1b783ba8cfe4736f525b316a75a991e0a701afd1

家住魔仙堡 2024-09-09 21:46:46

您可以使用 javascript 注入并直接操作该值,

selenium.getEval("this.browserbot.findElement('FirstName').value = ' ';");

请注意,这不会触发任何事件,因此您必须根据需要手动触发它们。

You could use javascript injection and manipulate the value directly

selenium.getEval("this.browserbot.findElement('FirstName').value = ' ';");

Note this will not fire off any events so you would have to manually trigger them if desired.

╰沐子 2024-09-09 21:46:46

将其值设置为空字符串 ('') 怎么样?

How about setting it's value to an empty string ('')?

不寐倦长更 2024-09-09 21:46:46

使用 keyPress() 函数“按下”空格键怎么样?

How about using the keyPress() function to "press" the spacebar?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文