Techie
Techie

Reputation: 1651

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.

Upvotes: 0

Views: 107

Answers (4)

Ven Test
Ven Test

Reputation: 1

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

Upvotes: 0

dheeraj
dheeraj

Reputation: 344

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!

Upvotes: 1

tehbeardedone
tehbeardedone

Reputation: 2858

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

Upvotes: 0

Kacper
Kacper

Reputation: 1199

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.

Upvotes: 0

Related Questions