【问题标题】:Not able to setvalue to a form field in webdriverio Cucumber framework无法在 webdriverio Cucumber 框架中为表单字段设置值
【发布时间】:2020-04-17 20:36:14
【问题描述】:

我对 webdriver io 和 cucumber 框架非常陌生。尝试使用应用程序的登录屏幕进行我的第一次测试。我可以调用浏览器并加载网站。但是当我尝试为登录字段设置值时,它会引发以下错误并中止

“执行登录时出错:使用默认用户登录:当我使用默认用户登录时” browser.$(...).setValue 不是函数”

我尝试安装同步包并在配置文件中将同步设置为 true。我无法让它工作。请帮忙!

这是我的配置文件

exports.config = {

//
// ====================
// Runner Configuration
// ====================
//
// WebdriverIO allows it to run your tests in arbitrary locations (e.g. locally or
// on a remote machine).
runner: 'local',
//
path: '/wd/hub',

specs: [
    './features/*.feature'
],
// Patterns to exclude.
exclude: [
    // 'path/to/excluded/files'
],
//

maxInstances: 10,

capabilities: [{

    // maxInstances can get overwritten per capability. So if you have an in-house Selenium
    // grid with only 5 firefox instances available you can make sure that not more than
    // 5 instances get started at a time.
    maxInstances: 5,
    //
    browserName: 'firefox',
    // If outputDir is provided WebdriverIO can capture driver session logs
    // it is possible to configure which logTypes to include/exclude.
    // excludeDriverLogs: ['*'], // pass '*' to exclude all driver session logs
    // excludeDriverLogs: ['bugreport', 'server'],
}],

logLevel: 'info',
sync:true,
bail: 0,

baseUrl: '********************',
//
// Default timeout for all waitFor* commands.
waitforTimeout: 10000,
//
// Default timeout in milliseconds for request
// if browser driver or grid doesn't send response
connectionRetryTimeout: 90000,
//
// Default request retries count
connectionRetryCount: 3,

services: ['selenium-standalone'],


framework: 'cucumber', 


cucumberOpts: {
    requireModule: ['@babel/register'],// <string[]> ("extension:module") require files with the given EXTENSION after requiring MODULE (repeatable)
    require: ['./step-definitions/*.js'],       // <string[]> (file/dir) require files before executing features

    backtrace: false,   // <boolean> show full backtrace for errors
    dryRun: false,      // <boolean> invoke formatters without executing steps
    //compiler: ['js:babel-core/register'],
    failFast: false,    // <boolean> abort the run on first failure
    format: ['pretty'], // <string[]> (type[:path]) specify the output format, optionally supply PATH to redirect formatter output (repeatable)
    snippets: true,     // <boolean> hide step definition snippets for pending steps
    source: true,       // <boolean> hide source uris
    profile: [],        // <string[]> (name) specify the profile to use
    strict: false,      // <boolean> fail if there are any undefined or pending steps
    tagExpression: '',  // <string> (expression) only execute the features or scenarios with tags matching the expression
    timeout: 60000,     // <number> timeout for step definitions
    ignoreUndefinedDefinitions: false, // <boolean> Enable this config to treat undefined definitions as warnings.
},

【问题讨论】:

  • 为 Cucumber 使用的语言添加标签会很有帮助。这将使其更有可能得到回应。这看起来像javascript?我还建议发布您的实际代码。 setValue is not a function 表示您遇到的问题是您可能错误地使用了该功能或拼写错误。看到代码可以帮助某人诊断它。
  • 是的,我正在使用 node.js,webdriverio 和 cucmber 框架。
  • 看起来好多了,而且您正在收到回复!您还可以通过标签提高获得答案的机会。在这种情况下,我建议添加 Node 标签。

标签: node.js cucumber webdriver-io


【解决方案1】:

尝试执行这个简单的测试用例:

describe("webdriver.io page", () => {
it("should have the right title", () => {
    browser.url('https://www.google.com');
    browser.maximizeWindow();
    $('input[name="q"]').setValue('some text');
    });
});

您可以发布您的测试用例示例吗?

黄瓜例子:

When('user enters valid username', () => {
  loginPage.emailAddress.addValue('some text');
});

其中 loginPage 是一个 PageObject,其中包含 emailAddress 作为元素。

【讨论】:

    【解决方案2】:

    欢迎来到 Stack-overflow。您在问题中提供的相关细节越多,您就越早得到答案。

    据我所知,问题可能是以下三个中的任何一个。

    1) setValue 不适用于您的元素,因为您没有选择合适的元素。您可以尝试改进您的选择器/定位器。

    2)您可以尝试在文本区域点击,尝试使用keys api。

    3) 您不需要将path 属性设置为该值。您可以将其保留为默认 '/'。

    参考:GitHub 上的示例 repo

    【讨论】:

      【解决方案3】:

      我在创建 config.js 时添加依赖项时选择了异步模式来运行命令。我删除了工作区并重复了所有步骤。但是这次将其更改为同步模式以运行命令并再次编写相同的代码。一切正常。谢谢大家的建议。

      【讨论】:

        【解决方案4】:

        有时输入字段可能有特殊格式。就像电话号码、SSN 或出生日期的输入文本一样,开发人员设置 maskChar = ""。这可能手动工作正常,但在您使用 setValue() 时会产生问题;

        因此,快速简便的解决方案是:

        const input = await $(selector);
        await input.setValue(['Home', 'my Text']);
        

        用您的变量或常量替换“我的文本”。现在,当 WDIO 执行此行时,它将首先清除文本(作为 setValue 的一部分),然后按“Home”键,然后按您的文本。

        【讨论】:

          猜你喜欢
          • 2017-11-15
          • 2017-02-22
          • 1970-01-01
          • 1970-01-01
          • 2021-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-31
          相关资源
          最近更新 更多