0

Hi I'm writing some e2e tests using protractor, chai and mocha. Language is Typescript. I'm very new to these things. I've been stuck for hours on locating an element. How do I locate button "Edit ValueChart" from a bunch of nested HTML as the following:

<div id="ValueChartView">
<input type="color" id="primitiveObjective-color-picker">
<div class="valuechart-toolbar">
    <form class="form-inline col-xs-12">
        <div class="form-group valuechart-toolbar-group pull-left">
            <a class="btn btn-default" *ngIf="enableManagement()" [routerLink]="['/createValueChart/editChart/BasicInfo']">
            Edit ValueChart
            </a>
        </div>

I've tried things like

var editValueChartBtn = p.browser.element(p.by.css('.form-group.valuechart-toolbar-group.pull-left .btn.btn-default'));

// The button should not appear on the page
expect(editValueChartBtn.isDisplayed()).to.eventually.be.false;

or

var editValueChartBtn = p.browser.element(p.by.cssContainingText('#ValueChartView','Edit ValueChart')); 

// The button should not appear on the page
expect(editValueChartBtn.isDisplayed()).to.eventually.be.false;

and a bunch of others, but none works. Always get error "No Such Element Found". Occasionally I got error "Assertion Error: Expected true to be false", in which case I suspect I didn't find the right element anyways. Could someone help me locate the element? Also, I found the above source codes in a template.html file, which is a bit different from the codes I see when inspecting on the webpage. Why is that? Thank you so much!

vw511
  • 171
  • 10
  • did you try by.buttonText('Edit ValueChart') – Jeremy Kahan Jun 12 '17 at 02:09
  • I did. Didn't work. Could it be some error other than the locator? – vw511 Jun 12 '17 at 04:42
  • Maybe try `element(by.css('#ValueChartView form a.btn-default'))`. Also, instead of `isDisplayed()`, why not `isPresent()`? – Ania Jun 13 '17 at 14:25
  • Ah @AnnaN , after combining both of your suggestions, it seems to be working! Thank you so much! Do you mind explaining the logic that you chose the locator? And as you asked, I thought `isDisplayed()` is false when `isPresent()` is false OR `isPresent()` is true but `isVisible()` is false. Therefore I thought `isDisplayed()` has a larger chance to be false when you are not sure about `isPresent()`. But seems like that's wrong? – vw511 Jun 14 '17 at 23:45
  • @vw511 you're welcome :) Element can be present but not displayed (isVisible is an old version of isDisplayed, with different approach to how to determine visibility). IsPresent will tell you if the element is on the page at all (hidden or not). My locator is much more specific than looking for button with text (there can be another one with the same text and you will always be pointing to first one found in this case). – Ania Jun 15 '17 at 09:55

0 Answers0