Sign in to follow this  
Followers 0
jodayautoit

Checking a Webpage status

43 posts in this topic

Hi all!

I have been working on my test script and reviewing some examples that some of you have posted to help us newbies out. I have found two pieces of code that appear to be something I could use to add to my existing script for webpages. I have looked it the Autoit help files but have not found how to correctly adapt these two pieces of code together. I was wondering if some one could point me in the right direction. All I am trying to do is to have a webpage load at a user specified IP address and the check that the webpage is on-line( or loaded correctly) or off-line( did not load correctly) Below is the script that will not work as written. Each work fine built seperately.

This is probably simple and I just can not see it.

Thanks

jodayautoit

#include <IE.au3>

#include <Inet.au3>

$sourceip=InputBox("") ;(you can use 209.85.147.147 as a test)

;Code to make sure valid address or script exits.

If $sourceip = 0 Then

Exit

EndIf

; Create a browser window , maximize it and navigate to selected IP address

$oIE = _IECreate($sourceip)

$HWND = _IEPropertyGet($oIE, "hwnd")

WinSetState($HWND, "", @SW_MAXIMIZE)

_IEAction($oIE, "visible")

_IELoadWait($oIE)

;Appears to be a good way to check if website is online or off. Need to adapt this code to above via IP address

;Above works as a script and below works as a script.

$URL = ("www.google.com")

$oIE = _IECreate($URL)

If NOT (_INetGetSource($URL) = " ") Then

MsgBox(0,"","Your website is Online")

Else

MsgBox(0,"","Your website is Offline")

EndIf

;

Share this post


Link to post
Share on other sites



You can probably use the Ping function, it returns @error if it can't ping the host.

Share this post


Link to post
Share on other sites

I use code similar to this. Actually, I just use it to verify a network connection.

Ping("google.com", 500)
If @error = 0 Then Msgbox(0 ,"", "Yay!")

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Thanks PowerCat and Somdcomputerguy!

I saw the Ping used in other examples and I did another test script with what you indicated and it does work. That gets me on/off line status .Is there a way to add in if the correct information on that page is correct. For my test script I have several weblinks on one webpage. Once the on-line status is correct and my script processes the first weblink which has different information on that child page could I use the following:

This was from one of the members I found in an earlier search.

Makes sure page is loaded

$oIE = _IECreate("www.google.com")

$pageText = _IEBodyReadText($oIE)

$check = StringInStr($pageText, "advanced")

I understand that the above code looks for the word "advanced" if the google page loaded correctly. Since I have several weblinks within a page, could I adapt this code into my script to check the several child pages using a unique word on each page? If so, could some one provide a simple example.

Basically ,I have used the Recorder function to record the mouse movements and mouse clicks for each web-link on the main page. I just want to add if possible a validation that each individual webpage loaded as expected and if it does not the script will display to the user a failed message and halt the script. I ran my test script and it seems to continue mouse clicks /movements even if the webpage is not correct since the main page is still working.

Example the main page is called "Home Page" and on the home page it has down the side several links that once clicked will open up a smaller window within the home page. I would like to be able to check the link worked as well. I would appreciate any suggestions.

I apologize for the long question.

Thanks

joday autoit

Share this post


Link to post
Share on other sites

This code will check one page for a specific text. It can easily be modified to add arrays and then handle multiple text lines and multiple URLs (pages), but the kids are home from school now and my AutoIt time is paused..

#include <INet.au3>
ConsoleWrite("Checking.. ")
Local $Text = "", $URL = "http://", $Source = _INetGetSource($URL)
If StringInStr($Source, $Text, 2) <> 0 Then ConsoleWrite("Page OK" & @LF)

Of course $Text and $URL need to be modified to fit usage.. Good luck with your project!


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Thanks Somdcomputerguy!

If I followed you correctly. I took your example and did the following script. BTW how does one get the autoit pop up window like your post to show up(where you put your code). I am still new to the forum about how to use different options. In any case you have provided me the solution to this question. The below does work and prints "Page Ok" if the google page is loaded and $Text is correct. If not it prints the "page not ok" so I can adapt this to my test script. Did I get this right?

