Wicky
Wicky

Reputation: 128

Need an example of how to do a button click on serenity-js

I am really new to serenity-js and Protractor. I followed the 'https://github.com/serenity-js/seed-cucumber/tree/master/features' to get familiar with serenity-js. Can someone give me an example of how to do a button click?

E.g click on 'TRY THE NEW ANGULAR'

Upvotes: 0

Views: 1718

Answers (1)

Jan Molak
Jan Molak

Reputation: 4526

A great place to start is to follow the official tutorial.

Update: This question was asked in 2019 and was related to Serenity/JS v2

To click on a button using Serenity v2 you'll need:

import { Actor } from '@serenity-js/core'
import { BrowseTheWeb, Click, Target } from '@serenity-js/protractor'

// define a Target you want the Actor to interact with
const LandingPage = {
  Buy_Now: Target.the('"Buy Now" button').located(by.id('#buy-now'));
}

describe('my scenario', () => {

  it('works', async () => {

    // instantiate an Actor
    const actor = Actor.named('Bob')
      .whoCan(BrowseTheWeb.using(protractor.browser));

    // tell the actor to perform the interaction to Click
    actor.attemptsTo(
      Click.on(LandingPage.Buy_Now),
    )
  })
})

In 2024 and with Serenity/JS 3, the ability to BrowseTheWebWithProtractor is provided automatically to your actors, so the scenario can be implemented without an explicit actor setup:

import { actorCalled } from '@serenity-js/core'
import { By, Click, PageElement } from '@serenity-js/web')
import { describe, it } from 'mocha'

const LandingPage = {
  Buy_Now: PageElement.located(By.id('buy-now'))
    .describedAs('"Buy Now" button')
}

describe('my scenario', () => {

  it('works', async () => {
    await actorCalled('Bob').attemptsTo(
      Click.on(LandingPage.Buy_Now)
    )
  })
})

Also, please note that Protractor is deprecated, so you'll want to use Serenity/JS with a more modern test runner like WebdriverIO or Playwright Test.

Follow the official [Serenity/JS Protractor migration guide to learn how to migrate your Serenity/JS test scenarios from Protractor.

Hope this helps!

Jan

Upvotes: 1

Related Questions