Sudhir Kunchakuri
Sudhir Kunchakuri

Reputation: 1

How to get the text from a particular xpath and how to store that in a string using Serenity

I want to fetch the text from an xpath and store it in a string.

After entering all inputs and submitting , a new code will get generated and which looks like Customercode: IN02732114(number will dynamic).

Now i want to fetch this code and store it in a string and later I want to use this string in other steps to search the data with this code.

i have used below different snippets to get the text from the xpath.

public static Question customer_code_value() { return actor -> Text.of(CustomerCreatePage.CUSTOMER_CODE_TEXT).viewedBy(actor).asString().substring(15, 26); }

String code= customer_code_value(); // trying to store the value in String code

but customer_code_value() method returns in Question and cant store in String.

Need some help on how to get the text and store it in string in Serenity. Please help me ...

Upvotes: 0

Views: 1029

Answers (1)

Jan Molak
Jan Molak

Reputation: 4536

To locate an element, use the Target:

import { Target } from '@serenity-js/protractor';
import { by } from 'protractor';

class CustomerCreatePage {
    static customerCode = () =>
        Target.the('customer code').located(by.xpath(...));
}

To retrieve the text of an element, use Text

import { Target, Text } from '@serenity-js/protractor';
import { by } from 'protractor';

class CustomerCreatePage {
    static customerCode = () =>
        Target.the('customer code').located(by.xpath(...));

    static customerCodeText = () =>
        Text.of(CustomerCreatePage.customerCode())
}

To perform a substring operation, use Question.map:

import { Target, Text } from '@serenity-js/protractor';
import { by } from 'protractor';

class CustomerCreatePage {
    static customerCode = () =>
        Target.the('customer code').located(by.xpath(...));

    static customerCodeText = () =>
        Text.of(CustomerCreatePage.customerCode())
            .map(actor => value => value.substring(15, 26));
}

To store the value so that it can be reused later, TakeNote of it:

import { actorCalled, TakeNotes, TakeNote, Note } from '@serenity-js/core';
import { BrowseTheWeb } from '@serenity-js/protractor';
import { protractor } from 'protractor';

actorCalled('Sudhir')
    .whoCan(
        BrowseTheWeb.using(protractor.browser),
        TakeNotes.usingAnEmptyNotepad(),
    )
    .attemptsTo(
        TakeNote.of(CustomerCreatePage.customerCodeText).as('customer code'),
        // do some other interesting things
        Enter.the(Note.of('customer code')).into(someField),
    )

Upvotes: 1

Related Questions