Thanks to all for the help.

jodayautoit

#include <IE.au3>

#include <INet.au3>

$URL = "http://www.google.com"

$oIE = _IECreate($URL)

ConsoleWrite("Checking.. ")

Local $Text = "Advertising", $URL = "http://www.google.com", $Source = _INetGetSource($URL)

If StringInStr($Source, $Text, 2) <> 0 Then

ConsoleWrite("Page OK" & @LF)

Else

ConsoleWrite("Page not OK" & @LF)

EndIf

Share this post


Link to post
Share on other sites

Hi all!

I added the checking that somdcomputerguy suggested. I guess I am missing one thing for my test script to work as I expect. Could someone point me to what I am doing wrong.

The below code will work if I set $URL to the actual IP address of the URL. In the top of my script I have the user enter the IP address as $sourceip. Basically this is a webpage that has a username and password also and that code works. I have looked in the help file and I thought I should just be able to change the Local $Text line to point to $sourceip to get the information for the URL but that does not work or I got the syntax wrong. How can I adapt this so the user IP address input is used as part of the checking statement?

Other than this minor issue my script is working. I appreciate the assistance from the members. The code below works fine once $URL is changed to a valid IP address.

Thanks

jodayautoit

$sourceip = InputBox(" Test Script", "Enter the IP address ") ;(some IP address )

;Code to make sure valid address or script exits.

If $sourceip = 0 Then

Exit

EndIf

$oIE = _IECreate($sourceip)

$HWND = _IEPropertyGet($oIE, "hwnd")

$URL = "http:// X.X.X.X"

ConsoleWrite("Checking.. ")

Local $Text = "Web", $URL = "http://X.X.X.X", $Source = _INetGetSource($URL)

If StringInStr($Source, $Text, 2) <> 0 Then

ConsoleWrite("Page OK" & @LF)

Else

ConsoleWrite("Page not OK" & @LF)

EndIf

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Try $Source = _INetGetSource($sourceip) instead of $Source = _INetGetSource($URL). This doesn't solver your current issue though, which I think can be resolved with the Ping test.<br>

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Thanks somdcomputerguy!

I did put the Ping test as you suggested in the code. That works as well. I put it after the password is entered to make sure the address is on-line. i just did not show it in my previous post. I like the console checking part you suggested so I wanted to add that as well. I just could not get the $URL statement to work without actually hard coding an IP address. I was hoping to be able to use the user input of an IP address. I changed the $Source= _INetGetsource ($sourceip) but it did not work as you had suspected. I need to read some more of the help files on these functions.

Thanks for your assistance.

jodayautoit.

$sourcepassword = InputBox("Test Script", "Enter the password ")

Sleep(2000)

Send($sourcepassword)

Send("{ENTER}")

Sleep(2000)

Ping($sourceip, 500)

If @error = 0 Then Msgbox(0 ,"", "Page found!")

ConsoleWrite("Checking.. ")

Share this post


Link to post
Share on other sites

#10 ·  Posted

Hi all!

I found a reasonable solution to my earlier issue about getting a way to check the webpage. The below code is a working example of the basics of what I wanted to test. I thought I would share. I had to use the IEDocReadHTML function. I am still going to try to figure out how to adapt the $URL and INetGet source code at some point but this works for what I need at the moment.

Thanks for the assistance to all. I have added more code for specific mouse movements and mouse clicks. This is just a stripped down version.

jodayautoit

;Sample code to enter IP Address, username, password and then check the page is correct or not

; Tested and works as written 5/3/2011 by jodayautoit.

#include <IE.au3>

;Code for IP address input

$sourceip = InputBox(" Test Script", "Enter the IP address ") ;(some IP address )

;Code to have Internet Explore launch to valid IP address

$oIE = _IECreate($sourceip)

$HWND = _IEPropertyGet($oIE, "hwnd")

WinSetState($HWND, "", @SW_MAXIMIZE)

_IEAction($oIE, "visible")

