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?

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Jan 23, 2019 at 12:38

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().

answered Jan 23, 2019 at 12:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.