量角器中的可变范围 [英] Variable scope in protractor

查看:211
本文介绍了量角器中的可变范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行量角器和茉莉花来运行单元测试。

I am running protractor and jasmine to run unit tests.

我需要知道我的网络应用程序的构建版本才能执行不同的测试。

I need to know the build version of my web app in order to execute different tests.

我已声明一个变量来存储此版本值。

I have declared a variable to store this version value.

var version ='';

我使用以下代码获取版本号。

I am getting the version number by using the following code.

menuObject.modaltext.getText().then(function(text) {
            version = text.slice(79,86);
            console.log(version);
            browser.driver.sleep(7000);
});

正确获取版本号并正确安装。

The version number is acquired correctly and is consoled properly.

但是当我在这个.then函数之外使用这个版本时,它的值变得未定义,我无法检查使用该变量的任何条件。

But when i use this version outside of this .then function, its value becomes undefined and I am unable to check for any conditions using that variable.

如何访问版本号,以便我用它来控制测试流程。

How can i access the version number so that I use it to control the flow of the tests.

![版本变量突出显示,我无法访问里面的版本如果情况]

![version variable is highlighted, I am unable to access the version inside the if cases]

推荐答案

尝试将 var 更改为 let 。这允许您在规范中访问版本

Try changing var to let. This allows to access your version inside your specs.

describe('Nodeprojectpart2Component', () => {
    let version = '';
    beforeEach(() => {
        version = '1.0';
      });

    it('test', () => {
      console.log( 'version' + version);
    });

  });

您的代码问题 - 您正在异步/回调函数中检索版本的值。在函数执行之前,执行控制台并打印 undefined 。我不确定你为什么要在规范之外定义代码。但是如果你仍然想要,你应该在 desribe 块中定义它之后立即拥有检索逻辑,类似于 -

Issue with your code - Your are retrieving the value of version inside an asynchronous/callback function. Now before your function executes, your console is executed and prints undefined. I am not sure why would you like to define code outside specs. But if you still want to, you shall have the retrieval logic right after you define it in desribe block, something like -

describe('Nodeprojectpart2Component', () => {
        let version = '';
        version = //logic to find the version here itself
        ....

这篇关于量角器中的可变范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