【问题标题】:Selenium: Get unique value?Selenium:获得独特的价值?
【发布时间】:2011-03-26 11:13:02
【问题描述】:

只是使用 selenium 为我填写一些表格。我需要它为我的用户名字段生成一个唯一值。我该怎么做?

我有

命令: type
目标: id_of_my_field
值: username+unique_value ???

【问题讨论】:

    标签: selenium selenium-ide


    【解决方案1】:

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

    :javascript{'username'+Math.floor(Math.random()*100000)}

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

    请参阅this SO question and answers 了解更多详情和其他方法...

    【讨论】:

    • 该死。我正在尝试javascript:。不知道{} 语法。谢谢!
    【解决方案2】:

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

    将以下 TEXT 保存为 .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);
    }
    

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

    【讨论】:

      【解决方案3】:

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

      1) 从Download here下载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>
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        这是一种无需在代码中使用 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 中包含英里秒数,您可以添加更多随机性

        【讨论】:

        • 这在连续生成数字的情况下会失败。由于缺乏精确性,即使包含更多细节(几分之一秒)也可能会失败。
        猜你喜欢
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        • 2022-01-11
        • 2020-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多