Protractor 量角器元素定位器是否需要getter函数

Protractor 量角器元素定位器是否需要getter函数,protractor,e2e-testing,Protractor,E2e Testing,当使用PageObjects进行量角器e2e测试时,是否应该对元素定位器变量使用getter函数,而不是使用变量 例如: public loginButton: ElementFinder = $('#login-submit'); public loginUsername: ElementFinder = $('#login-username'); 或者在页面对象中使用getter函数,如下所示: public get loginButton(): ElementFinder { ret

当使用PageObjects进行量角器e2e测试时,是否应该对元素定位器变量使用getter函数,而不是使用变量

例如:

public loginButton: ElementFinder = $('#login-submit');
public loginUsername: ElementFinder = $('#login-username');
或者在页面对象中使用getter函数,如下所示:

public get loginButton(): ElementFinder {
  return $('#login-submit');
}

public get loginUsername(): ElementFinder {
  return $('#login-username');
}

一种方法比另一种好吗?

使用这两种方法都可以,但是如果您要对元素进行变换或做其他事情,那么我认为getter函数会更好,因为这就是我们首先使用变异子的原因。

如果有,我不确定哪种方法更方便。
我使用量角器已经有一年多了,我总是将定位器存储在变量中。
我通常会做的是:

const button = element(by.buttonText('Button'));
然后我将创建一个与元素交互的函数:

const EC = protractor.ExpectedConditions;

const clickElement = async (element) => {
    await browser.wait(EC.elementToBeClickable(element));
    await element.click();
};
最后,使用它:

await clickElement(button);

当然,我将定位器和函数存储在页面对象中,并在spec文件中调用它们。到目前为止,它工作得很好。

不需要getter,因为dragrator ElementFinder和ElementArrayFinder对象是惰性的-在您尝试调用某些方法之前,不会对该元素进行任何搜索。实际上,这也是您不需要使用Wait for Digrator元素搜索方法的原因:

const button = $('button') // no await needed, no getter needed
console.log('Element is not yet tried to be searched on the page')
await button.click() // now we are sending 2 commands one by one - find element and second - do a click on found element.

ElementFinder可用于构建用于查找元素的定位器链。ElementFinder在调用操作之前不会实际尝试查找元素,这意味着可以在页面可用之前在帮助文件中设置元素