0

只是使用 selenium 为我填写一些表格。我需要它为我的用户名字段生成一个唯一值。我怎样才能做到这一点?

我有

命令:类型
目标: id_of_my_field
值:用户名+unique_value ???

4

4 回答 4

5

您可以使用 javascript 来做到这一点:

:javascript{'用户名'+Math.floor(Math.random()*100000)}

这将为您的用户名附加一个 6 位随机数。

有关更多详细信息和其他方法,请参阅此 SO 问题和答案...

于 2010-08-06T18:01:05.097 回答
1

我的解决方案对我很有效:

将以下文本另存为 .js 文件并添加到 Options->Options "Selenium Core Extensions" 列表...

Selenium.prototype.doTypeRandomName = function(locator) 
{
  /**
  * Sets the value of an input field to a random "name" 
  * as though you typed it in.
  */
  // All locator-strategies are automatically handled by "findElement"
  var element = this.page().findElement(locator);

  /* The following block generates a random name 8 characters in length */
  var allowedChars = "abcdefghiklmnopqrstuvwxyz";
  var stringLength = 8;
  var randomstring = '';

  for (var i=0; i<stringLength; i++) {
    var rnum = Math.floor(Math.random() * allowedChars.length);
    randomstring += allowedChars.substring(rnum,rnum+1);
  }

  // Replace the element text with the new text
  this.browserbot.replaceText(element, randomstring);
}

完成后,您只需TypeRandomName在 Selenium 中为您希望生成随机“名称”的每个文本框选择命令。

于 2012-03-13T15:57:26.513 回答
1

全局可重用解决方案的步骤如下

1) 从此处下载 sideflow.js

2)在其中添加以下行:

Selenium.prototype.doTypeRandomName = function(locator) {
    /**
     * Sets the value of an input field to a random email id,
     * as though you typed it in.
     *
     * @param locator an <a href="#locators">element locator</a>
     */

    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    /* The following block generates a random email string */
    var allowedChars = "abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    var stringLength = 8;
    var randomstring = '';

    for (var i=0; i<stringLength; i++) {
        var rnum = Math.floor(Math.random() * allowedChars.length);
        randomstring += allowedChars.substring(rnum,rnum+1);
    }

    // Replace the element text with the new text
    this.browserbot.replaceText(element, randomstring);
};

3) 保存文件

4) 转到 Selenium ide -> options -> options -> Selenium Core extensions -> 在此处提供文件参考。

5) 现在您的 randomname 函数将出现在自动智能感知中,并将作为“typerandomname”命令类别。

6) 示例用法可能是(如果基本 url 是 google.com)

<tr>
    <td>open</td>
    <td>/</td>
    <td></td>
</tr>
<tr>
    <td>typerandomname</td>
    <td>css=input[class='gbqfif']</td>
    <td></td>
</tr>

希望这可以帮助你

于 2014-09-08T07:16:14.533 回答
0

这是一种无需在代码中使用 JavaScript 即可生成唯一数字的方法:(我使用了 Java)

    DateFormat dateFormat = new SimpleDateFormat("ddHHmmss");            /* Here you create object of the class DateFormat, and SPECIFY THE FORMAT (ddHHmmss). (Don`enter code here`t forget to import class Date(ctrl+shift+o  if you are using Eclipse)) */  
    Date date = new Date();                                              /* Here you create object of the class Date. (Dont forget to import class Date. (Dont forget to import class Date(ctrl+shift+o  if you are using Eclipse)) */  
    String random_number = dateFormat.format(date);                      /* Assign your current Date(something like 19184123) (ddHHmmSS) to a local variable(it require a String) */  
    yourWebDriver().findElemnt(By.cssSelector("input#someId")).sendKeys("test"+random_number+"@tester.com");    /* send keys to your input injecting random number */ 

这种方法将给出真正唯一的数字,永远不会重复,因为它使用您当前的时间......

如果在 DateFormat 中包含英里秒数,您可以添加更多随机性

于 2013-02-20T10:00:10.457 回答