_IELoadWait($oIE)

;Code for Username input

$sourceuser = InputBox("Test Scirpt", "Enter the username ")

Sleep(2000)

Send($sourceuser)

Sleep(2000)

Send("{TAB}")

;Code for Password input

$sourcepassword = InputBox("Test Scirpt", "Enter the password ")

Sleep(2000)

Send($sourcepassword)

Send("{ENTER}")

Sleep(2000)

;Code to verify IP address is active

Ping($sourceip, 500)

If @error = 0 Then MsgBox(0, "", "Page found!")

;Code to check if correct page is loaded.

ConsoleWrite("Checking.. ")

$sHTML = _IEDocReadHTML ($oIE)

MsgBox(0, "Document Source", $sHTML); use this to get the correct information for $Text then comment out.

Local $Text = "Login Page"

If StringInStr($sHTML, $Text, 2) <> 0 Then

ConsoleWrite("Page OK" & @LF)

Else

ConsoleWrite("Page not OK" & @LF)

EndIf

; put other test code

Share this post


Link to post
Share on other sites

#11 ·  Posted

Hi all!

My previous script works for the initial webpage I was trying to read. Unforunately I found that the home page does not have what I call "normal source code" that can be viewed by the user if they hit the right click and selected "view source code". This webpage has what seems to be something called "frames or child windows" in it. I can only read the main home page window so my above script if I add the same checking code to look at one of the links will show me the same information in the console for the IEDocReadHTML function. I tried using the _INetGetsource which I orignally tried to use but that does not return any code in my script. If some one could provide me a sample of how _INetGetsource should work I might be able to figure it out.The main page is composed of split windows of what I think are not Active windows until you click on them. Which is why my script returns no code to the console. If I was not clear enough let me know what other information is required. Basically the project I am writing this test script for is similar to a router that has a GUI interface running HTML. All I am trying to do is verify that each webpage has the correct information on it when it is activated. I only need to check 5 webpages for correct data and thought the _INetGetSource would be correct in this case. I guess since I am still a newbie to this scripting tool I may have looked at this completely wrong and what I wrote( will help from you guys) will only work for the first webpage and nothing else.

Any suggestions are greatly appreciated. I think Autoit will work for many things I would like to automate the testing and I like it is easy to run the build and test the scripts. I hope it can provide me a way to test this correctly.

Thanks

jodayautoit

Code below: What needs to be changed to read the actual webpage that is displayed?

;Sample code to enter IP Address, username, password and then check the page is correct or not

; Tested and works as written 5/3/2011 by jodayautoit.

#include <IE.au3>

#include <inet.au3>

#include <array.au3>

;Code for IP address input

$sourceip = InputBox(" Test Script", "Enter the IP address ") ;(some IP address )

;Code to have Internet Explore launch to valid IP address

$oIE = _IECreate($sourceip)

$HWND = _IEPropertyGet($oIE, "hwnd")

WinSetState($HWND, "", @SW_MAXIMIZE)

_IEAction($oIE, "visible")

_IELoadWait($oIE)

;Code for Username input

$sourceuser = InputBox("Test Scirpt", "Enter the username ")

Sleep(2000)

Send($sourceuser)

Sleep(2000)

Send("{TAB}")

;Code for Password input

$sourcepassword = InputBox("Test Scirpt", "Enter the password ")

Sleep(2000)

Send($sourcepassword)

Send("{ENTER}")

Sleep(2000)

;Code to verify IP address is active

Ping($sourceip, 500)

If @error = 0 Then MsgBox(0, "", "Page found!")

;Code to check if correct page is loaded.

ConsoleWrite("Checking.. ")

_IEAction($oIE, "visible")

_IELoadWait($oIE)

$sHTML = _IEDocReadHTML($oIE)

MsgBox(0, "Document Source", $sHTML); use this to get the correct information for $Text then comment out.

Local $Text = "Login Page"

If StringInStr($sHTML, $Text, 2) <> 0 Then

ConsoleWrite("Page OK" & @LF)

_IELoadWait($oIE)

