当前位置: 代码迷 >> JavaScript >> 单击动态生成的按钮
  详细解决方案

单击动态生成的按钮

热度:99   发布时间:2023-06-05 09:37:02.0

我将给出“开始日期”和“结束日期”,然后单击“创建”按钮。 预期的输出是

  1. 使用下载按钮从“从日期”到“到日期”找到N个案例
  2. 从“从日期”到“到日期”共找到0个案例,没有下载按钮

在第一种情况下:

<div data-ng-if="canDownload()" class="ng-scope"
<h3 class="ABC" id="summary">N cases ound from "from dates" to "to dates"
<a data-ng-href="URL" id="summaryHREF"
<button class="XYZ" type="submit">Download<

在第二种情况下:

<div data-ng-if="noCases()" class="ng-scope"
<h3 class="ABC" >0 cases ound from "from dates" to "to dates"

我成功地测试了阳性方案(在发现病例的情况下)

let notes = element(by.id("summary"));

var EC = protractor.ExpectedConditions;
var flag = browser.wait(EC.visibilityOf(notes), 5000, '**** There are cases to Download ****');

if(flag){

  this.downloadReg = element(by.xpath("//button[text()='Download']"));
  this.downloadReg.click();
}
else{
  console.log("No Cases found and Do Nothing");

}

如何检查“摘要”文本是否包含“找到0个案例...”,然后什么也不做,或者是否找到案例,然后单击“动态生成的下载”按钮。

请尝试以下代码段,

browser.wait(EC.visibilityOf(element(by.css('#summary'))), 5000, '**** There are cases to Download ****').then(flag => {
      if(flag){
        this.downloadReg = element(by.xpath("//button[text()='Download']"));
        this.downloadReg.click();
      }else{
        console.log("No Cases found and Do Nothing");
      }
    });

干杯!

我建议使用:

if else没有其他必要, if else无需使用-当测试找不到预期的测试时,它将在超时时失败。

您可以先检查DOM中是否存在下载按钮,然后单击它。 否则,什么也不做,继续前进。

假设在第二种情况下, h3元素也具有'summary' id属性。

const notes = element(by.id('summary'));
await browser.wait(EC.visibilityOf(notes), 5000);

const downloadBtn = element(by.buttonText('Download'));
const flag = await downloadBtn.isPresent();

if (flag) {
    await downloadBtn.click();
}

1)等待元素使用期望的条件(EC)定位2)使用cssContainingText('locator',“ string”)

要不然

使用以下命令编写动态xpath:

  相关解决方案