1

I have a long text 20 lines with HTML code that I need to use into a form test. For small texts I could use something like this:

browser.actions().mouseMove(element(by.id("field_bodytext")).sendKeys("BodyText <h2>Protractor</h2> Text1")).perform();

and it will work fine. But for 20 lines it does not. I have seen this information in Protractor API page, but can not see how to use it in my case:

http://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.sendKeys

var form = driver.findElement(By.css('form'));
var element = form.findElement(By.css('input[type=file]'));
element.sendKeys('/path/to/file.txt');
form.submit();

There is an example for selenium that I do not see how to adapt: Selenium Webdriver enter multiline text in form without submitting it

This is the form field in Angular:

<div class="form-group"><a href="https://wordhtml.com/" target="_blank">Wordhtml.com</a>
                <label class="form-control-label" jhiTranslate="jhipsterpressApp.post.bodytext" for="field_bodytext">Bodytext</label>
                <textarea type="text" rows="10" cols="50" class="form-control" name="bodytext" id="field_bodytext"
                    [(ngModel)]="post.bodytext" required minlength="2" maxlength="65000">
                <div [hidden]="!(editForm.controls.bodytext?.dirty && editForm.controls.bodytext?.invalid)">
                    <small class="form-text text-danger"
                    [hidden]="!editForm.controls.bodytext?.errors?.required" jhiTranslate="entity.validation.required">
                    This field is required.
                    </small>
                    <small class="form-text text-danger"
                    [hidden]="!editForm.controls.bodytext?.errors?.minlength" jhiTranslate="entity.validation.minlength" translateValues="{ min: 2 }">
                    This field is required to be at least 2 characters.
                    </small>
                    <small class="form-text text-danger"
                    [hidden]="!editForm.controls.bodytext?.errors?.maxlength" jhiTranslate="entity.validation.maxlength" translateValues="{ max: 65000 }">
                    This field cannot be longer than 65000 characters.
                    </small>
                </div>
                </textarea>
            </div>

And the text to be inserted could be something like this:

<h2>What is JHipsterPress?</h2>
<ul>
<li>It is an open source and collaborative project made with Jhipster.</li>
<li>It is a live project that it is explained in its GitHub project. Explained? What do you mean? Whether you are a beginner that wants to find out an example about how to <a href="https://github.com/Tonterias/JhipsterPress08/blob/master/ReadMe/Solution1.md" target="_blank"> How to open access to the REST Api:</a> or a more advance user who wants to see <a href=" https://github.com/Tonterias/JhipsterPress08/blob/master/ReadMe/Solution20.md" target="_blank"> How to change DTOs to load attributes of not related entities:</a> and see the actual code working, just visit the <a href="https://github.com/Tonterias/JhipsterPress08" target="_blank"> ReadMe file at GITHUB</a></li>
<li>And YES, you can use it for your own website.</li>
<li>At the same time JHipsterPress will try to create a community for Jhipster developers to join groups about different topics such as Websockets, Mapstruct, or anything you like.</li>
</ul>
<br />
Mike
  • 1,059
  • 5
  • 28
  • 51
  • what exactly happens when you tried to insert long text? and what input type do you have? textarea? – Majesty Jan 21 '19 at 12:15
  • It stops after reading the first and does not continue in the next line [16:02:53] E/launcher - Error: D:\BasuraTemporal\Jhipster\Protractor\VSproyect\spec1Login.js:22 browser.actions().mouseMove(element(by.id("field_bodytext")).sendKeys("

    What is JHipsterPress?

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Invalid or unexpected token at createScript (vm.js:80:10)
    – Mike Jan 21 '19 at 15:03
  • It seems that the problem is not in the length, but in some specific symbol in it. could you please add an HTML that you are trying to insert? also, `element.sendKeys('/path/to/file.txt')` would work only for file inputs, it is not a case for input text or textarea – Majesty Jan 21 '19 at 15:15
  • Hi Lunin, It is a textarea. I modified the question to add the HTML of the field and part of the text that I want to insert. Thanks for your help! If you need anything else let me know – Mike Jan 21 '19 at 15:20
  • Lunin, is it possible to put all the text in a variable and use the varaible to insert it? Thanks – Mike Jan 21 '19 at 15:49
  • what version of protractor do you have? just tested locally and I have no problem to insert that text into `textarea` input – Majesty Jan 21 '19 at 16:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187060/discussion-between-mike-and-lunin-roman). – Mike Jan 21 '19 at 16:11

1 Answers1

0

Put the HTML in quotes `` as @lunin-roman is saying and then call it:

let htmltext = `<h2>What is JHipsterPress?</h2> and so on`; 

  element(by.id("field_bodytext")).sendKeys(htmltext);

and then use it in the element.sendKeys

Jorge M. Nures
  • 680
  • 1
  • 14
  • 30