运行此测试后,在sendKeys
,click
或submit
之前元素出现之前似乎没有正常等待,即使我正在等待调用driver.wait
直到elementLocated
。
它似乎也开始了测试,而beforeAll
仍在执行。
异步测试
import { webDriver } from './helpers';
import LoginPage from './pages/loginPage';
import LogoutPage from './pages/logoutPage';
import RolePage from './pages/rolePage';
import PatientSelect from './pages/patientSelect';
describe('example test', () => {
const driver = webDriver;
beforeAll(async () => {
const loginPage = new LoginPage(driver);
await loginPage.init();
await loginPage.login('test', 'test1', 50002);
const rolePage = new RolePage(driver);
await rolePage.selectRole(2);
});
it('should select a patient', async () => {
const patientSelect = new PatientSelect(driver);
await patientSelect.searchField('Mary');
await patientSelect.selectPatient(1);
});
afterAll(async () => {
const logoutPage = new LogoutPage(driver);
await logoutPage.init();
await logoutPage.logout();
await driver.quit();
});
});
基页对象
import { until } from 'selenium-webdriver';
import { defaultTimeout } from '../helpers';
export default class BasePage {
constructor(driver) {
if (driver == null) {
throw new TypeError('Expecting an instance of WebDriver');
}
this.driver = driver;
}
async waitThenGetElement(element) {
await this.driver.wait(until.elementLocated(element), defaultTimeout);
const elements = await this.driver.findElement(element);
return elements;
}
async waitThenGetElements(element) {
await this.driver.wait(until.elementLocated(element), defaultTimeout);
const elements = await this.driver.findElements(element);
return elements;
}
}
登录页面对象
import { By, until } from 'selenium-webdriver';
import BasePage from './basePage';
export default class LoginPage extends BasePage {
async init() {
await this.driver.get(`${__ROOT_URI__}/login?return_to=/`);
await this.driver.wait(until.titleIs('Login'));
}
async username(value) {
const element = await this.waitThenGetElement(By.id('UserName'));
element.sendKeys(value);
}
async password(value) {
const element = await this.waitThenGetElement(By.id('Password'));
element.sendKeys(value);
}
async cdb(value) {
const element = await this.waitThenGetElement(By.id('Cdb'));
element.sendKeys(value);
}
async submit() {
const element = await this.waitThenGetElement(By.className('btn-primary'));
element.submit();
}
async login(username, password, cdb) {
await this.username(username);
await this.password(password);
await this.cdb(cdb);
await this.submit();
}
}
患者选择页面对象
import { By } from 'selenium-webdriver';
import BasePage from './basePage';
export default class PatientSelect extends BasePage {
async searchField(value) {
const element = await this.waitThenGetElement(By.className('form-control'));
element.sendKeys(value);
}
async searchResults() {
const element = await this.waitThenGetElements(By.className('patient-link'));
return element;
}
async selectPatient(position) {
const searchResults = await this.searchResults();
searchResults[position - 1].click();
}
}
助手
import { Builder } from 'selenium-webdriver';
export const webDriver = new Builder()
.forBrowser('chrome')
.build();
export const defaultTimeout = 10e3;