1
describe('Enter Base API URL', function() {   
  it('1st scenario', function() {  
  // Take any URL.   
  browser.get('xyz.com');   
  browser.ignoreSynchronization=true;  
  var user= by.xpath('//input[@name="username"]');  
  });

  it("2nd scenario", function() {   
  browser.driver.findElement(user).sendKeys('hi');   
  });   
browser.close();
});

How can I use this user variable in my second it block?

1 Answer 1

1

Ideally, this user field should be defined in a separate "page object".

As far as two tests under describe go, it is just about defining a variable in a "higher" scope:

describe('Enter Base API URL', function() {   
    var user;

    it('1st scenario', function() {  
        // Take any URL.   
        browser.get('xyz.com');   

        browser.ignoreSynchronization=true;  
        user = by.xpath('//input[@name="username"]');  
    });

    it("2nd scenario", function() {   
        browser.driver.findElement(user).sendKeys('hi');   
    });   
});

Note that user = by.xpath('//input[@name="username"]'); by itself would do nothing, protractor/selenium would not try to locate the element at this point. Only when you are actually using the element, it would locate it on a page.

Also, note that protractor introduces the concepts of an ElementFinder - meaning, you should probably be using element() as opposed to browser.driver.findElement().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.