用于自动化网站登录测试的Vbscript [英] Vbscript for automated website login testing
问题描述
我正在尝试运行一个vbscript,它会执行以下操作:
I am trying to run a vbscript, that would do following:
- 启动IE并加载网站。
- 登录该网站。
- 验证登录是否成功。
现在我有了处理第1部分和第2部分的脚本。第3部分,即验证我遇到的登录成功。我该怎么做?
Now I have got the script that deals with part 1 and 2. Its part 3, i.e. verify login success that I am stuck with. How do I do this?
以下是我在某个论坛上发布的代码:
Here is the code I got off some forum:
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "https://my.website.com"
Do While (IE.Busy)
WScript.Sleep 10
Loop
Set Helem = IE.document.getElementByID("formUsername")
Helem.Value = "username" ' change this to yours
Set Helem = IE.document.getElementByID("formPassword")
Helem.Value = "password" ' change this to yours
Set Helem = IE.document.Forms(0)
Helem.Submit
推荐答案
在此高级别验证登录成功的最佳方法可能是在HTML中搜索仅在登录时出现的元素。如果存在则假设您已登录,如果它不尝试导航并登录再次。
The best way to verify login success at this high a level is probably to search for an element in the HTML that only appears when logged in. If that exists assume you're logged in, if it doesn't try to navigate and log in again.
这是一个非常简单的例子,它使用Len()来检查包含文本的元素是否存在。如果你愿意,你可以更复杂,并做一些事情,比如验证你所看到的信息是否与你登录时所看到的相符。
Here is a very simple example that uses Len() to check if an element containing text exists. You can be more sophisticated if you want and do things like verify that the information you're seeing matches what you would see if you were logged in.
你可以使用上面用来抓取元素然后比较任何成员的相同函数。
You can use the same functions you used above to grab elements and then compare any of their members.
Dim IE
Dim Helem
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "http://www.example.com"
Set Helem = IE.document.getElementByID("formUsername")
Helem.Value = "username" ' change this to yours
Set Helem = IE.document.getElementByID("formPassword")
Helem.Value = "password" ' change this to yours
Set Helem = IE.document.Forms(0)
Helem.Submit
Do While (IE.Busy)
WScript.Sleep 10
Loop
Dim someElement
Set someElement = IE.document.getElementByID("someElement")
If Len(someElement.innerText) > 0 Then
MsgBox "logged in"
End If
这篇关于用于自动化网站登录测试的Vbscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!