2017-12-06 10 views
1

ページ上のハイパーリンクの数を数え、それらが動作するかどうかをテストしようとしています。私はすべてのリンクを数え、その数をJavaScriptに表示する能力を持っていますが、私はコマンドラインで分度器にその値を返すように見えません。回答に基づいて分度器:すべてのハイパーリンクを数えてページ上でテストする

アップデートコード:

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'); 
    }); 
}); 
    }); 
}); 

また私は、彼らが開くかどうかを確認するためのリンクを「チェック」します。 URLにアクセスしてすべてのリンクをクリックして別のウィンドウで開く方法はありますか?リンクが機能しているか、有効なURLを持っている場合は属性を取得しますか?

私は元々idをクリックしてリンクをクリックし、テストが実行されたときに視覚的に確認しました。しかし平均的なページに15〜20のリンクがあります - 私はもっと自動化された方法が必要です。

エルンストの答えは、ソリューションに私を指摘したが、それはいくつかのコードの再構築およびカプセル化を必要としました:https://github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/LinkCheck

+2

私は新しいタブでリンクを開くことについてはよく分からないが、なぜあなただ​​けではありません'element.all(by.css( 'a'))。count()'を使ってページ上のリンク数を取得しますか?あるいは 'element.all(by.tagName( 'a'))。count()'? – tehbeardedone

+0

@tehbeardedone - テストを実行した後、cmd行にカウントを表示するにはどうすればよいですか? – Dasman

+2

'console.log()' – tehbeardedone

答えて

2

//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() 

あなたはFIできますNDアウトよりおよそcount()then()ここelement.all()http://protractortest.org/#/api

あなたのリンクの応答コードを確認するには分度器ボックス(see here)のうち、このような要求をサポートしていませんので、それは、少しトリッキーになります。

しかし、それを行う方法を提供する2つのSO-投稿herehereがあります。

これをテストするには、getAttribute('href')を使用する必要があります。 それはこのようなものになるだろう(注:テストしたが、一部はSO-回答を2以上からコピー言及されていない)

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; 
} 
+0

これはコンソールの出力部分を処理します。 – Dasman

+0

リンクにhrefタグがあることを確認する以外の方法でリンクを「テスト」する方法はありません。 – Dasman

+0

'linkCount.click()'は基本的に各リンクをクリックします。しかし、私はリンクが新しいタブで開くことを前提としていました。同じタブで開くと、そのようには機能しません(最初のクリック後にページを離れたため)。あなたはリンクの "テスト"を正確にしたいのですか?関数ごとに、各リンクに、この行 'expect($$( 'a [href^=" https "]')。count())で" https:// "で始まるhref属性があるかどうかを調べることができます。 (cnt) 'となる。しかし、それは本当にあなたが "テストする"ことに依存します。 –

0

ます。また、各リンクからテキストを取得し、このようなすべての配列要素をカウントしようとすることができます:

あなたがこれを行うことができますコンソールで、あなたの数を報告すること
var allLinks = element.all(By.tagName('a')); 
allLinks.getText().then(function(findAllLink) { 
var numberofLinks = findAllLink.length; 
console.log(numberofLinks); 
}) 
関連する問題