【问题标题】:Executing Multiple IT blocks inside a Protractor For loop for web testing在量角器 For 循环中执行多个 IT 块以进行 Web 测试
【发布时间】:2019-05-27 20:45:02
【问题描述】:

我们需要在登录网页后使用 for 循环,并在页面的 for 块内执行多​​个测试。我理想的测试场景应该像下面的sn-p。我们有一个表格,每一行都有按钮,我们将导航到该特定按钮的下一页并验证数据。目前我们将所有期望和断言都插入到一个 IT 块中,但这不是一个好的解决方案。我们需要在不同的 IT 块中拆分测试部分。

require('..\\waitAbsent.js');
require("../node_modules/jasmine-expect/index.js");
var EC = protractor.ExpectedConditions;

describe('Student Enrollment Page Content Validation', function() {


beforeAll(function () {
    browser.driver.manage().window().maximize();
    browser.get(globalVariables.loginMain);
    globalVariables.Email_Input_box.sendKeys(globalVariables.Demo_User);
    globalVariables.Password_Input_Box.sendKeys(globalVariables.Demo_PWD);
    globalVariables.Submit_Button.click();
    browser.wait(EC.invisibilityOf(globalVariables.Submit_Button), 25000, 'submit button is not disappearing yet');
});

async function Row_Numbers() {

    const RowCount = (globalVariables.tableData_Dashboard.all(by.tagName("tr")).count()).then(function(RC){

        return RC;

    });

}

for (var i = 1; i<Row_Numbers(); i++){

    function tableData(n){
        var row_1 = globalVariables.tableData_Dashboard.all(by.tagName("tr")).get(n);

        // get cell values
        var cells = row_1.all(by.tagName("td"));

        it ('should return the data fo the first cell', function(){
            var Student_ID = cells.get(0).getText().then(function (SID) {
                console.log(SID);

                return SID;

            });

            expect(Student_ID.toEqual('Something'));
        });

        it ("should show the button in this row", async function(){

            const Button = globalVariables['Edit_Button_' + n];
            // console.log(Button)
            expect(await Button.isDisplayed());
            Button.click();
            // do some thing

        });
    }tableData(i)
}

});

当我们使用这个脚本运行测试时,我们得到以下错误:

E/launcher - 等待 Protractor 与页面同步时出错:“angularJS 可测试性和 angular 可测试性都未定义。这可能是因为这是一个非 Angular 页面,也可能是因为您的测试涉及 s 客户端导航,这可能会干扰 Protractor 的引导。详情见https://github.com/angular/protractor/issues/2643

如何使用 for 循环来实现我们的目标?

【问题讨论】:

    标签: javascript jasmine protractor


    【解决方案1】:

    我有几个关于这个的问题。

    首先,您的角度错误是因为您声明的 Row_number 函数在任何 it 阻塞之外,因此在您的 beforeAll 运行之前运行。

    接下来应该不需要你的 tableData 函数,因为它似乎用循环中的 i 计数器替换它的参数 n 会产生相同的效果。

    最后,如果您的代码需要跨越多个页面来完成这些测试,那么使用数据驱动的方法并为每个测试编写单独的数据文件可能会更好。这些表行的值是变化还是一致?

    更新: 这种方法可能看起来像这样,但我没有测试过。

    beforeAll(function () {
        browser.driver.manage().window().maximize();
        browser.get(globalVariables.loginMain);
        globalVariables.Email_Input_box.sendKeys(globalVariables.Demo_User);
        globalVariables.Password_Input_Box.sendKeys(globalVariables.Demo_PWD);
        globalVariables.Submit_Button.click();
        browser.wait(EC.invisibilityOf(globalVariables.Submit_Button), 25000, 'submit button is not disappearing yet');
    });
    
    it('test it', async () => {
        globalVariables.tableData_Dashboard.all(by.tagName("tr")).forEach((row) => {
            var cells = row.all(by.tagName("td"));
    
            var Student_ID = cells.get(0).getText().then(function (SID) {
                console.log(SID);
                return SID;
            });
    
            expect(Student_ID.toEqual('Something'), 'should return the data fo the first cell');
    
            const Button = globalVariables['Edit_Button_' + n];
            // console.log(Button)
            expect(Button.isDisplayed(), 'should show the button in this row').toBe(true);
            Button.click();
            // do some thing
    
        });
    })
    

    【讨论】:

    • 表格数据是动态的..它们是通过API调用呈现的。
    • 是否正确地说您的测试应该验证特定学生 id 进入特定页面并包含有关该学生的信息?并且您想对表中的每一行都这样做
    • 是的,完全正确。在每个学生页面上,我们需要测试多个部分
    • 您将面临的挑战是量角器将处理您的代码,以确定在启动任何代码之前需要执行多少块。您正在尝试遍历行数,但我不知道在评估规范之前获取此信息的方法。我看不到使用多个它来实现这一目标的方法。如果您能找到一种方法来对 onPrepare 中填充数据的内容进行 api 调用并从中获取计数,您可以将其设置为全局变量
    • 非常感谢。我将在脚本中使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2016-08-11
    • 2015-04-09
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多