首页 > 解决方案 > 如何根据通过特征文件传递的数据更新 xpath

问题描述

我有一个需要点击日期的场景。需要从功能文件发送日期。xpath如下。

//table[@class="mat-calendar-table"]/tbody/tr/td[@aria-label="January 1, 2019"].  

功能文件如下

 Scenario: Protractor date pickers Test"
    Given Go to title page
    Then The title must be "Datepicker | Angular Material"
    When enter the date "January 6, 2019"

请让我知道如何将日期从功能文件传递到 xpath

标签: protractorcucumber

解决方案


import { Given, Then, When } from "cucumber";

// if using async/await
When(/^enter the date "(.*)"$/, async(date) => {
  var util = require('util');
  var xpath = '/table[@class="mat-calendar-table"]/tbody/tr/td[@aria-label="%s"]'

  return await element(by.xpath(util.format(xpath, date))).sendKeys(date);
});

// not using async await
When(/^enter the date "(.*)"$/, (date) => {
  var util = require('util');
  var xpath = '/table[@class="mat-calendar-table"]/tbody/tr/td[@aria-label="%s"]'

  return element(by.xpath(util.format(xpath, date))).sendKeys(date);
});

推荐阅读