簡體   English   中英

捕獲HTTP流量

[英]Capture HTTP Traffic

精簡版:
我希望確保請求URL(部分匹配)(客戶端)。

長版:
我希望自動化部分測試。 目前我使用Fiddler2手動驗證。

這是場景:

  1. 用戶導航到站點A.
  2. 我的應用使用跟蹤網址重定向(在Fiddler的HTTP流量中看到)
  3. 用戶最終在站點A上,現在應用了參數。

我想在C#中驗證步驟2是通過進行部分匹配(例如包含{string})來實現的。

題:
我該怎么辦呢? 我已經開始研究HttpWebRequest類和FiddlerCore ,但是我喜歡使用最簡單的代碼(所以其他團隊成員如果需要更新)會讓我問一下StackOverflow的用戶會推薦什么。

看看SharpPcap 它基於pcap(Windows上的WinPcap),這是流行的Wireshark使用的數據包捕獲庫。

有一個非常棒的CodeProject教程,有許多示例代碼可以幫助您入門: http//www.codeproject.com/Articles/12458/SharpPcap-A-Packet-Capture-Framework-for-NET

一旦你掌握了數據包(SharpPcap確實捕獲,而不是解析),你可以使用Packet.Net將數據包解析為可用的東西(在你的情況下是HTTP通信)。

編輯:當我閱讀問題時,沒有看到#2作為中間URL,它看起來像是(僅)重定向動作。 根據您選擇瀏覽器和執行的重定向類型 ,您可以使用Selenium讀取頁面引用並獲取重定向。

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;

// Call any javascript
var referrer = js.executeScript("document.referrer");

我會推薦Selenium Webdriver用於C#中的所有網站/應用測試需求。 它與NUnit,MSTest和其他測試框架非常完美地集成 - 它非常易於使用。

使用Selenium Webdriver,您將從C#測試代碼啟動自動瀏覽器實例(Firefox,Chrome,Internet Explorer,PhantomJS等)。 然后,您將使用簡單的命令控制瀏覽器,例如“轉到URL”或“在輸入框中輸入文本”或“單擊按鈕”。 在API中查看更多內容。

它也不需要其他開發人員 - 他們只是運行測試套件,並假設他們安裝了瀏覽器,它將工作。 我已成功地使用它,在開發人員團隊中進行了數百次測試,每個開發人員都有不同的瀏覽器偏好(即使是我們每次調整的測試)和團隊構建服務器。

對於此測試,我將在步驟1中轉到URL ,然后等待一秒 ,並在步驟3中讀取該URL。

以下是一些示例代碼,適用於通過示例介紹Selenium-WebDriver API 由於我不知道您正在尋找的URL和{string} (在此示例中為“cheese”),因此樣本沒有太大變化。

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;

class RedirectThenReadUrl
{
    static void Main(string[] args)
    {
        // Create a new instance of the Firefox driver.

        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.

        // Further note that other drivers (InternetExplorerDriver,
        // ChromeDriver, etc.) will require further configuration 
        // before this example will work. See the wiki pages for the
        // individual drivers at http://code.google.com/p/selenium/wiki
        // for further information.
        IWebDriver driver = new FirefoxDriver();

        //Notice navigation is slightly different than the Java version
        //This is because 'get' is a keyword in C#
        driver.Navigate().GoToUrl("http://www.google.com/");

        // Print the original URL
        System.Console.WriteLine("Page url is: " + driver.Url);

        // @kirbycope: In your case, the redirect happens here - you just have
        // to wait for the new page to load before reading the new values

        // Wait for the page to load, timeout after 10 seconds
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        wait.Until((d) => { return d.Url.ToLower().Contains("cheese"); });

        // Print the redirected URL
        System.Console.WriteLine("Page url is: " + driver.Url);

        //Close the browser
        driver.Quit();
    }
}

聽起來你想要嗅探HTTP流量。 您可以使用像winpcap這樣的數據包捕獲驅動程序,導入該DLL並進行測試,或者使用@SimpleCoder提到的SharpPcap。

最小努力的路徑是寫一個FiddlerScript插件 ,檢查請求並在必要時重定向。

跟進:我最終使用Telerik的代理發送HTTP請求並通過C#解析響應。 這是用作跳板的文章:

https://docs.telerik.com/teststudio/advanced-topics/coded-samples/general/using-the-http-proxy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM