如何操纵"文本"在Jasmine JS的Webdriver中?

时间:2015-05-06 07:32:14

标签: javascript node.js webdriver jasmine protractor

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

通常在尝试阅读此验证时测试失败或者接收到" 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]

2 个答案:

答案 0 :(得分:2)

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

你可能意味着:

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

答案 1 :(得分:0)

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

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

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

请告诉我这是否有帮助!