Node.JS - 如何在JS中对动态文件名执行验证?

时间:2016-02-08 08:44:34

标签: javascript node.js selenium-webdriver jasmine protractor

我试图在“split()”中使用,以便忽略文件名中的动态字符串,并仅对固定值“Schedule”执行验证,然后返回响应以通过测试。

describe('Frontage - Calendar test cases', function() {

    var fs = require('fs');
    var splitFileName = "Schedule"
    var filename = 'c:\\automation\\tests\\downloaded\\' + splitFileName.split();

    it('New App - Calendar - Print calendar', function(){
       $$("button.font-xs.md-button.ng-scope.md-ink-ripple.gray-btn").get(1).click();
       browser.driver.wait(function() {
          return fs.existsSync(filename);
       }, 30000);
    });
});

错误

Failures:

  1) Test cases pull - Frontage Sanity Testing  Frontage - Calendar test cases New App - Calendar - Print calendar
   Message:
     Error: Wait timed out after 30000ms
   Stacktrace:
     Error: Wait timed out after 30000ms
    at C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1630:20
    at [object Object].promise.ControlFlow.runInFrame_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)
    at [object Object].promise.Callback_.goog.defineClass.notify (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2464:25)
    at [object Object].promise.Promise.notify_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:563:12)
    at Array.forEach (native)
    at Object.goog.array.forEach (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\array\array.js:203:43)
    at [object Object].promise.Promise.notifyAll_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:552:16)
    at goog.async.run.processWorkQueue (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21)
From: Task: <anonymous wait>
    at [object Object].promise.ControlFlow.wait (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1617:15)
    at [object Object].webdriver.WebDriver.wait (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:714:21)
    at [object Object].<anonymous> (C:\automation\tests\testCases\newApp\calendarTC.js:382:23)
    at C:\automation\npm\node_modules\protractor\node_modules\jasminewd\index.js:94:14
    at [object Object].promise.ControlFlow.runInFrame_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)
    at [object Object].promise.ControlFlow.runEventLoop_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1755:8)
    at [object Object].<anonymous> (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2056:12)
    at goog.async.run.processWorkQueue (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21)
    at runMicrotasksCallback (node.js:337:7)
    at process._tickCallback (node.js:355:11)
From: Task: Asynchronous test function: it()
    at [object Object].<anonymous> (C:\automation\npm\node_modules\protractor\node_modules\jasminewd\index.js:93:33)
    at [object Object].<anonymous> (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\async-callback.js:45:37)
    at [object Object].jasmine.Block.execute (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:1174:17)
    at [object Object].jasmine.Queue.next_ (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31)
    at [object Object].jasmine.Queue.start (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2162:8)
    at [object Object].jasmine.Spec.execute (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2503:14)
    at [object Object].jasmine.Queue.next_ (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31)
    at onComplete (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2205:18)
    at [object Object].jasmine.Spec.finish (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2477:5)
    at [object Object].onComplete (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2504:10)==== async task ====
Error
    at [object Object].<anonymous> (C:\automation\tests\testCases\newApp\calendarTC.js:380:5)
    at [object Object].jasmine.Env.describe_ (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:913:21)
    at [object Object].jasmine.Env.describe (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15)
    at describe (C:\automation\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
    at Object.<anonymous> (C:\automation\tests\testCases\newApp\calendarTC.js:1:63)

1 个答案:

答案 0 :(得分:0)

您可以使用fs.readdir获取所有文件名,然后使用正则表达式针对您要匹配的内容检查它们。

it('New App - Calendar - Print calendar', function(){
   $$("button.font-xs.md-button.ng-scope.md-ink-ripple.gray-btn").get(1).click();
   browser.driver.wait(function() {
      var filepath = 'c:\\automation\\tests\\downloaded\\';
      var matcher = /Schedule/;
      var foundFile = false;
      fs.readdirSync(filepath).forEach(function(filename) {
          if (matcher.test(filename)) {
             foundFile = true;
           }
       });
       return foundFile;
   }, 30000);
});