1

I am very new to protractor and still getting my head around promises btu am trying to do a, what I see as, a simple for loop. Basically I have an imbedded text editor that I cannot use the (element...).clear(); function with due to the element not being able to be interactive with.

So I would like to get the text length(x) in the text editor and then press the backspace x number of times, clearing the text.

Any help would be greatly appreciated.

element(by.css("div[class='ace_content']")).getText().then(function(textvalue) {
   for (var i = textvalue.length; i == 0; i--) {
      console.log("i =" + i)
      element(by.css("div[class*='ace_editor'] > textarea")).sendKeys(protractor.Key.BACK_SPACE);
   }
johnny 5
  • 19,893
  • 50
  • 121
  • 195
Husain Ashraf
  • 241
  • 1
  • 5
  • 13

2 Answers2

0

I haven't tried this, but could you use the getWebElement locator (http://www.protractortest.org/#/api?view=ElementFinder.prototype.getWebElement) and then set the value of the element to an empty string? Since that locator returns a DOM reference, it should work. Something like:

var editor = element(by.css('div[class*='ace_editor'] > textarea')).getWebElement();
editor.value = '';
MBielski
  • 6,628
  • 3
  • 32
  • 43
0

Let's make it simpler - select all the text in the editor and push backspace once:

var editor = element(by.css("div.ace_content")),
    keys = protractor.Key;
browser.actions()
    .click(editor)
    .sendKeys(keys.chord(keys.COMMAND, "a"))  
    .sendKeys(keys.BACKSPACE)
    .perform();

keys.CONTROL on windows.

FYI, here is a cross-platform way to handle the CTRL/COMMAND automagically:

Here is a sample test that demonstrates how does it work (using the ACE Editor Demo page):

describe("Ace editor", function () {
    beforeEach(function () {
        browser.ignoreSynchronization = true;
        browser.get("https://ace.c9.io/#nav=about");
    });

    it("should change the ace editor text", function () {
        var EC = protractor.ExpectedConditions,
            editor = $(".ace_content"),
            keys = protractor.Key;

        browser.wait(EC.visibilityOf(editor), 5000);

        browser.actions()
            .click(editor)
            .sendKeys(keys.chord(keys.COMMAND, "a"))
            .sendKeys(keys.BACKSPACE)
            .sendKeys("test")
            .perform();

        expect($(".ace_identifier").getText()).toEqual("test");
    });
});

Note that there is always that invisible textarea element that is behind the Ace Editor wrapper. You can alternatively make it visible and interact with it.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you very much for the above information and hints but after implmenting I get the error 'Failed: unknown error: keys should be a string'. I am assuming this is at the .sendKeys. – Husain Ashraf Dec 10 '15 at 22:36
  • @HusainAshraf could u please show me what code are u executing? Thanks! – alecxe Dec 10 '15 at 22:38
  • Also I am using a Mac – Husain Ashraf Dec 10 '15 at 22:51
  • I am using the first example var editor = element(by.css("div.ace_content")), keys = protractor.Key; browser.actions() .click(editor) .sendKeys(keys.chord(keys.COMMAND, "a")) .sendKeys(keys.BACKSPACE) .perform(); – Husain Ashraf Dec 10 '15 at 22:51
  • @HusainAshraf interesting, could you please show me the complete traceback of the error? – alecxe Dec 11 '15 at 04:30
  • apologies for the late reply, the full staceback of the error is "Failed: unknown error: keys should be a string (Session info: chrome=47.0.2526.106) (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.10.5 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 2 milliseconds Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'" – Husain Ashraf Jan 05 '16 at 11:00