简体   繁体   中英

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
  2. 0 cases found from "from dates" to "to dates" without a download button

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.

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

There's no need to use if else - when the test won't find expected test it'll fail on timeout.

You could just check to see if the download button is present in the DOM first and then click on it. Otherwise, do nothing and move on.

This is assuming the h3 element also has the 'summary' id attribute in the second scenario.

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

or else

write the dynamic xpath using the following::

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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