ConsoleWrite("***** _INetGetSource *****" & @CR & _INetGetSource($oIE) & @CR & @CR)

ConsoleWrite("***** _IEBodyReadText *****" & @CR & _IEBodyReadText($oIE) & @CR & @CR)

ConsoleWrite("***** _IEDocReadHTML *****" & @CR & _IEDocReadHTML($oIE) & @CR & @CR)

Else

ConsoleWrite("Page not OK" & @LF)

EndIf

Share this post


Link to post
Share on other sites

#12 ·  Posted

Hi all!

I spoke with some one who wrote the HTML code for what I am trying to test. He said the code is uses Frames and there are 3 Frame being used. A Main menu,Nmenu,and menu. How do I get my script to read the correct Frame to get the correct information on that page once it is loaded. An example of the HTML is below.

Another piece of the equation. Any suggestions or Ideas are greatly appreciated.

Thanks

jodayautoit.

<html>

<head><title>Login Page</title>

<link rel="shortcut icon" href="favicon.ico">

<link rel="icon" href="animated_favicon1.gif" type="image/gif">

</head>

</head>

<frameset framespacing="0" rows="72,*">

<frame name="Banner" scrolling="auto" src="htm_banner.htm

<frameset cols="150,*">

<frameset rows="*,18%">

<frame name="menu" target="main" src="htm_menu.htm

<frame name="nmenu" src="htm_nmenu.htm

</frameset>

<frame name="main" src="htm_Summary.htm

</frameset>

<noframes>

<body topmargin="1" leftmargin="1">

<p>This page uses frames, but your browser doesn't support them. <br>

These pages can be best viewed in Internet Explorer 4.xx and Netscape 4.xx at resolution 1024x768

</p>

</body>

</noframes>

</frameset>

</body>

Share this post


Link to post
Share on other sites

#13 ·  Posted

Hello. I've suddenly become real busy and haven't had much AutoIt time, but about the 'array thing' I mentioned earlier, to check multiple pages; a simple (working) version of that is here. I've left the URL and Text variable information intact, feel free to use it as that to test it. Please don't 'test' it a couple hundred thousand times a minute, or a day! :unsure:

#include <INet.au3>

Local $URL_Array[2]  = ["http://somdcomputerguy.com", "http://somdcomputerguy.com/?p=anotherpage"]
Local $Text_Array[2] = ["barb", "sites i'm doing"]

For $a = 0 To 1
    ConsoleWrite("Checking " & $URL_Array[$a] & " - ")
    If StringInStr(_INetGetSource($URL_Array[$a]), $Text_Array[$a], 2) <> 0 Then ConsoleWrite("Page OK" & @LF)
Next

I've briefly read thru some of your posts, to be short - use the frames' src target that you want as an URL . So, htm_nmenu.htm for the nmenu frame.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#14 ·  Posted

Thanks somdcomputerguy!

I will try what you have suggested. I did try the array code in my original attempt at this. I could not get it to work but I think that was because I had the whole IP address input confusing me to the $URL. I will try it again. I just tested your example once to see if it compiled and printed to the console on my PC. I will change the example to a local page here as to not run it over and over accidently.

Thanks for your help on this. As a newbie to Autoit, I may have picked a more complex test to start with than I should have. Maybe I should have started out smaller. Basically the idea I was getting at, was to automate a long manual test sequence on this piece of equipment I work on which uses a web-based application. I thought I could just automate the mouse click /movements to where I manually click and visually verify the data is correct. It appears Autoit has the ability to accomplish my goal. It is just more complicated on the verification of the webpage data than I had expected. I was not expecting to deal with Framesets and how the HTML code could be read.

Thanks again. you have been of great help. When I get this written and working I will share the base code of what made it work.

jodayautoit

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Good luck with this project. To keep frames from getting confusing, just remember - the pages loaded in a frameset are just pages like a 'main' page, they are just called or loaded differently, they can be handled just like a regular page.. Keep asking away if any more issues arise. Oh yeah, I'm glad that example I posted worked for you, I hope it helps some. I've noticed now that I didn't comment that code at all, I think you've got it figured out but let me know if it's doing something you don't understand.<br>

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#16 ·  Posted

