2017-03-09 81 views
1

我可能有不寻常的情况,我必须从帮助器文件中的元素中获取文本,然后在spec文件中比较此文本。例如:量角器:从spec文件中的帮助器文件中使用变量

两个页面文件:

this.matchText = function(elem) { 
    return elem.getText(); 
}; 

助手文件:

 // page objects 
     var calculationPage = require('../calculation_page/calculation_page.js'); 

     // variables 
     var globalPrice = ""; 

     module.exports = exports = function() { 
      describe('... 
       it('... 
        // initialize page object 
        var calculation = new calculationPage(); 

        // store price into global variable 
        calculation.matchText(calculation.price).then(function(text) { 
         globalPrice = text; 
        }); 

        // verify price equals expected 
        expect(calculation.matchText(calculation.priceSum)).toContain(globalPrice); 

        ??? 
       }); 
      }); 
     } 

如何globalPrice存储为可能被传递到spec文件中的变量?

规格文件:

// page objects 
var homePage = require('../home_page/home_page.js'); 

// helpers 
var getPrice = require('../helpers/get_price.js'); 

describe('... 
    it('... 

     // helper - get price 
     getPrice(); 

     // initialize page object 
     var home = new homePage(); 

     // verify if price equals expected 
     expect(home.matchText(home.price)).toContain(???); 
    }); 
}); 

如何读取从规范文件的辅助文件的全局变量?

+0

为什么你'describe','it',和'在你的助手文件expect'块? – Gunderson

+0

因为目前我在一个规范中称这些为助手,但会将它们更改为spec文件。 – jurijk

回答

1

您可以将您需要的任何值全局转储到量角器全局对象 - browser

让我们说..在助手文件中,您需要存储值。然后做到这一点 - browser.globalPrice = text

然后这个值将在您的spec文件中可用。从browser对象访问它像任何其他价值expect(home.matchText(home.price)).toContain(browser.globalPrice);

请参阅我的回答@Protractor: initialise Global variable in one js and use the same var in other js

+0

谢谢!就是这个! ;)正是我在找什么。 – jurijk

+0

只是一个简单的问题;)我可以用你写的方式存储任何东西吗? – jurijk

+0

@jurijk ..欢迎光临!是的,你可以存储任何东西......复杂的对象,引用任何东西 – AdityaReddy