代码之家  ›  专栏  ›  技术社区  ›  nhrcpt

量角器将变量值显示为“未定义”

  •  1
  • nhrcpt  · 技术社区  · 7 年前

    我们从网站的一个页面上的元素定义变量,点击编辑按钮,打开下一个页面。在本页中,我们需要声明在前一页捕获的数据与第二页显示的数据匹配。我们的问题是,一旦测试移到第2页,它就无法调用我们在第1页定义的变量。下面是我们的代码片段:

         it ('Student ID Validation', function(){
    
            // get rows
            var rows = tableData_Dashboard.all(by.tagName("tr"));
    
            // get cell values
            var cells = rows.all(by.tagName("td"));
    
            var Student_ID = cells.get(0).getText().then(function(SID){
                console.log(SID);
            });
        Edit_Button_1.click();
        browser.sleep(2000);
        expect(Student_ID_on_Reg_Page.getAttribute('value')).toEqual(Student_ID);
    

     Message:
    Expected '123456' to equal undefined.
    

    我们怀疑这可能是由于异步,但事实并非如此。测试在存储了第1页的变量后移到第2页,因此我们不知道为什么会发生这种情况。我们如何解决这个问题并将变量用于断言目的?

    2 回复  |  直到 7 年前
        1
  •  3
  •   alecxe    7 年前

    问题是您已经指定了 then()

    var Student_ID = cells.get(0).getText().then(function(SID){
        console.log(SID);
    });
    

    因为什么也没有归还, Student_ID 将成为一个承诺,将解决成 undefined .

    您需要退货:

    var Student_ID = cells.get(0).getText().then(function(SID){
        console.log(SID);
        return SID;
    });
    

    var Student_ID = cells.get(0).getText();
    
        2
  •  0
  •   nhrcpt    7 年前

    .then(function(SID){ console.log(SID); });