Webdriver JS - 将sendKeys存储到变量并重用

时间:2016-08-01 21:31:54

标签: javascript node.js selenium webdriverjs

目前我有一个包含10个字段的表单,我需要做sendkeys>存储值并在保存表单后断言此值。对于这些字段中的每一个,我需要创建一个函数并将值存储在变量中,还是有更好的方法?

我的实际代码:

var email = driver.findElement(By.name('email'));
email.sendKeys('info@domain.com');
email.getAttribute("value").then(function(email_text) {
    var email = email_text;
});

干杯, 圣拉斐尔

2 个答案:

答案 0 :(得分:0)

如果我理解正确,过程看起来应该填写一些字段,记住它们的值并在提交表单后检查值。

对于这样的任务,没有一个标准的决定,这取决于开发人员。

因此,我们知道我们需要哪些值,并可以将其存储在例如map

{
'email':'example@email.com',
'telephone':111222333
}

Key是查找元素的名称,值 - 用于sendKey和checkValue方法。

您应该编写两个方法,这些方法将与测试数据映射一起使用,并将按地图键填充输入并按周期检查值。

答案 1 :(得分:0)

你的意思是你想把它作为数组吗?

// you can represent each field as an object
var fields = [
    { elementName: 'email',    expectedText: 'info@domain.com' },
    { elementName: 'password', expectedText: 'bla bla bla' }
];

// sendKeys to each field with the specified text
fields.forEach(function(field) {
    browser.driver.findElement(by.name(field.elementName)).sendKeys(field.expectedText);
});

// to get all the field text (from promises) and store it as an array
browser.controlFlow().execute(function() {
    var textArray = [];
    fields.forEach(function(field) {
        browser.driver.findElement(by.name(field.elementName)).getAttribute('value').then(function(actualText) {
            textArray.push({elementName: field.elementName, actualText: actualText});
        });
    });
    return textArray;
}).then(function(storedTextArray) {
    // do something with the stored text array here
});
相关问题