HTMLUnit:确定是否“登录"那是成功的 [英] HTMLUnit : Determine if the "login" was successful

查看:118
本文介绍了HTMLUnit:确定是否“登录"那是成功的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个脚本,其唯一目的是检查网站/服务是否已启动并正在运行.这样做的方法是,它使用其URL连接到页面,并使用用户凭据登录到站点.如果登录成功,则服务运行正常.

I have developed a script whose sole purpose is to check if the website/service is up and running. The way it does that is ,it connects to the page using its url and logs in to the site using the user credentials. If the login is successful then the service is running fine.

此脚本已用Java编写,并使用HTMLUnit.这是我的问题. 如何确保登录后返回的HTML页面(填写表单后单击登录/登录"按钮)是登录后的帐户主页".换句话说,如何确定登录操作是否成功.

This script has been written in Java and uses HTMLUnit. Here lies my problem. How do i ensure the HTML page returned after logging in(clicking the login/sign in button after filling out the form) is the post-login "Account home page".In other words how do i determine if the login operation was successful.

这就是我现在正在做的事情.帐户页面通常包含一些与用户相关的信息.例如,如果我登录yahoo邮件,则该页面右上角将显示"Welcome,Username",否则该页面将始终显示"Compose"或"inbox".我正在使用这种逻辑来测试成功.

Here is how i am doing it right now. Account pages usually have some user related info. For instance if i log in to yahoo mail it will have "Welcome , Username" on the top right corner of the page Or page will always have "Compose" or "inbox" on it. I am using this logic to test for success.

这是我在测试此脚本时的观察结果.我碰到过此规则崩溃的情况.

This has been my observation while testing this script. I have come across cases where this rule falls apart.

  • 登录后返回的页面有时是错误页面,要求您
    检查您输入的凭据.

  • Some times the page returned after login is an error page asking you to
    check your entered credentials.

有时返回的页面可能会要求您打开javascript或
在浏览器中启用Cookie.

There are times where page returned may ask you to turn on your javascript or
enable cookie in your browser.

我遇到了服务器返回的页面被 相同的登录前页面(未提供原因说明)

I have come across a case where the page returned by the server has been the same pre-login page (no explanation given as to why)

某些网页本质上是动态的,因此内容会发生变化 时.在这种情况下,关键字搜索可能会导致错误
底片.这就是为什么这种寻找弦状铰链的逻辑的原因
完全取决于搜索字符串/关键字"的选择.

Some web pages are dynamic in nature hence the content changes from time to time. In such cases key-word search may result in false
negatives.Which is why this logic of searching for presence of a string hinges
purely on the choice of "search string/keywords".

我要提出的观点是,针对这些情况进行预先编码是不现实的.

The point that i am trying to make is that coding for these cases upfront is not realistic.

我尝试比较登录前和登录后页面的网址,但发现了
在很多情况下两者都是相同的.因此,即使这种方法也不是确定性的.

I tried comparing urls of the pre-login and post-login pages but found out
that there are many cases where both are the same.Hence even this method is not conclusive.

我需要确定登录是否成功的可靠方法.我不是专业的Web开发人员.服务器是否在新页面中返回任何状态代码,可以解决? HTMLUnit是否具有测试成功和失败的能力.

I need a sure shot way of determining if the login was successful. I am not a professional web developer. Does the server return any status code with the new page, that can be resolved? Does HTMLUnit have some ability to test for success and failure.

感谢您的帮助/评论.谢谢!!!

I appreciate your help/comments.Thank you!!!

推荐答案

嗯...这是一个棘手的问题.这是因为您无法控制服务器.我要求您A可能会收到A,但您应该准备好接收B,C和D ...并且您可能会想念E.

Well... this is kind of a tricky question. This is because you've no control of the server. I you ask for A you might probably receive A but you should be prepared to receive B, C and D... and you will probably miss E.

我需要确定登录是否成功的可靠方法.

I need a sure shot way of determining if the login was successful.

根据您的评论,查找欢迎使用<用户名>"字符串应该是很确定的镜头.换句话说,如果您在结果页面中拥有该字符串,那么您就可以登录.确定的射击.

Based on your comments, looking for the "Welcome <Username>" string should be quite sure shot. In other (more programmatic) words, if you you have that string in the result page then you are logged in. There is your sure shot.

现在,您已经提到在某些情况下您尝试登录而没有收到该字符串.在这种情况下,根据您的示例,几乎应该永远不要登录.

Now, you've mentioned that there are cases in which you try to log in and you don't receive that string. In those cases, and based on your examples, you should almost always not be logged in.

但是,正如您所说,他们可以从"Welcome,< Username>"更改该字符串.到你又来了!"而且您会得到假阴性.但是,应用这种逻辑不太可能会出现误报.

However, as you said, they can change that string from "Welcome, <Username>" to "There you are again!" and you will be getting false negatives. It is unlikely, however, that you ever get false positives applying that logic.

那么,有没有办法在猜测用户是否已登录时获得100%成功,并且在猜测用户是否未登录时获得100%成功呢?不,没有.理解这一点的最简单方法是以类似人的方式使用网络:

So, is there any way to have a 100% success on guessing if the user is logged in and also 100% success on guessing if the user is not logged in? No, there isn't. The simplest way to understand this is using the web in a human-like way:

方案1:

  1. 尝试登录
  2. 您会看到一个红色标签,上面写着用户名和密码不正确
  3. 您推断您尚未登录.您是对的

方案2:

  1. 尝试登录
  2. 您会看到欢迎使用< Username>"
  3. 您推断您已登录.您是对的

方案3:

  1. 尝试登录
  2. 您收到启用javascript"消息
  3. 您推断您尚未登录.但是,这恰好是来自服务器的建议,并且您实际上已经登录.刷新页面,然后看到欢迎使用.因此,即使您是人工操作,也失败了.

方案4:

  1. 尝试登录
  2. 您超时了
  3. 您推断您尚未登录.但是,登录请求到达了服务器,因此服务器使用HtmlPage答复时,您的Internet连接,ISP或 Internet 都中断了下降一毫秒,您的数据包就丢失了.所以在这里,即使是人类,您也失败了
  1. Try to log in
  2. You get a timeout
  3. You infer you are not logged in. However, the login request got to the server logged you in and when the server answered back with the HtmlPage your internet connection, ISP, or just the Internet broke down for a millisecond and your packet got lost. So here you failed even as a human

这些只是少数情况,但还有更多情况.现在想想看:即使是人类的头脑也不能100%确信登录试验的结果……我们如何期望一款无头的浏览器能做到这一点? :)

Those are just a few scenarios but there are many more. Now think of this: even a human head con not be 100% sure of the result of a log in trial... how can we expect a headless browser to be? :)

这篇关于HTMLUnit:确定是否“登录"那是成功的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