代码之家  ›  专栏  ›  技术社区  ›  Mike

如何使用sendkeys量角器API插入长文本?

  •  1
  • Mike  · 技术社区  · 5 年前

    我有一个长文本20行HTML代码,我需要使用到表单测试中。对于小文本,我可以使用类似的方法:

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

    它会很好地工作。但对于20行来说,情况并非如此。我在Protractor API页面中看到了这些信息,但在我的案例中看不到如何使用它:

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

    有一个硒的例子我不知道如何适应: Selenium Webdriver enter multiline text in form without submitting it

    这是角度形式字段:

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

    要插入的文本可以是这样的:

    <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 />
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Jorge M. Nures    5 年前

    将HTML放在引号``中,正如@lunin roman所说,然后调用它:

    let htmltext = `<h2>What is JHipsterPress?</h2> and so on`; 
    
      element(by.id("field_bodytext")).sendKeys(htmltext);
    

    然后在element.sendkeys中使用它