0

我想从 xpath 中获取文本并将其存储在字符串中。

输入所有输入并提交后,将生成一个新代码,类似于 Customercode: IN02732114(数字将动态)。

现在我想获取此代码并将其存储在一个字符串中,稍后我想在其他步骤中使用此字符串来使用此代码搜索数据。

我在下面使用了不同的片段来从 xpath 获取文本。

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

字符串代码= customer_code_value(); // 尝试将值存储在字符串代码中

但是 customer_code_value() 方法在问题中返回并且不能存储在字符串中。

需要一些有关如何获取文本并将其存储在 Serenity 中的字符串的帮助。请帮我 ...

4

1 回答 1

0

要定位元素,请使用Target

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

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

要检索元素的文本,请使用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())
}

要执行substring操作,请使用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));
}

要存储该值以便以后可以重用TakeNote它:

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),
    )
于 2021-03-15T02:03:46.487 回答