Thanks somdcomputerguy!

I made a smaller script just to check the example using the changes for my webpage. I got the following error on the console when I tried to get the second page. It indicated something about "invalid session ID". I was told by the software person on HTML for this piece of equipment that the other frame pages are accessed thru the main page once you are logged in. I guess I am still confused how to access the other pages. Below is the error. I am logged into the main page correctly. I would appreciate if you could explain what I am not getting correct or if I am trying to test something that is not allowed to be tested. I also tried it with the other page name htm_menu.htm and it also did not work.

Thanks

jodayautoit.

This first part is the main login page that works and I get all the source code. I only pasted the first few lines.

Checking http://x.x.x.x - Page OK

***** _INetGetSource *****

<html>

<head><title>Login Page</title>

<link rel="shortcut icon" href="favicon.ico">

<link rel="icon" href="animated_favicon1.gif" type="image/gif">

</head>

<script LANGUAGE="Javascript">

This is the error that I do not fully understand.

Checking http://x.x.x.x - ***** _INetGetSource *****

<html><head>Invalid Session<title></title></head><body>

<p><pre></pre></p><p>For security reasons. Please login using following link.<a href="htm_login.htm" target="_top"><b>Login</b></a></p></body></html>

Checking http://x.x.x.x/?p=htm_systemSummary.htm - Page OK

***** _INetGetSource *****

<html><head>Invalid Session<title></title></head><body>

<p><pre></pre></p><p>For security reasons. Please login using following link.<a href="htm_login.htm" target="_top"><b>Login</b></a></p></body></html>

This is the smaller script code I used

#include <INet.au3>

#include <IE.au3>

$oIE = _IECreate("http://x.x.x.x")

Sleep(2000)

Send("username")

Sleep(2000)

Send("{TAB}")

;Code for Password input

Sleep(2000)

Send("password")

Send("{ENTER}")

Local $URL_Array[2] = ["x.x.x.x", "http://x.x.x.x/?p=htm_login.htm"]

Local $Text_Array[2] = ["Web", "Login"]

For $a = 0 To 1

ConsoleWrite("Checking " & $URL_Array[$a] & " - ")

If StringInStr(_INetGetSource($URL_Array[$a]), $Text_Array[$a], 2) <> 0 Then ConsoleWrite("Page OK" & @LF)

ConsoleWrite("***** _INetGetSource *****" & @CR & _INetGetSource("http://x.x.x.x/htm_login.htm") & @CR & @CR)

Next

Local $URL_Array[2] = ["http://x.x.x.x", "http://x.x.x.x/?p=htm_systemSummary.htm"]

Local $Text_Array[2] = ["System Summary", "Login"]

For $a = 0 To 1

ConsoleWrite("Checking " & $URL_Array[$a] & " - ")

If StringInStr(_INetGetSource($URL_Array[$a]), $Text_Array[$a], 2) <> 0 Then ConsoleWrite("Page OK" & @LF)

ConsoleWrite("***** _INetGetSource *****" & @CR & _INetGetSource("http://x.x.x.x/htm_systemSummary.htm") & @CR & @CR)

Next

Share this post


Link to post
Share on other sites

#17 ·  Posted

hi jodayautoit,

When posting code, you should enclose it within code or autoit tags. This makes it easier to read, copy-paste and understand.

Example posting some autoit code in autoit tags:

Step 1:

Posted Image

Step 2:

Posted Image

This gives:

Posted Image

Isn't it pretty? :unsure:

Also, check out code tags for posting non-autoit code like html etc.

Posted Image

Hope this helps :>

-smartee

Share this post


Link to post
Share on other sites

#18 ·  Posted

Thanks smartee!

I did post earlier I was not sure how to put the code in the autoit box so it would be easier to read. I will try what you have shown above and post my code. Below if I followed you correctly should be the code in an autoit format. If not I will try again.

Thanks

