简体   繁体   中英

How to pass a variable retrieved from 1 protractor test script to another

I want to achieve the following:

  1. Create a dynamic id for a customer in my createPlan.js
  2. Store the ID in a global variable
  3. Use this globalVariable in a separate file, sendforreview.js

Currently I'm using jasmine data provider but dynamically I'm not able to
do it every time after i run the first js file then i update the jasmine data
provider and then I run the next js file so that the plan number is fed as
input in the search textbox .

This is a simple logic but it'll be applicable across any application I guess.

createplan.js file:

var plannumberis, using = require('jasmine-data-provider');
describe("Plan Creation", () => {
    using(leadEnters.leadinputs, (data, description) => {
        it("Plan is generated and stored globally", () => {
            jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
            browser.wait(EC.urlContains('WorkFlow/#/app/impPlan'));
            browser.wait(EC.presenceOf(element(by.xpath("//div[@class='sweModal-body']"))), 160 * 10000);
            var dialog = element(by.className("sweModal-body"));

            expect(dialog.isDisplayed).toBeTruthy();

            var dialog-text = element(by.className("sweModal-content"));
            dialogtext.getText().then(function (text) {
                console.log(text);

                plannumberis=text.slice(20, 28);// --here i store the plan number in var
                // plannumberis **

                console.log("implementation plan id is :" + plan);
                browser.wait(EC.presenceOf(element(by.css("button[class='btn btn-primary okBtn']"))));

                element(by.css("button[class='btn btn-primary okBtn']")).click();
            })
        })
    })
})

sendforreview.js

describe("STAGE 2:Developer Activities : Segment Checkout", () => {
    using(DeveloperInputs.Devinputs, (data, description) => {
        it("Developer checksout the required segments", () => {
            searchPlanId.clickSearch();
            searchPlanId.selectSearchTypeWithIndex(2);

            searchPlanId.enterImplementationNo(data.PlanNumber);// --here i want to
            // call theplannumber generated in the first js file.How to do this

            browser.wait(EC.presenceOf(element(by.css("i[class='fa fa-search']"))), 10000);
            searchPlanId.clickSearchButton();
            developerActions.editFirstImplementation(plannumberis);

            // Here I want to call the id generated in createplan.js

            // So in jasmine data provider I'm manually updating
            // this after I run the 1stjs file then pass it in second jsfile
        })
    })
})

You should be able to achieve this by using NodeJS's global variables keyword which make variables accessible to all files in the project.

file1.js

//delcare var
global.newGlobalVar = 'value we want in another file'

file2.js

//use the var
console.log(newGlobalVar)

However as it appears you are using loops in both your test files I cannot see how this approach will work for you

The issue you're having is that var plannumberis is on the module (file) scope, not the global scope. Your variable is only accessible within that spec file.

Some more from the docs :

In browsers, the top-level scope is the global scope. This means that within the browser var something will define a new global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module.

The way to put something on the global scope is to put it on the global object.

global.plannumberis=text.slice(20, 28);

After that you can reference it from any module using plannumberis .


However, I cannot encourage your design decision here. Having interdepencencies between specs like this is highly discouraged. With the way you've designed it, if anyone ever tries to run sendforreview.js by itself, the test will fail in a confusing and seemingly-arbitrary manner . You should write your specs so that they are all independent of each other and can be run in any order.

In this particular case, you could accomplish that by combining the two files into a single spec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM