te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jasmine - How to share data between javascript files? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jasmine - How to share data between javascript files? - Stack Overflow

programmeradmin3浏览0评论

I have (I think) kind of unique problem with js. I'm writing tests using protractor stuff and Jasmine and I need to share data between js files. Is there any way to do that? All the solutions I've found are for webpages and I use just js files.

I look forward to your swift response, if there is any information missing, please let me know and I'll add it immediately.

I have (I think) kind of unique problem with js. I'm writing tests using protractor stuff and Jasmine and I need to share data between js files. Is there any way to do that? All the solutions I've found are for webpages and I use just js files.

I look forward to your swift response, if there is any information missing, please let me know and I'll add it immediately.

Share Improve this question asked Sep 25, 2014 at 10:29 Krzysztof PiszkoKrzysztof Piszko 1,4453 gold badges13 silver badges17 bronze badges 7
  • 2 I haven't used protractor yet, but with karma and jasmine, you can share data using requirejs. If you want I can give some more details. – kihu Commented Sep 25, 2014 at 10:43
  • As far as I know I can only use: Protractor API, Selenium API, Jasmine, JS. It's not my private project so unfortunately I can't use other stuff. UNLESS, I understood you wrong and this is "built into" js. @Edit: Wait, you mean that requirejs is built into Jasmine? If yes, then by all means elaborate. – Krzysztof Piszko Commented Sep 25, 2014 at 10:51
  • no, requirejs is a saparate library requirejs – kihu Commented Sep 25, 2014 at 10:55
  • That's a darn shame then. Thanks anyway! – Krzysztof Piszko Commented Sep 25, 2014 at 10:56
  • Otherwise. perhaps you could declare your data as a global variable in first test, then it would be available for other tests to read. I'm not 100% sure it will work, though. – kihu Commented Sep 25, 2014 at 11:01
 |  Show 2 more ments

4 Answers 4

Reset to default 8

I have not tested this myself, but maybe you can try to put stuff in the global scope using:

global.mySharedData = {someKey: 'some value'}

// in one of your test files
it('should do something', function() {
  global.mySharedData = {someKey: 'some value'}
});

...

// This is in another test suite
it('should do something', function() {
  var valueFromFirstTest = global.mySharedData.someKey;
});

http://nodejs/api/globals.html

Let me know if it works.

If you need to share dynamic data between files you can also do the following. Here's a working example. What I needed to do was take parts of the URL and use them across different files.

it('should click on one of the clickable profiles', function(){

        //Get entity type and entity id before clicking the link
        tableEls.get(1).all( by.xpath('./td') ).get(0).element( by.xpath('./a') ).getAttribute('href').then(function(text){

            var hrefTokens  = text.split('/');
            var entityID    = hrefTokens[ hrefTokens.length - 1 ];
            var entityType  = hrefTokens[ hrefTokens.length - 2 ];

            browser.params.entityID   = entityID;
            browser.params.entityType = entityType;
        });

        tableEls.get(1).all( by.xpath('./td') ).get(0).element( by.xpath('./a') ).click();
        browser.sleep(2000);
    });

I simply assigned the values that I needed to use in other files to the browser.params. So in my other files I can access them like this

    it('Retrieving JSON Data ...', function(){

        var entityID   = browser.params.entityID;
        var entityType = browser.params.entityType;
   });

There is a simple way to share data and even functions among Protractor spec JavaScript files. They run in node.js with a built-in way to define modules and use and reuse them - http://nodejs/docs/latest/api/modules.html.

Assuming the following folder's stracture:

o e2e
|-- utils.js
|-- a-spec.js
|-- b-spec.js

In utils.js:

exports.sharedData = { num: 42, str: 'hi' };
exports.foo = function (x) { return x + 1; };

In a-spec.js:

var utils = require('./utils.js'); // Note './'

describe("The a page", function () {
    it("should give the ultimative answer", function () {
        expect(element(by.binding("answer")).getTest())
            .toBe(utils.sharedData.num); // Using shared data
    });
});

I included this in my config js file where i declare my variables for protractor.

(function () {
    this.defaultPassword = function () {
        return 'superPassword';
    };
}());

Usage in other file is like:

var userLogin = { 'Email': '[email protected]', 'Password': defaultPassword },
发布评论

评论列表(0)

  1. 暂无评论