htmlunit .net MVC 视图单元测试
我正在使用Java中的htmlUnit(转换为在.net中运行)此处详细信息
我使用与示例相同的逻辑,使用 Web 客户端,这与我的 MVC3 Razor 代码/视图完美配合。
当我的视图上方有 [Authorize]
时,就会出现问题,因为 Web 客户端无法访问该页面。有办法解决这个问题吗?我如何(如果可能)将其设置为自动登录?如果我只是解析登录页面并使用管理员/密码进行提交..这是否会延续到
我尝试过的此“会话”中访问的每个页面:
[SetUp]
public void Setup()
{
//Initiate WebClient
webClient = new WebClient();
//Login
var loginPage = (HtmlPage)webClient.getPage(properties.DomainNameToTest + "Account/Logon/");
((HtmlInput)loginPage.getElementByName("UserName")).setValueAttribute(properties.adminUsername);
((HtmlInput)loginPage.getElementByName("Password")).setValueAttribute(properties.adminPassword);
var action = (HtmlPage)loginPage.getElementByName("loginButton").click();
}
这工作正常。
I am using htmlUnit from Java (Converted to run in .net) Details here
I am using the same logic as the example, using the web client and this works perfectly with my MVC3 Razor code/views.
The problem occurs when I have [Authorize]
above my views as they webclient can't access the page. Is there a way around this? How would I (if possible) set it to automatically log on? If I just parse a login page and do a submit with Admin/Password.. will this carry over to every page visited in this "session"
I have tried:
[SetUp]
public void Setup()
{
//Initiate WebClient
webClient = new WebClient();
//Login
var loginPage = (HtmlPage)webClient.getPage(properties.DomainNameToTest + "Account/Logon/");
((HtmlInput)loginPage.getElementByName("UserName")).setValueAttribute(properties.adminUsername);
((HtmlInput)loginPage.getElementByName("Password")).setValueAttribute(properties.adminPassword);
var action = (HtmlPage)loginPage.getElementByName("loginButton").click();
}
This works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HtmlUnit 是一个网络浏览器。它只是没有图形用户界面:您必须使用代码告诉它要做什么。如果您能够使用常规 Web 浏览器访问您的页面,那么只需对 HtmlUnit 执行相同的操作,它就会按预期工作。
HtmlUnit is a web browser. It just doesn't have a graphical user interface: you have to tell it what to do using code. If you're able to access your page using a regular web browser, then just do the same thing with HtmlUnit, and it will work as expected.