【问题标题】:calling js file function in selenium ide在 selenium ide 中调用 js 文件函数
【发布时间】:2013-02-13 15:43:47
【问题描述】:

公司刚刚开始使用带有 Firefox 的 Selenium IDE 1.10。在某些网页中,有一个用户名和密码以及允许进入的输入按钮。我找到了一个允许使用这个 .js 文件生成随机文本的代码。我如何从这个 .JS 文件调用这个函数来填充这个文本框并输入一个虚构的密码。这只是 Web 测试中的一个非常初步的阶段,以查看某些功能是否有效。建议?如何从 IDE 本身调用此函数,而无需经过密集的 Java 脚本培训。公司目前不希望仅使用 1.10 IDE 本身和一些简单的 javascript 来填充字段(如果可能)。

    // Generate random text for a variable
// Possible options:
//   length      number indicating how long to make the string (defaults to 8)
//
//   type        string indicating what type of string to create alpha, numeric
//               or alphanumeric (defaults to alphanumeric)
//
//   length|type pipe delimited option list

Selenium.prototype.doRandomString = function( options, varName ) {

    var length = 8;
    var type   = 'alphanumeric';

    var o = options.split( '|' );

    for ( var i = 0 ; i < 2 ; i ++ ) {
        if ( o[i] && o[i].match( /^\d+$/ ) )
            length = o[i];

        if ( o[i] && o[i].match( /^(?:alpha)?(?:numeric)?$/ ) )
            type = o[i];
    }

    switch( type ) {
        case 'alpha'        : storedVars[ varName ] = randomAlpha( length ); break;
        case 'numeric'      : storedVars[ varName ] = randomNumeric( length ); break;
        case 'alphanumeric' : storedVars[ varName ] = randomAlphaNumeric( length ); break;
        default             : storedVars[ varName ] = randomAlphaNumeric( length );
    };
};

function randomNumeric ( length ) {
    return generateRandomString( length, '0123456789'.split( '' ) );
}

function randomAlpha ( length ) {
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alpha );
}

function randomAlphaNumeric ( length ) {
    var alphanumeric = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alphanumeric );
}

function generateRandomString( length, chars ) {
    var string = '';
    for ( var i = 0 ; i < length ; i++ )
        string += chars[ Math.floor( Math.random() * chars.length ) ];
    return string;
}

【问题讨论】:

    标签: javascript selenium


    【解决方案1】:

    打开一个记事本并将您的 javascript 代码放入其中,然后保存记事本的 userextension.js 文件

    打开 selenium IDE > 选项 > 常规选项卡。浏览 selenium 核心扩展中保存的 userextension.js 文件,然后关闭 selenium 并重新启动。然后是行动

    命令:随机字符串

    目标:6

    值:文本

    命令:类型

    Target::id=文本框的id

    值:${文本}

    这里6是要生成的字符串的长度

    text 是存储生成字符串的变量

    id=文本框的id是要放置生成的字符串的地方

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 2018-08-14
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多