clicking a dynamically generated button

104 views Asked by At

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.

4

There are 4 answers

1
dheeraj On BEST ANSWER

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!

3
Kacper On

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.

3
tehbeardedone On

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();
}
0
Ven Test On

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