191218&testcafe 常用的几个功能实现

testcafe 常用的几个功能实现

  • 录入数据
  • 获取当前页面地址
  • 切换 antd 的 select 组件
  • 切换 antd 的日历
  • 下载
  • 跳转到指定页面

录入数据

//录入数据
await t.typeText(dom, "2", { replace: true });

获取当前页面地址

const location_param = ClientFunction(() => window.location); //获取当前页面地址

切换 antd 的 select 组件

//切换subtype
this.changeOption = async function (type) {
  let id = `#${type}`;
  console.log(id);
  let option = Selector(`.ant-select-dropdown-menu li[id=\"${type}\"]`);

  await t.click(this.option).click(option);
};

/***
 *  CASE : [下拉框过滤查询] - 批量
 */
for (const option of Selects) {
  test(`${select}`, async (t) => {
    await xxPage.changeOption(select);
    if (xxPage.table.find("tr").length > 0) {
      for (let i = 0; i < 10; i++) {
        // 取第1-10行的第5列,检查 option
        let tdText = await xxPage.table.find("tr").nth(i).find("td").nth(5)
          .textContent;
        await t.expect(tdText).eql(select);
      }
    }
  });
}

切换 antd 的日历

test("calender", async (t) => {
  const startDate = "2019/10/01";
  const endDate = "2019/11/30";
  await xxPage.changeCalender(
    moment(startDate).format(dateFormat),
    moment(endDate).format(dateFormat)
  );
  if (xxPage.table.find("tr").length > 0) {
    let startText = await xxPage.table.find("tr").nth(0).find("td").nth(10)
      .textContent;
    let endText = await xxPage.table.find("tr").nth(0).find("td").nth(11)
      .textContent;
    await t.expect(moment(startText)).gte(moment(startDate));
    await t.expect(moment(endText)).gte(moment(endDate));
  }
}).before(async (t) => {
  await xxPage.changeOption("option1");
});

下载

import fs from "fs";
const path = require("path");
async function enableDownloadForHeadlessChrome(t) {
  const browserConnection = t.testRun.browserConnection;
  const client =
    browserConnection.provider.plugin.openedBrowsers[browserConnection.id]
      .client;
  const { Page } = client;

  await Promise.all([Page.enable()]);

  await Page.setDownloadBehavior({
    behavior: "allow",
    downloadPath: path.resolve(__dirname, "../../downloaded"),
  });
}

const getDownloadCount = () => {
  let components = [];
  const files = fs.readdirSync(`${BASE_FILE_PATH}`); //必须得物理路径,相对的还不行
  files.forEach(function (item, index) {
    let stat = fs.lstatSync(`${BASE_FILE_PATH}${item}`);
    if (stat.isFile() === true && item != ".DS_Store") {
      components.push(item);
    }
  });
  return components;
};

/***
 *  下载
 */
test("download", async (t) => {
  const downloadedBefore = getDownloadCount().length;
  await enableDownloadForHeadlessChrome(t);
  await t.click(xxPage.btnDownload).wait(10000);
  const downloadedAfter = getDownloadCount().length;
  await t.expect(downloadedAfter).eql(downloadedBefore + 1); //判断文件个数是否新增
});

跳转到指定页面

const hrefSyn = async (t, currentPage, dom, targetPage) => {
  await t.navigateTo(currentPage).click(dom);
  const _location = await location_param();
  console.log(
    xxPage.getPathName(_location.hash),
    xxPage.getPathName(targetPage)
  );
  await t
    .expect(xxPage.getPathName(_location.hash))
    .eql(xxPage.getPathName(targetPage));
};

/***
 *  跳转
 */
test("go href", async (t) => {
  await hrefSyn(t, url1, hrefDom, url2);
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值