[英]TypeScript - unable to import functions from a helper class
我正在使用Protractor框架創建TypeScript,並計划在類中使用一組輔助函數(如isDisplayed(),isPresent(),isClickable()等),並嘗試在spec文件中重用它們。
最終結果將如下所示:
selectDropDown(element, option);
enterText(element, text);
但是,由於無法從幫助文件中導出功能,因此在創建幫助文件時遇到了問題。 自動完成或自動建議不起作用,因為無法訪問該對象。 :(
helper.ts
import { } from 'jasmine';
import { by, element, browser } from 'protractor';
import { By } from 'selenium-webdriver';
export class helper {
async sayHello(name: String) {
console.log('Hello ' + name);
}
}
樣品page.ts
import { by, element, browser, WebElement } from 'protractor';
import { async } from 'q';
import { helper } from '../../helper'; //Importing the helper here
export class loginPage{
help123: helper = new helper();
help123. //Here the auto completion just doesn't happen.
我要去哪里錯了?
您將必須創建一個新變量才能使其工作,例如:
import { helper } from '../../helper';
export class loginPage{
let help = new helper();
help.sayHello('name');
}
一個簡單的解決方案可能是使助手函數中的方法靜態化。 如果你有:
export class helper{
static async sayHello(name:String){
console.log('Hello '+name);
}
}
然后可以像下面這樣調用此方法:
import { helper } from '../../helper';
export class loginPage{
helper.sayHello('name');
}
另外,在您的幫助器類中,您可以使用諸如以下方法的吸氣劑:
export class helper{
static get x() {return new this();} //could be named anything, I chose 'x'
async sayHello(name:String){
console.log('Hello '+name);
}
}
這將允許您調用類似的方法
import { helper } from '../../helper';
export class loginPage{
helper.x.sayHello('name');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.