Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page Object Methods?

Tags:

I'm having trouble setting up my page object to work. It's probably a simple syntax problem, but I haven't been able to find a solution. I am trying to do something like this:

Test:

test('Verify that a user can update their billing profile', async (t) => {
  await t
    .billingPage.enterBillingInformation('4111111111111111')
    .click(billingPage.saveButton)
    .expect(billingPage.successMessage.exists).ok();
});

Page:

import { Selector, t } from 'testcafe';

export default class billingPage {
  constructor() {
    this.cardNameInput = Selector('#cc_name');
    this.cardNumberInput = Selector('#credit-card-number');
    this.saveButton = Selector('button').withText('Save Card');
    this.successMessage = Selector('div').withText('Your billing information has been successfully updated.');
  }

  async enterBillingInformation(cardNum) {
    await t
      .typeText(this.cardNameInput, 'Foo Bar')
      .typeText(this.cardNumberInput, cardNum)
  }
}

This was working when I had the function contents all within the test, but I want to write a second test with invalid credentials and, obvs, I want to reuse code (the actual function is way longer with many more fields). But I can't figure out what I'm doing wrong.

I get this error:

TypeError: Cannot read property 'enterBillingInformation' of undefined

An example in the docs of how to use methods in the page object would be really helpful! This page seems to show how to set up the function, but there is no corresponding code snippet to show how to actually use it in the test.

http://devexpress.github.io/testcafe/documentation/test-api/test-code-structure.html#test-controller

like image 944
Samantha Blasbalg Avatar asked Sep 06 '18 22:09

Samantha Blasbalg


People also ask

What are the different page object models?

Page object model (POM) can be used in any kind of framework such as modular, data-driven, keyword driven, hybrid framework etc.

What is a page object?

Page Object is a Design Pattern that has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT.

Why PageFactory is used in Selenium?

Introduction to PageFactory Class in Selenium The major benefit of the @FindBy annotation is that it lets you initialize page elements without using the FindElement (or FindElements) in Selenium. PageFactory class in Selenium also provides the initElements method for initializing the web elements.


1 Answers

The "t" object is not known inside the billingPage class. You need to pass it to the enterBillingInformation function from the parent test. Here is complete code:

index.js

import { Selector, ClientFunction } from 'testcafe';
import BillingPage from './billingPage';

const billingPage = new BillingPage();

fixture `Getting Started`
    .page `Your page`;

test('Verify that a user can update their billing profile', async t => {
    await billingPage.enterBillingInformation("4111111111111111", t);  

    await t
        .click(billingPage.saveButton)
        .expect(billingPage.successMessage.exists).ok();  
});

billingPage.js

import { Selector } from 'testcafe';

export default class BillingPage {
    constructor () {
        this.cardNameInput = Selector('#cc_name');
        this.cardNumberInput = Selector('#credit-card-number');
        this.saveButton = Selector('button').withText('Save Card');
        this.successMessage = Selector('div').withText('Your billing information has been successfully updated.');  
    }

    async enterBillingInformation(cardNum, t) {
        await t
          .typeText(this.cardNameInput, 'Foo Bar')
          .typeText(this.cardNumberInput, cardNum)
    }
}

You can learn more about the TestCafe Page model here.

like image 155
Marion Avatar answered Sep 28 '22 19:09

Marion