export const username = 'exampleUser'; export const password = 'examplePassword';
import { username, password } from './globalVariables'; describe('Login page', () => { it('should log in successfully', () => { browser.get('https://example.com/login'); const usernameField = element(by.id('username')); const passwordField = element(by.id('password')); usernameField.sendKeys(username); passwordField.sendKeys(password); const loginButton = element(by.id('login-button')); loginButton.click(); expect(browser.getCurrentUrl()).toContain('dashboard'); }); });
export function waitForElement(selector: string, timeout: number = 5000) { const EC = protractor.ExpectedConditions; const elementToWaitFor = element(by.css(selector)); return browser.wait(EC.visibilityOf(elementToWaitFor), timeout); }
import { waitForElement } from './globalFunctions'; describe('Home page', () => { it('should display welcome message', () => { browser.get('https://example.com'); waitForElement('.welcome-message'); const welcomeMessage = element(by.css('.welcome-message')); expect(welcomeMessage.getText()).toContain('Welcome!'); }); });In this example, we define a global function in globalFunctions.ts that waits for an element to be visible on the page. We then import this function in our homePage.ts file and use it to wait for a welcome message to appear before asserting that it contains the expected text. This allows us to easily reuse this function in multiple test files if we need to wait for elements on different pages.