Java 如何向使用SeleniumWebDriver处理的HTTP请求添加头或参数?
我正在使用SeleniumWebDriver对web应用程序进行单元测试。它在JUnit测试中使用,尽管我广泛阅读了可用文档并四处搜索,但我找不到一种方法:Java 如何向使用SeleniumWebDriver处理的HTTP请求添加头或参数?,java,post,selenium,junit,webdriver,Java,Post,Selenium,Junit,Webdriver,我正在使用SeleniumWebDriver对web应用程序进行单元测试。它在JUnit测试中使用,尽管我广泛阅读了可用文档并四处搜索,但我找不到一种方法: 向驱动程序传递的HTTP请求添加头 向这样的请求添加参数,就像驱动程序在提交表单后获得了他的目标URL一样 可以使用适当的表单创建一个测试网页,并让Webdriver自动跳转以获取这些参数,但这是一个相当丑陋的黑客行为。我想避免它,特别是为了测试原子性。(这是单元测试。) 在Wendriver之前,我使用Spring的MockHttpS
- 向驱动程序传递的HTTP请求添加头
- 向这样的请求添加参数,就像驱动程序在提交表单后获得了他的目标URL一样
在Wendriver之前,我使用Spring的MockHttpServletRequest和MockHttpServletResponse来实现这一点,这很有魅力,但我想使用Webdriver的强大功能来断言目标页面的内容。您可以尝试评估browsermob代理。它有助于操纵标题。
. 与webdriver集成很简单。您只需使用设置的代理值启动驱动程序。可能对正在寻找解决方案的其他人有用 这就是我在我的案例中解决问题的方法。希望对任何具有类似设置的人都有帮助
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(C://Downloads//modheader//modheader.crx));
// Set the Desired capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
// Instantiate the chrome driver with capabilities
WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
Javascript
token-1
,value-1
,token-2
,value-2
是要添加的请求头和值
driver.get(“您想要的网站”)代码>
这对我不起作用。设置后,当我在浏览器上打开扩展时,标题不会显示。我还认为扩展没有将我通过UI(扩展)输入的值存储在本地存储中
// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");
Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
((Javascript)driver).executeScript(
"localStorage.setItem('profiles', JSON.stringify([{ title: 'Selenium', hideComment: true, appendMode: '',
headers: [
{enabled: true, name: 'token-1', value: 'value-1', comment: ''},
{enabled: true, name: 'token-2', value: 'value-2', comment: ''}
],
respHeaders: [],
filters: []
}]));");