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