我是htmlunit的新手,需要对用htmlunit编写的网页进行测试。该登录页面中没有表单。
以下是用户名,密码和提交按钮的HTML代码 -
<div class="loader-mask" data-bind="visible: false">
<div class="loader"> Loading Form...</div>
</div>
<div class="form-group">
<label class="control-label">Email ID:</label>
<input type="text" name="username" class="form-control" placeholder="user@company.com" data-bind="
value: loginHandler.userEmail,
enterKey: signIn
">
</div><!-- /.form-group -->
<div class="form-group">
<label class="control-label">Password:</label>
<input type="password" name="password" class="form-control" placeholder="Passwords are case-sensitive" data-bind="
value: loginHandler.userPassword,
enterKey: signIn
">
</div><!-- /.form-group -->
<div class="text-right">
<p><button type="button" id="qaSignIn" class="btn btn-default" data-bind="
click: signIn,
disable: loginHandler.isLoggingIn,
continueOn: userLoggedIn,
continueLink: { link: 'dashboard.html', params: {} }
">
现在,在htmlunit,我可以轻松获取用户名,密码和按钮,我也可以设置用户名和密码字段的值,但是当我点击按钮时,它返回相同的html登录页。我已经尝试使用浏览器上的凭据登录,我可以在那里登录。凭证很好。
我的htmlunit代码是 -
HtmlTextInput htmlInputUsername = (HtmlTextInput) loginPage.getElementByName("username");
htmlInputUsername.setValueAttribute("myUserName");
HtmlPasswordInput htmlInputPassword = (HtmlPasswordInput) loginPage.getElementByName("password");
htmlInputPassword.setValueAttribute("myPassword");
HtmlButton htmlButton = (HtmlButton) loginPage.getElementById("qaSignIn");
loginPage = htmlButton.click();
我在点击登录按钮之前和之后尝试打印page.asXml(),但它返回相同的xml页面。知道我做错了什么吗? 我甚至尝试过设置这些选项 -
webClient.getOptions().setUseInsecureSSL(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.waitForBackgroundJavaScript(30000);
提前致谢。
答案 0 :(得分:1)
如果不知道分析您网页的网址,我只能猜测。
首先尝试启用javascript
webClient.getOptions().setJavaScriptEnabled(true);
第二次
webClient.waitForBackgroundJavaScript(30000);
不是一种选择。在WebClient设置期间执行此操作是无用的。点击
后,您必须拨打此电话loginPage = htmlButton.click();
webClient.waitForBackgroundJavaScript(30000);
也许可以选择使用WETATOR(www.wetator.org)进行测试。与网页的交互更加简单,所有等待魔法都会自动完成。
答案 1 :(得分:0)
js ajax可能有这样的代码“window.location.href”。那么你应该这样做:
Task("Deploy")
.IsDependentOn("Build")
.Description("Deploy build website")
.Does(() =>
{
DeployWebsite(new DeploySettings()
{
SourcePath = "./Examplesite/obj/Release/Package/Examplesite.CI.MVCApp.zip",
SiteName = "BuildAuto",
Port=8082,
ComputerName = "NET1-7103",
Username = "labuser",
Password = "*****"
});
});
Task("build")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
try {
MSBuild(currentDirectoryAppInfo + @"\Examplesite.CI.MVCApp.sln", new MSBuildSettings()
.SetConfiguration(configuration)
.WithProperty("DeployOnBuild","true")
.SetVerbosity(Verbosity.Minimal));
}
catch(Exception ex) {
buildStatus = false;
}