如何通过 Jasmine JS 在 Webdriver 中操作 "text" 的验证?

How to manipulate verification on "text" in Webdriver by Jasmine JS?

我已经尝试了很多代码,但没有一个通过测试。

通常当它试图读取此验证时测试失败,或者它收到 "false" 而不是 "true"。

全部脚本如下:

 describe('LiveSite Portal - Client share a document ', function() {

  it('LiveSite - Home Page', function() {
       liveSiteHome();
    });

  it('LiveSite Portal - Client share a document - Load new document', function() {

    element(by.xpath("//div[@class='actions-row']//a[.='Share Document']")).click();
    browser.sleep(2000);

    expect (element(by.xpath("//div[@class='upload-section']/div[1]/div[2]/div")).isPresent()).toBe(true);

    captureScreen("documentForm");
    browser.sleep(2000);
    element(by.css("input.file-input.upload-file-input")).sendKeys("C:\automation\myDocument.docx");
    browser.sleep(3000);

    var reviewElement = element(by.bind('selectedFiles[0].name')); 
    // expect (reviewElement.getText('myDocument.docx').isPresent()).toBe(true);
    expect(reviewElement.getText()).toEqual("myDocument.docx");

    captureScreen("documentLoaded");
    browser.sleep(2000);
    element(by.xpath("//div[@class='upload-section']/div[2]/div[2]/textarea")).sendKeys("Hi This is my document.");
    element(by.name("email")).sendKeys("idanvcita@gmail.com");
    element(by.name("first_name")).click();
    browser.driver.sleep(3000);

    //expect(element(by.css("span.welcome-back")).isPresent()).toBe(true);

    captureScreen("documentFormReturenedClient");
    browser.sleep(2000);
    element(by.css("input.btn")).click();
    browser.sleep(2000);

    // expect (element(by.linkText("Document Submitted Successfully")).isPresent()).toBe(true);

    captureScreen("documentSubmittedSuccesfully");
    browser.sleep(2000);
    element(by.xpath("//div[@class='actions']//a[.='Home']")).click();
    browser.sleep(2000);

    expect (browser.getCurrentUrl()).toContain('www.vcita.com/v/');

  });

});

这是我最后一次尝试:

var reviewElement = element(by.bind('selectedFiles[0].name')); 
expect(reviewElement.getText()).toEqual("myDocument.docx");

有人知道这有什么问题吗?

量角器的错误如下:

firefox #1] PID: 10036
[firefox #1] Specs: c:\automation\tests\document.js
[firefox #1]
[firefox #1] Using the selenium server at http://localhost:4444/wd/hub
[firefox #1] LiveSite Portal - Client share a document
[firefox #1]   LiveSite - Home Page - pass
[firefox #1]   LiveSite Portal - Client share a document - Load new document - f
ail
[firefox #1]
[firefox #1]
[firefox #1] Failures:
[firefox #1]
[firefox #1]   1) LiveSite Portal - Client share a document  LiveSite Portal - C
lient share a document - Load new document
[firefox #1]    Message:
[firefox #1]      TypeError: undefined is not a function
[firefox #1]    Stacktrace:
[firefox #1]      TypeError: undefined is not a function
[firefox #1]     at [object Object].<anonymous> (c:\automation\tests\document.js
:19:36)
[firefox #1]     at C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\no
de_modules\jasminewd\index.js:94:14
[firefox #1]     at [object Object].promise.ControlFlow.runInFrame_ (C:\Users\id
an\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\l
ib\webdriver\promise.js:1877:20)
[firefox #1]     at [object Object].promise.ControlFlow.runEventLoop_ (C:\Users\
idan\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver
\lib\webdriver\promise.js:1755:8)
[firefox #1]     at [object Object].<anonymous> (C:\Users\idan\AppData\Roaming\n
pm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise
.js:2056:12)
[firefox #1]     at goog.async.run.processWorkQueue (C:\Users\idan\AppData\Roami
ng\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\ru
n.js:125:21)
[firefox #1]     at runMicrotasksCallback (node.js:337:7)
[firefox #1]     at process._tickCallback (node.js:355:11)
[firefox #1] From: Task: Asynchronous test function: it()
[firefox #1]     at [object Object].<anonymous> (C:\Users\idan\AppData\Roaming\n
pm\node_modules\protractor\node_modules\jasminewd\index.js:93:33)
[firefox #1]     at [object Object].<anonymous> (C:\Users\idan\AppData\Roaming\n
pm\node_modules\protractor\node_modules\minijasminenode\lib\async-callback.js:45
:37)
[firefox #1]     at [object Object].jasmine.Block.execute (C:\Users\idan\AppData
\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.
3.1.js:1174:17)
[firefox #1]     at [object Object].jasmine.Queue.next_ (C:\Users\idan\AppData\R
oaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.
1.js:2209:31)
[firefox #1]     at [object Object].jasmine.Queue.start (C:\Users\idan\AppData\R
oaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.
1.js:2162:8)
[firefox #1]     at [object Object].jasmine.Spec.execute (C:\Users\idan\AppData\
Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3
.1.js:2503:14)
[firefox #1]     at [object Object].jasmine.Queue.next_ (C:\Users\idan\AppData\R
oaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.
1.js:2209:31)
[firefox #1]     at onComplete (C:\Users\idan\AppData\Roaming\npm\node_modules\p
rotractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2205:18)
[firefox #1]     at [object Object].jasmine.Spec.finish (C:\Users\idan\AppData\R
oaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.
1.js:2477:5)
[firefox #1]     at [object Object].onComplete (C:\Users\idan\AppData\Roaming\np
m\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2504
:10)==== async task ====
[firefox #1] Error
[firefox #1]     at [object Object].<anonymous> (c:\automation\tests\document.js
:7:3)
[firefox #1]     at [object Object].jasmine.Env.describe_ (C:\Users\idan\AppData
\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.
3.1.js:913:21)
[firefox #1]     at [object Object].jasmine.Env.describe (C:\Users\idan\AppData\
Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3
.1.js:898:15)
[firefox #1]     at describe (C:\Users\idan\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
[firefox #1]     at Object.<anonymous> (c:\automation\tests\document.js:1:63)
[firefox #1]
[firefox #1] Finished in 19.558 seconds
[firefox #1] 2 tests, 2 assertions, 1 failure
[firefox #1]

首先您需要使用一些定位器来识别元素。请参阅 https://angular.github.io/protractor/#/ 编写测试 部分

var reviewElement= element(by.xxx()); 

expect (reviewElement.getText('Review your information').isPresent()).toBe(true);

如果有帮助,请告诉我!

元素的 getText() 上没有可用的 isPresent() 函数。

你的意思可能是:

var reviewElement = element(by.bind('selectedFiles[0].name')); 
expect(reviewElement.isPresent()).toBe(true);  

如果您想验证元素中是否存在任何文本:

expect(reviewElement.getText()).toBeTruthy();

或者,通过 jasmine-matchers 提供的自定义 toBeNonEmptyString() 匹配器:

expect(reviewElement.getText()).toBeNonEmptyString();

或者,如果要检查文本是否等于 smth,请使用 toEqual():

expect(reviewElement.getText()).toEqual("My text");