使用页面中的静态对象(Cypress)



通过在Cypress上实现e2e测试的各种示例,我发现许多人使用创建新对象的方法,而不是使用static。他们为什么这么做?为什么不将static用于页面对象方法呢?因为我们不更改类本身中的任何数据,因此也不与this通信,而且我们不需要具有同一页面的多个实例(或者我看不到使用这种方法的场景(。我知道Selenium使用页面工厂,因此有必要创建一个新对象,但我没有在Cypress中找到类似的方法。

创建新对象的示例:

import { BasePage } from './BasePageClass'
import { navMenu } from './NavigationMenuClass';
import { queryPage } from './QueryPageClass';
export class MainPage extends BasePage  {
constructor() {
super();
this.mainElement = 'body > .banner';
}
verifyElements() {
super.verifyElements();
cy.get(this.mainElement).find('.container h1').should('be.visible');
}
switchToQueryingPage() {
navMenu.switchToQueryingPage();
queryPage.verifyElements();
}
};
export const mainPage = new MainPage();

使用静态的示例:

import { BasePage } from './BasePageClass'
import { navMenu } from './NavigationMenuClass';
import { queryPage } from './QueryPageClass';
export default class MainPage extends BasePage  {
static mainElement = 'body > .banner';
constructor() {
super();
}
static verifyElements() {
super.verifyElements();
cy.get(MainPage.mainElement).find('.container h1').should('be.visible');
}
static switchToQueryingPage() {
navMenu.switchToQueryingPage();
queryPage.verifyElements();
}
};

这里有一个我的前辈教我使用的命令组合的例子。我有一个函数buyProduct,但它需要几个步骤,我在不同的命令中制作了这些步骤,可以在不同的情况下使用:

it('buyProductAsCustomer', ()=>{
cy.login(customer)
cy.shopFe_openProductByName(product.name)
cy.shopFe_addProductToCart()
cy.shopFe_openCheckoutFromCart()
cy.shopFe_selectPaymentMethod("payWithCreditCardMethod")
cy.shopFe_fillCheckoutFormAsCustomer(customer)
cy.shopFe_checkoutPageGetOrderData()
cy.shopFe_submitCheckoutForm()
cy.shopFe_completePaymentWithCreditCard()
});
it('buyProductAsGuest', ()=>{
cy.shopFe_openProductByName(product.name)
cy.shopFe_addProductToCart()
cy.shopFe_openCheckoutFromCart()
cy.shopFe_selectPaymentMethod("payWithCashMethod")
cy.shopFe_fillCheckoutFormAsGuest(guest)
cy.shopFe_checkoutPageGetOrderData()
cy.shopFe_submitCheckoutForm()
cy.shopFe_completePaymentWithCreditCard()
});

这两种情况可以通过几个变量来控制,但随着时间的推移,这些变量往往会增加十倍。因此,我们将它们分离成更小的可重复部分,这些部分可以以不同的方式组合。

最新更新