开发者

Selenium: How to enter white space as the value of a text field?

开发者 https://www.devze.com 2022-12-31 22:32 出处:网络
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 t

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 开发者_如何学Gocharacter

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.


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 @!


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


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.


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


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


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


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.");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号