CasperJS - 屏幕截图返回黑色图像&当目标url不是localhost时,测试用例找不到元素

时间:2015-12-03 16:22:04

标签: javascript phantomjs casperjs

我正在使用CasperJs和Phantomjs 1.9.8。我正在测试使用Windows身份验证的网站。我正在测试部署在远程服务器上和本地的版本。

以下是测试用例。

var baseUrl = "http://SNG09034236:9999";   //For testing app on remote server

//var baseUrl = "http://localhost:9999";  //For testing app locally

casper.options.viewportSize = {
  width: 1024,
  height: 800
}

casper.pageSetting = {userName: 'myuid', password:pwd};

casper.options.waitTimeout = 5000;

casper.on('resource.requested', function(requestData, networkRequest) {
     //console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData) + "\n");
});
casper.on("resource.received", function(response){
    // console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response) + "\n");
});



casper.test.begin('Connecting to application', function suite(test) {
   casper.start(baseUrl, function() {
       document.querySelector("body").style.height = "700px";
       this.echo("TITLE : ====> " + this.getTitle());
       this.captureSelector('c:\\temp\\myImage.png', 'html');
  })
   .then(function(){
     test.assertTitle("My app", "Title is correct");
   })
   .waitForSelector("#showUser",
       function success() {
           test.assertExists("#showUser", "Shadow button found");
           this.click("#showUser");
       },
       function fail() {
         console.log('Shadow button NOT found');
           test.assertExists("#showUser");
   })
   .waitForSelector("#show-window-body",
       function success() {
           test.assertExists("#show-window-body", "Show div found");
           this.click("#show-window-body");
       },
       function fail() {
           test.assertExists("#show-window-body");
   })
   .waitForSelector("input[name='search_user-inputEl']",
       function success() {
          test.assertExists("input[name='search_user-inputEl']", "Show text box found");
          this.click("input[name='search_user-inputEl']");
          this.sendKeys('#search_user-inputEl', 'tomjspn3');
       },
       function fail() {
           test.assertExists("input[name='search_user-inputEl']");
   })
   .waitForSelector("li",
       function success() {
           test.assertExists("li", "User found");
           this.click("li");
       },
       function fail() {
           test.assertExists("li", "User NOT found");
   })
  .run(function() {
    casper.echo("TESTS COMPLETED");
    casper.exit();
  });
});

的问题:

  1. this.captureSelector('c:\\temp\\myImage.png', 'html');始终返回黑色图像文件(用于本地和远程执行的网站)。但如果测试另一个网站(google.com等),我会得到正确的截图。
  2. 在测试用例中,我正在搜索showUser div。当运行针对托管在本地机箱上的站​​点的测试用例时,一切正常,当我通过将URL更改为远程服务器来运行相同的测试用例时,系统找不到showUser div并且不生成任何结果
  3. 实际上在添加各种钩子后,我可以在连接到远程站点时看到以下错误:

    Response (#1, stage "end"): {"contentType":null,"headers":[],"id":1,"redirectUR
    ":null,"stage":"end","status":401,"statusText":"Authorization Required","time":
    2015-12-03T17:26:15.691Z","url":"http://SNG09034236:9999/"}
    
    ResourceError: {
        "errorCode": 5,
        "errorString": "Operation canceled",
        "id": 0,
        "url": "http://SNG09034236:9999/"
    }
    

    我也改变了casper.pageSetting to casper.options.pageSetting

0 个答案:

没有答案