Java脚本-发送在'it'到其他'it'

protractor java script - send var which created in 'it' to other 'it'

本文关键字:it 其他 脚本 -发 Java      更新时间:2023-09-26

我在第一个'it'函数中创建了以下2个变量:' theccurrentaccount;', 'switchToAccount;'

  var theCurrentAccount;
  var switchToAccount;
it("should find the current account", function () {
    ptor.findElements(protractor.By.xpath("...")).then(function (elements) {
        if (elements.length > 0){
            theCurrentAccount = context.switchAccount.accountName1;
            switchToAccount = context.switchAccount.accountName2;
        }
        else{
                theCurrentAccount = context.switchAccount.accountName2;
                switchToAccount = context.switchAccount.accountName1;
        }
    });
});

switchAccount(ptor, switchToAccount);

现在当我调用函数switchAccount时,它是这样定义的:

var switchAccount = function (ptor, id) {
    it("should switch account to" + "'#" + id + "'", function () {
    });
}

id未定义。我怎样才能做得正确?

首先通读并执行代码的同步部分,然后解析延迟语句。诸如findElements之类的交互将总是被延迟,但是您也可以将自己的命令插入controlFlow队列中。

var theCurrentAccount;
var theOtherAccount;
var switchAccount = function() {
    if (theCurrentAccount === 'name1') {
        theCurrentAccount = 'name2';
        theOtherAccount = 'name1';
    } else if (theCurrentAccount === 'name2') {
        theCurrentAccount = 'name1';
        theOtherAccount = 'name2';
    }
};
it("should find the current account", function () {
    expect(theCurrentAccount).toBe(undefined);
    expect(theOtherAccount).toBe(undefined);
    switchAccount();
    expect(theCurrentAccount).toBe(undefined);
    expect(theOtherAccount).toBe(undefined);
    ptor.findElements(protractor.By.xpath("...")).then(function() {
        theCurrentAccount = 'name1';
        expect(theCurrentAccount).toBe('name1');
        expect(theOtherAccount).toBe(undefined);
        switchAccount();
        expect(theCurrentAccount).toBe('name2');
        expect(theOtherAccount).toBe('name1');
    });
    expect(theCurrentAccount).toBe(undefined);
    expect(theOtherAccount).toBe(undefined);
    switchAccount();
    expect(theCurrentAccount).toBe(undefined);
    expect(theOtherAccount).toBe(undefined);
    browser.controlFlow().execute(function() {
        expect(theCurrentAccount).toBe('name2');
        expect(theOtherAccount).toBe('name1');
        switchAccount();
        expect(theCurrentAccount).toBe('name1');
        expect(theOtherAccount).toBe('name2');
    });
    expect(theCurrentAccount).toBe(undefined);
    expect(theOtherAccount).toBe(undefined);
    switchAccount();
    expect(theCurrentAccount).toBe(undefined);
    expect(theOtherAccount).toBe(undefined);
});

定义这些browser.params,它们是整个执行过程中的静态变量:

broswer.params.theCurrentAccount = context.switchAccount.accountName1;
broswer.params.switchToAccount = context.switchAccount.accountName2;

这些参数是静态的,可以跨所有规格访问