網(wǎng)站首頁 編程語言 正文
Selenium.WebDriver
Selenium WebDriver 是一組開源 API,用于自動測試 Web 應(yīng)用程序,利用它可以通過代碼來控制chrome瀏覽器!
有時候我們需要mock接口的返回,或者攔截和轉(zhuǎn)發(fā)請求,今天就來實現(xiàn)這個功能
代碼已開源: https://github.com/yuzd/OpenQA.Selenium.Chrome.Fiddler
nuget
OpenQA.Selenium.Chrome.Fiddler
開始coding
我們新創(chuàng)建一個功能:OpenQA.Selenium.Chrome.Fiddler
一個chrome擴展 最起碼有2個文件
manifest.json
background.js
稍微解釋一下:
manifest.json 是來描述chrome擴展的
{ "version": "1.0.0", "manifest_version": 2, "name": "Chrome Fiddler", "permissions": [ "proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>", "webRequest", "webRequestBlocking" ], "background": { "scripts": ["background.js"] }, "minimum_chrome_version":"22.0.0" }
background.js 是邏輯處理模塊
因為攔截api 或者 轉(zhuǎn)發(fā) 需要用的chrome的api
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
//邏輯處理
},
{ urls: ['<all_urls>']},
['blocking', 'extraHeaders', 'requestBody']
);
這個api的函數(shù) 接收的details參數(shù)
- details.url 是api的接口
函數(shù)的返回
- {cancel:true} 攔截請求
- {redirectUrl:''} 轉(zhuǎn)發(fā)到指定url
寫selenium.chrome插件
- 新建一個netstand工程,然后引用
Selenium.WebDriver
復(fù)制以下代碼
/// <summary>
/// Add Fiddler extention
/// </summary>
/// <param name="options">Chrome options</param>
/// <param name="fiddlerOption">Proxy host</param>
public static void AddFiddler(this ChromeOptions options, FiddlerOption fiddlerOption)
{
var backgroundProxyJs = ReplaceTemplates(background_js, fiddlerOption);
if (!Directory.Exists("Plugins"))
Directory.CreateDirectory("Plugins");
var guid = Guid.NewGuid().ToString();
var manifestPath = $"Plugins/manifest_{guid}.json";
var backgroundPath = $"Plugins/background_{guid}.js";
var archiveFilePath = $"Plugins/proxy_auth_plugin_{guid}.zip";
File.WriteAllText(manifestPath, manifest_json);
File.WriteAllText(backgroundPath, backgroundProxyJs);
using (var zip = ZipFile.Open(archiveFilePath, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(manifestPath, "manifest.json");
zip.CreateEntryFromFile(backgroundPath, "background.js");
}
File.Delete(manifestPath);
File.Delete(backgroundPath);
options.AddExtension(archiveFilePath);
}
private static string ReplaceTemplates(string str, FiddlerOption fiddlerOption)
{
if (fiddlerOption.OnBeforeRequestOptions != null)
{
var beforeConfigs = Newtonsoft.Json.JsonConvert.SerializeObject(fiddlerOption.OnBeforeRequestOptions);
str = str.Replace("{before_configs}", beforeConfigs);
}
return str;
}
上面的代碼主要是創(chuàng)建一個chrome擴展zip包
然后再selenium.chrome啟動的時候傳進去這個zip包的地址
使用方法
var driverBinary = @"D:\soft\chrome\chrome2\Chrome-bin\";
ChromeOptions options = new ChromeOptions
{
BinaryLocation = Path.Combine(driverBinary, "chrome.exe")
};
Environment.SetEnvironmentVariable("webdriver.chrome.driver", driverBinary);
options.AddArgument("--disable-blink-features=AutomationControlled");
options.AddArguments("--disable-infobars");
List<string> ls = new List<string> { "enable-automation" };
options.AddExcludedArguments(ls);
#region Fillder
options.AddFiddler(new FiddlerOption
{
OnBeforeRequestOptions = new List<FiddlerOnBeforeRequestOptions>
{
// 配置轉(zhuǎn)發(fā)
new FiddlerOnBeforeRequestOptions
{
Match = "https://www.cnblogs.com/yudongdong/ajax/GetPostStat",//正則
RedirectUrl = "http://localhost:5000/GetPostStat",//如果匹配成功則將requestBody轉(zhuǎn)發(fā)到這個url中去
Cancel = false//如果配置了cancel=true那么轉(zhuǎn)發(fā)將無效,true的意思是直接攔截這次的請求,不去發(fā)送了
},
// 配置攔截
new FiddlerOnBeforeRequestOptions
{
Match = "https://www.cnblogs.com/yudongdong/ajax/blogStats",
Cancel = true//true的意思是直接攔截這次的請求,不去發(fā)送了
},
}
});
#endregion
var chrome = new ChromeDriver(driverBinary, options);
實現(xiàn)效果
原文鏈接:https://www.cnblogs.com/yudongdong/p/16503468.html
相關(guān)推薦
- 2024-07-15 arthas操作spring被代理目標對象命令速查
- 2023-02-01 Python局部函數(shù)及用法詳解(含nonlocal關(guān)鍵字)_python
- 2024-02-28 UNI-APP中,swiper和tabbar結(jié)合實現(xiàn)滑動翻頁效果
- 2022-11-26 React從插槽、路由、redux的詳細過程_React
- 2022-03-16 Docker安裝Nginx問題及錯誤分析_docker
- 2022-03-28 python?Pandas中數(shù)據(jù)的合并與分組聚合_python
- 2022-08-31 ES6變量賦值和基本數(shù)據(jù)類型詳解_基礎(chǔ)知識
- 2022-04-20 淺談C語言的變量和常量_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支