Do loop won't exit after conditions are met? - vbscript

I've been trying to make a loop that repeats itself until an does not element exists on the page. However, when the condition is met, the loop does not exit. Any suggestions?
Dim userExt
userExt = 1
Do until Not Browser("Some_browser").Page("Some_page").WebElement("This username is already").exist
Browser("Some_browser").Page("Some_page").WebEdit("WebEdit").Set "registertester" & userExt+1
userExt = userExt+1
Browser("Some_browser").Page("Some_page").WebElement("Save & Continue").Click
Loop
I also tried changing the first line to
Do until Browser("Some_browser").Page("Some_page").WebElement("This username is already").exist=False
I tried adding an if exist then exit loop, which didn't work. It just kept looping. I know for sure the element does not exist on the page.

The element did not exist in the source code but for some reason UFT found it. I found that out by going on the page and click the "Highlight in Application" button in UFT. Although nothing was highlighted, UFT did not generate any error. I went on to check the properties of the element and found a property called visible.
I changed the Do Until statement to:
Do until Browser("Some_browser").Page("Some_page").WebElement("This username is already").GetROProperty("visible")="False"

Related

Is there something I'm doing wrong to pick up this error?

I am new to Ti-basic, and I am trying to code it. I'm trying to make this 'special type of input' program. It is kind of like input, but it will show the word as it is being pressed (and there is no need to put in alpha)
Here is the code so far that I believe is pertaining to the error
:{41,42,43,51,52,53,54,55,61,62,63,64,65,71,72,73,74,75,81,82,83,84,85,91,92,93,94,102,103,103}→∟KEYS
:"ABCDEFGHIJKLMNOPQRSTUVWXYZθ :?"→Str7
:0→K
:""→Str1
:
:Repeat K=105
:getKey→K
:If max(∟KEYS-K)
:prgmFINDIND
:.........
:End
Inside prgmFINDIND, This is the code
:1+sum(not(cumSum(∟KEYS=K)))→I
://I is used later on in the code. It isn't pertaining to the problem.
I have done some testing with pause on this already, and I found the problem was in the if statement. It returns an 'INVALID DIM' error.
Anybody know what's wrong?
In this part (edited a bit)
Repeat K=105
getKey->K
If max(|LKEYS=K
prgmFINDIND
Str1+sub(Str7,I,1->Str1
End
prgmFINDIND is only called if the key that was pressed is in the list, otherwise the index I is not changed (and possibly implicitly zero, or whatever other value that was left there).
Pressing GOTO on the INVALID DIM actually goes to Str1+sub(Str7,I,1->Str1, indicating that a bad index was used to index into Str7.
It could be fixed by using an If/Then block, which can cover more than one statement:
Repeat K=105
getKey->K
If max(|LKEYS=K
Then
prgmFINDIND
Str1+sub(Str7,I,1)->Str1
End
End

VBScript - check if mailItem is still viable

I've ran into a problem with one of my scripts.
The script automates our shared mailbox (outlook). It auto-assigns mails to the correct person.
Everytime the script runs, it loops all the mails in the folder and checks if it has a category. If not, it assigns it to the right user.
Problem is, when a mail with no category gets dragged to another folder while running the script, it throws an error when trying to execute mail.Categories
Line: 222
Error: Could not complete the operation due to error 8004010f.
Is there a way to check if the mailItem is still viable?
I tried using IsEmpty, but the msgbox doesn't trigger.
Set outlook = CreateObject("Outlook.Application")
Set namespace = outlook.GetNameSpace("MAPI")
Set Account = namespace.Folders("accountName")
Set Inbox = argentaAccount.Folders("Inbox")
For Each mail in Inbox.Items
If IsEmpty(mail) Then
MsgBox("test")
End If
'check if item has a category'
If mail.Categories <> "" Then
'has a category'
Else
'Execute mailhandling code'
End If
Next
If anyone has any solution, I would be forever grateful.
With this line as a clue:
If IsEmpty(mail) Then
VBScript may work the same as VBA where For Each works like:
For i = 1 to Inbox.Items.Count
When item 1 is moved (or deleted), item 2 moves into position 1. On the next loop, position 2 with item 3 is processed. Every second item is skipped. In the second half of the loop there is nothing to process.
One way of dealing with this in VBA is to step in reverse order:
For i = Inbox.Items.Count to 1 step -1
Set mail = Inbox.Items(i)
If mail.Categories <> ""

QTP - Object doesn't support this property or method error

I am not sure what's wrong with the below code.
Set obj=description.Create()
obj("micClass").Value="Link"
obj("name").Value="Advertising Programs"
Set totalnobuttons=Browser("title:=.*").Page("title:=.*").ChildObjects(obj)
totalnobuttons.highlight
print totalnobuttons.count
For i=0 to totalnobuttons.count-1
print totalnobuttons(i).GetRoProperty("name")
Next
This gives an error "Object doesn't support this property or method error" during execution. I need to highlight the "Advertising Programs" program link using the above code.
Your line:
totalnobuttons.highlight
is a culprit. You are trying to highlight the whole collection of Link objects. You cannot do that. Instead, remove that line and put that in your For...Loop like this:
For i = 0 to totalnobuttons.count-1
totalnobuttons(i).HighLight
print totalnobuttons(i).GetRoProperty("name")
Next
You are trying to find collection object. Return type should be always array of objects.
Try to use advanced for loop to proceed. Find the code below:
For each button in totalnobuttons
button.HighLight
print button.GetRoProperty("name")
Next

Printing VBScript variables in QTP UFT

How can I check values of my variables at run time when using QTP UFT?
I simply want to create a variable, do logic and fill it with data, set a breakpoint in the line following the variable and then check or output its value somewhere.
I have tried:
print variableName
WScript.Echo variableName
The first produces error: Print function type mismatch
The second produces error: Object required: "WScript"
I'm not sure where the problem lies as I've just started to get into both UFT and VBScript (mostly did C# and javascript and everything is quite different here). Could someone tell me the correct solution and perhaps also explain these errors to me?
If you wanted to see all variables use the debug viewer in QTP.
View -> Debug Viewer
There you can list all the variables you want to watch. You should be able to see them in break points.
Note : Ensure you have Windows script debugger installed to use the debug viewer.
You can also add the variable to watch .. Insert a breakpoint>> start the script then right click on the variable under test and add it to watch(Add to Watch) . After Adding you will see a watch window and the value of the variable will be displayed.
I obsess over my variables... so much so that I sprinkly my code with print statements... Actually, I abstract print into my own UDF (so that I can optionally add logging to a file if needed)...
Here's my function: (it's actually a sub because it doesn't return anything)
Sub say(textToSay)
If talkative Then 'note that if I set global var "talkative" to false, then the whole log is disabled
print textToSay
End If
End Sub
Then, ALL my code typically looks like this...
say "click submit button"
Broswer("WebSite").Page("Search").WebButton("Submit").click
say "check for result"
if not Browser("WebSite").Page("Results").Exist then
say "results page didn't appear after exist timeout"
failtest
else
say "successfully found results page"
end if
Honestly, I'm shocked that your "print variableName" statement gave an error, print is actually available in the VBScript api, so it should have worked.
All of my output goes to the Output pane in UFT. I would give my right-arm to find a way to programmatically clear that output pane between runs, but noone seems to know how to do it.
The benefit of all this logging is that I can watch my code run and see EVERY branch taken by the code, and I can add statements to print my variables.
Here's an example that shows how I would answer your question:
result = browser("WebSite").Page("Results").WebElement("Result").GetROProperty("innertext")
say "result:" & result
if result = "Approved" then
Reporter.ReportEvent micPass, "Check for approved", "Approved!"
else
Reporter.ReportEvent micFail, "Check for approved", "NOT Approved!"
End If
Notice the say statement in there - I'll be able to see that immediately during code execution without waiting until the results are shown at the end.

"Compile Error: Only Comments may appear after End Sub, End Function.." Access 2010 VBA

I am creating a form and I have created my first function in the VBA window. The function creates Buttons. After I type anything after the 'End function' I get an error- "Only comments appear after .." What's wrong? Am I putting the function in the wrong area?
Here is my code:
Option Compare Database
Option Explicit
Function MainMenuOptions()
Dim frm As String
frm = "Main"
Dim SCFormB As Control
Dim SCSearchB As Control
Dim SCReportB As Control
Set SCFormB = CreateControl(frm, acOptionButton, , , , 0.5, 0.6, 1.5, 0.55)
Set SCSearchB = CreateControl(frm, acOptionButton, , , , 0.5, 1.5, 1.5, 0.55)
Set SCReportB = CreateControl(frm, acOptionButton, , , , 0.5, 2.4, 1.5, 0.55)
End Function
I have recently had this problem. I'm creating a new Shares/Derivatives trade and management application, and I'm making awesome progress on creating a form, when at one point, I starting getting this error for ALL of the _Click() handlers for the controls on the form. But, it was not happening for an _Enter() handler for one of the fields on the form. There was no extraneous text after any of the 'End' statements - at all! I discovered that, somehow, the incorrect LINE ENDINGS had made their way onto the ends of the 'End' statements.
The Fix: go to the end of each of your 'End' statements, hit DELETE until the next Sub statement comes up to the cursor (i.e., you now have the start of the next procedure on the same line as the previous proc's End statement), then hit ENTER to put the next Sub statement back on it's own line. This will ensure that you have the correct CR/LF (or whatever is required of that editor/compiler) at the ends of each of your event handlers.
Apparently, you have extra code in your module which is outside of a function or sub. In the VBA editor select debug -> compile and it should highlight the offending code which you can remove or revise.
FYI: CreateControl uses twips(one 1440th of an inch) for left, top, width, and height. You may want to multiply the numbers you are using by about 1440.
If you check the Microsoft Office Dev Center for this error message you get the following explanation:
You placed executable code outside a procedure. Any nondeclarative
lines outside a procedure must begin with a comment delimiter (').
Declarative statements must appear before the first procedure
declaration. Comments are ignored when the code executes.
This means that the type of code you can write outside a Sub or Function is limited.
I had this error, and the solution was simple, however VBA's debugger threw me off the scent for a while. I had extra code after "End Sub" that needed deleting (copy and paste error), however rather than highlighting the extra code after the "End Sub" statement, the debugger highlighted the beginning of the Sub procedure, causing me to think that the error was between the opening statement of the highlighted Sub procedure and the "End Sub" ABOVE it, when in fact the error was after the "End Sub" of the highlighted sub procedure, so I needed to look well BELOW the highlighted text. So, in effect, VBA highlighted the beginning of a sub-procedure that was entirely fine, rather than the issue immediately after it.
It's like someone leaving a bag of trash immediately outside your front door so you walk into the trash as soon as you walk through the door, and then you say "Huh, I need to move the trash" while pointing at the door.
I had this issue as well, but I didn't have any code outside of my subs. I was getting the error after Access would generate a piece of code (I was trying to add code for the NoData state of my report). When I put a dummy comment below the End Sub that was having the issue it fixed the issue, at least for the last three compiles I have done.

Resources