jodayautoit

#include <IE.au3>
#include <INet.au3>

$oIE = _IECreate("http://x.x.x.x")

Sleep(2000)
Send("username")
Sleep(2000)
Send("{TAB}")

;Code for Password input

Sleep(2000)
Send("password")
Send("{ENTER}")


Local $URL_Array[2]  = ["http://x.x.x.x", "http://x.x.x.x/?p=htm_login.htm"]
Local $Text_Array[2] = ["Web", "Login"]

For $a = 0 To 1
    ConsoleWrite("Checking " & $URL_Array[$a] & " - ")
    If StringInStr(_INetGetSource($URL_Array[$a]), $Text_Array[$a], 2) <> 0 Then ConsoleWrite("Page OK" & @LF)
    ConsoleWrite("***** _INetGetSource *****" & @CR & _INetGetSource("http://x.x.x.x/?p=htm_login.htm") & @CR & @CR)
Next

Local $URL_Array[2]  = ["http://x.x.x.x", "http://x.x.x.x/?p=htm_login.htm/htm_menu.htm?"]
Local $Text_Array[2] = ["Menu", "Start-up"]


For $a = 0 To 1
    ConsoleWrite("Checking " & $URL_Array[$a] & " - ")
    If StringInStr(_INetGetSource($URL_Array[$a]), $Text_Array[$a], 2) <> 0 Then ConsoleWrite("Page OK" & @LF)
    ConsoleWrite("***** _INetGetSource *****" & @CR & _INetGetSource("http://x.x.x.x/?p=htm_login.htm/htm_menu.htm?") & @CR & @CR)
Next

Share this post


Link to post
Share on other sites

#19 ·  Posted

Checking http://x.x.x.x - ***** _INetGetSource *****
<html><head>Invalid Session<title></title></head><body>
<p><pre></pre></p><p>For security reasons. Please login using following link.<a href="htm_login.htm" target="_top"><b>Login</b></a></p></body></html>

Checking http://x.x.x.x/?p=htm_systemSummary.htm - Page OK
***** _INetGetSource *****
<html><head>Invalid Session<title></title></head><body>
<p><pre></pre></p><p>For security reasons. Please login using following link.<a href="htm_login.htm" target="_top"><b>Login</b></a></p></body></html>

This should be the error on the autoit console. This is the part I do not fully understand.

Thanks

jodayautoit.

Share this post


Link to post
Share on other sites

#20 ·  Posted

Hi all!

I figured out what webpage was what just by using IE to see if I could even call the page from the http:// bar. Basically I think and you guys could tell me if this is correct, the format I was entering in the script for the page was wrong.

I was entering "main page/call other page". Apparently in this case it is "call other page/main page"

this is what I should have entered in my script I think since you can type this in IE and the correct page is shown.

I will attempt to edit my script to enter these as there are. If this works then the only concern I have is what do I name the http:.//x.x.x.x in my script since I want to use the users input for the IP Address. Goes back to one of my earlier posts showing the user input is defined as ($sourceip). I presume I just change any http:// reference to ($sourceip)

Thanks

jodayautoit

http://x.x.x.x/htm_systemSummary.htm/htm_login.htm
http://x.x.x.x/htm_banner.htm/htm_login.htm
http://x.x.x.x/htm_menu.htm/htm_login.htm

my stripped version of test script.

$oIE = _IECreate($sourceip)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)

;Code for Username input
$sourceuser = InputBox("Test Script", "Enter the username ")

Sleep(2000)
Send($sourceuser)
Sleep(2000)
Send("{TAB}")

;Code for Password input
$sourcepassword = InputBox("Test Scirpt", "Enter the password ")
Sleep(2000)
Send($sourcepassword)
Send("{ENTER}")

Sleep(2000)

;Code to verify IP address is active
Ping($sourceip, 500)
If @error = 0 Then MsgBox(0, "", "Page found!")

;Code to check if correct page is loaded.

ConsoleWrite("Checking.. ")

;will update with the new http://x.x.x.x page checking. not sure what it will be

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0