简体   繁体   English

量角器:计算所有超链接并在页面上测试它们

[英]Protractor: Count all hyperlinks and test them on a page

I am trying to count the number of hyperlinks on a page and test them to see if they work. 我试图计算页面上的超链接数量,并测试它们是否有效。 I have the ability to count all the links and display that count in JavaScript but I can't seem to return that value in protractor on Command Line. 我有能力计算所有链接并在JavaScript中显示该计数,但我似乎无法在命令行上的量角器中返回该值。

Update Code based on Answers: 根据答案更新代码:

browser.waitForAngularEnabled(false);
describe('Clicks on the correct Drupal hyperlink', function() {

  it('should find all links', function () {
    browser.get('file:///C:/Users/Dasman/Documents/PROTRACTOR_E2E_TESTING/TestSite.html');
    let allLinks = element.all(by.tagName('a'));
    allLinks.count().then(function(link_tally){
      console.log('There are a total of ' + link_tally + " links on this page with proper tags.")
    })
  browser.sleep(2000);

  // A Protracterized httpGet() promise
function httpGet(siteUrl) {
  var http = require('http');
  var defer = protractor.promise.defer();

  http.get(siteUrl, function(response) {

      var bodyString = '';

      response.setEncoding('utf8');

      response.on("data", function(chunk) {
          bodyString += chunk;
      });

      response.on('end', function() {
          defer.fulfill({
              statusCode: response.statusCode,
              bodyString: bodyString
          });
      });

  }).on('error', function(e) {
      defer.reject("Got http.get error: " + e.message);
  });

  return defer.promise;
}

it('should return 200 and contain proper body', function() {
  httpGet(allLinks).then(function(result) {
    allLinks.count().then(function(statusCode){
      console.log('Status code is: ' + statusCode)
    })
      expect(result.statusCode).toBe(200);
      expect(result.bodyString).toContain('Apache');
  });
});
    });
});

In addition I want to "check" the links to see if they open. 此外,我想“检查”链接,看看它们是否打开。 Is there a way to go to a URL and click all the links and have them open on separate windows? 有没有办法转到URL并单击所有链接并在单独的窗口中打开它们? or get an attribute if the link works or has a valid URL? 或者如果链接有效或具有有效的URL,则获取属性?

I originally xpath the id and clicked the link and as the test ran - I visually verified. 我最初xpath的id并点击链接,并在测试运行时 - 我在视觉上验证。 But with 15-20 links on a average page - I need a more automated way. 但是在平均页面上有15-20个链接 - 我需要一种更自动化的方式。

Ernst's answer pointed me to the solution, but it did need some code restructuring and encapsulation: https://github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/LinkCheck 恩斯特的回答指出了解决方案,但它确实需要一些代码重组和封装: https //github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/LinkCheck

To report your count in the console you can do this: 要在控制台中报告您的计数,您可以执行以下操作:

//find all links
let linkCount = element.all(by.css('a'));

// count links, with then() resolve promise and log count result
linkCount.count().then(function(cnt){
    console.log('Total links = '+cnt);
});

//here you click each of the links:
linkCount.click()

You can find out more about count() , then() and element.all() here: http://protractortest.org/#/api 你可以在这里找到更多关于count()then()element.all()信息: http//protractortest.org/#/api

To check the response code of your links, it will be a bit tricky, because Protractor doesn't support such requests out of the box ( see here ). 要检查链接的响应代码,这将有点棘手,因为Protractor不支持开箱即用的此类请求( 请参阅此处 )。

However, there are two SO-Posts here and here , which provide a way to do it. 但是, 这里这里有两个SO-Posts,提供了一种方法。

To get it tested one by one you should be able to use getAttribute('href') . 要逐个测试,您应该能够使用getAttribute('href') It would be something like this (Note: not tested, but partly copied from above two mentioned SO-Answers) 它会是这样的(注意:没有经过测试,但部分复制了上面两个提到的SO-Answers)

linkCount.each(function(elem){
    elem.getAttribute('href').then(function(link){
        this.httpGet("http://localhost:80").then(function(result) {
            expect(result.statusCode).toBe(200);
        });
    });
});

// A Protracterized httpGet() promise
this.httpGet = function(siteUrl) {
    var http = require('http');
    var defer = protractor.promise.defer();

    http.get(siteUrl, function(response) {

        var bodyString = '';

        response.setEncoding('utf8');

        response.on("data", function(chunk) {
            bodyString += chunk;
        });

        response.on('end', function() {
            defer.fulfill({
                statusCode: response.statusCode,
                bodyString: bodyString
            });
        });

    }).on('error', function(e) {
        defer.reject("Got http.get error: " + e.message);
    });

    return defer.promise;
}

You can also try to get Text from each link and count all array element like this: 您还可以尝试从每个链接获取Text并计算所有数组元素,如下所示:

var allLinks = element.all(By.tagName('a'));
allLinks.getText().then(function(findAllLink) {
var numberofLinks = findAllLink.length;
console.log(numberofLinks);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM