简体   繁体   English

单击动态生成的按钮

[英]clicking a dynamically generated button

I will give "from dates" and "To dates" and hit a "create" button. 我将给出“开始日期”和“结束日期”,然后单击“创建”按钮。 The expected output is 预期的输出是

  1. N cases found from "from dates" to "to dates" with a download button 使用下载按钮从“从日期”到“到日期”找到N个案例
  2. 0 cases found from "from dates" to "to dates" without a download button 从“从日期”到“到日期”共找到0个案例,没有下载按钮

In the 1st scenario : 在第一种情况下:

<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<

In the 2nd scenario : 在第二种情况下:

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

I am successful in testing the postive scenario(where cases found) 我成功地测试了阳性方案(在发现病例的情况下)

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");

}

How do I check if the "summary" text contains "0 cases found...." then do nothing or if the cases found, then click on the Dynamically generated Download button. 如何检查“摘要”文本是否包含“找到0个案例...”,然后什么也不做,或者是否找到案例,然后单击“动态生成的下载”按钮。

Pls try the below snippet, 请尝试以下代码段,

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");
      }
    });

Cheers! 干杯!

I recommend to use: ExpectedConditions.textToBePresentInElement 我建议使用: ExpectedConditions.textToBePresentInElement

There's no need to use if else - when the test won't find expected test it'll fail on timeout. if else没有其他必要, if else无需使用-当测试找不到预期的测试时,它将在超时时失败。

You could just check to see if the download button is present in the DOM first and then click on it. 您可以先检查DOM中是否存在下载按钮,然后单击它。 Otherwise, do nothing and move on. 否则,什么也不做,继续前进。

This is assuming the h3 element also has the 'summary' id attribute in the second scenario. 假设在第二种情况下, 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) Wait for the element to locate using expected conditions(EC) 2) use the cssContainingText('locator',"string") 1)等待元素使用期望的条件(EC)定位2)使用cssContainingText('locator',“ string”)

or else 要不然

write the dynamic xpath using the following:: 使用以下命令编写动态xpath:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM