【问题标题】:Protractor: Is it possible to test that there are no 404 in my app?量角器:是否可以测试我的应用程序中没有 404?
【发布时间】:2015-08-25 12:56:42
【问题描述】:

我是量角器的新手,我想写一个测试看看没有带有 url 的锚点给出 404 错误。

我看过这个How to test html links with protractor?,但只针对一个确定的链接,我想对页面中的所有链接都这样做。

测试应该通过 http 状态 200,如此处所述How to use protractor to get the response status code and response text?

我有两个问题:

  • 这个测试在量角器中有意义吗?
  • 是否可以对此进行测试?如果有,怎么做?

【问题讨论】:

    标签: angularjs testing protractor e2e-testing


    【解决方案1】:

    我想把它实现为一个页面对象,这样我就可以对每个页面规范文件使用一个简单的一行期望语句。事后我认为这可以使用 API 测试框架(例如cheerio.js)以更简单的方式实现,但这里是如何使用量角器和 jasmine 实现它(使用 ES2015 语法所以 update node 到当前版本)!请记得安装 request、bluebird 和 request-promise npm 包。

    PageObject.js

    crawlLinks(){
    
        const request = require('request');
        const Promise = require('bluebird');
        const rp = require('request-promise');
    
        return $$('a').then(function(elems){
          return Promise.map(elems, elem => {
            return elem.getAttribute("href").then(function(url){
              if(url){
                var options = {
                  method: 'GET',
                  uri: url,
                  resolveWithFullResponse: true
                };
                return rp(options).then(function(response){
                  console.log('The response code for ' + url + ' is ' + response.statusCode);
                  return response.statusCode === 200;
                });
              }
            });
          }).then((allCodes) => {
            console.log(allCodes);
            return Promise.resolve(allCodes);
          });
        });
      }
    

    测试

    it("should not have broken links", function(){
         expect(pageObject.crawlLinks()).not.toContain(false);
    });
    

    【讨论】:

    • 在我看来,Promisebluebird 的使用已经过时了。这不是在 Protractor 5.x 中直接解决的吗?
    • 我在 Protractor 5.x 发布之前回答了这个问题,所以是的,非常过时。
    【解决方案2】:

    我认为它绝对可行,如果范围有限,这样做是有意义的,因为这不是 selenium-webdriver 用于的典型 UI 测试。您可以执行类似的操作,找到所有链接,进入 url 并使用 request 之类的模块触发 GET 请求。这是一个伪代码。

    var request = require('request');
    var assert = require('assert');
    element.all(by.tagName('a')).then(function(link) {
       var url = link.getAttribute('href');
       if(url) {
           request(url, function (error, response, body) {
               assert.equal(response.statusCode, 200, 'Status code is not OK');
           });
        }
    });
    

    【讨论】:

    • 我收到错误 -> TypeError: Cannot read property 'statusCode' of undefined at Request._callback
    • 这表明你得到了一个error。你能打印错误吗?
    【解决方案3】:

    404 错误出现在浏览器控制台中(至少在 chrome 中出现),您可以从量角器访问它

    browser.manage().logs().get('browser').then(function(browserLogs) {
       browserLogs.forEach(function(log){
          expect(log).toBeFalsy();
       });
    });
    

    上面的代码将导致所有控制台消息被视为测试失败,您可以根据自己的需要进行调整。您可以在所有测试中将类似的代码放在afterAll 中。

    【讨论】:

      【解决方案4】:

      这里我写了一个java demo,可以满足你的要求。另外我对量角器不熟悉,但希望这可以帮助

      package com.selenium.webdriver.test;
      
      import java.io.IOException;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      
      import org.apache.commons.httpclient.HttpClient;
      import org.apache.commons.httpclient.HttpException;
      import org.apache.commons.httpclient.methods.GetMethod;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.htmlunit.HtmlUnitDriver;
      
      public class Traverse {
      
         private WebDriver driver;
         private String baseUrl;
         private Map<String, String> tMap;
      
      public Traverse(String url) {
          driver = new HtmlUnitDriver();
          baseUrl = url;
          tMap = new HashMap<String,String>();
      }
      
      
      //get status code.
      public int getRespStatusCodeByGet(String url) throws HttpException, IOException {
          GetMethod method = new GetMethod(url);
          HttpClient client = new HttpClient();
      
          client.executeMethod(method);
      
          return method.getStatusCode();
      }
      
      //single page traversal
      public boolean search(String url) throws HttpException, IOException {
          if(getRespStatusCodeByGet(url) != 200) {
              System.out.println("Bad page " + url);
              return false;
          }
          driver.get(url);
      
          List<WebElement> elements = driver.findElements(By.tagName("a"));
      
          for(int i=0;  i<elements.size(); i++) {
              String cUrl = elements.get(i).getAttribute("href");
              String cText = elements.get(i).getText();
      
              if(cUrl != null && cUrl.startsWith("http") && !tMap.containsKey(cText)) {
      
                  tMap.put(cText, cUrl);
      
                  System.out.println(cUrl);
      
                  search(cUrl);
      
      
              }
      
          }
          return true;
      }
      
      //client
      public static void main(String[] args) throws HttpException, IOException {
          Traverse t = new Traverse("http://www.oktest.me/");
          t.search(t.baseUrl);
      }
      }
      

      检查坏页你可以得到你想要的。

      【讨论】:

      • 问题是 javascript/量角器特定的。
      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多