How to use variable for xml attribute value in vb 2015? - visual-studio

I wonted to know if i can use a variable in selecting node statement ex:
Pgs = xmldoc.SelectNodes("/* [#Id = 160578]")
I need to replace this 160578 number with a variable?

A naive (and prone to injection) is string manipulation e.g. xmlDoc.SelectNodes(String.Format("/*[#Id = {0}]", yourVariable)). The right approach would be to use the second argument to SelectNodes and implement variable resolution in an implementation of XsltContext https://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltcontext(v=vs.110).aspx. The project https://mvpxml.codeplex.com/releases/view/4894 has a class DynamicContext doing that, although that project is done in C# you could of course compile it with VS and integrate the API into your VB code:
Dim idVar As Double = 160578
Dim dynContext As New DynamicContext
dynContext.AddVariable("id", idVar)
Pgs = xmldoc.SelectNodes("/*[#Id = $id]", dynContext)

Related

Using one variable for multiple items data in descriptive programming

I know that with Descriptive programming you can do something like this:
Browser("StackOverflow").Page("StackOverflow").Link("text:=Go To Next Page ", "html tag:=A").Click
But is it possible to create some kind of string so I can assign more than one data value and pass it as single variable? I've tried many combinations using escape characters and I always get error.
For example in the case above, let's say I have more properties in the Page object, so I'd normally have to do something like this:
Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")...etc...
But I'd like to pass "name:=StackOverflow", "html id:=PageID" as a single variable, so when writing many objects I'd only have to write:
Browser(BrowserString).Page(PageString).WebEdit("name:=asdfgh")
And the first part would remain static, so if the parents' data needs to be modified I'd only have to modify two variables and not all the objects created in all libraries.
Is it possible?
If I was not clear enough please let me know.
Thank you in advance!
I think what you're looking for is UFT's Description object
This allows you finer grained control on the description since in descriptive programming all values are regular expressions but with Description you can turn the regular expression functionality off for a specific property.
Set desc = Description.Create()
desc("html tag").Value = "A"
desc("innertext").Value = "More information..."
desc("innertext").RegularExpression = False
Browser("Example Domain").Navigate "www.example.com"
Browser("Example Domain").Page("Example Domain").WebElement(desc).Click
If you want to represent this with plain string then it's a bit more of a problem, you can write a helper function but I'm not sure I would recommend it.
Function Desc(descString)
Set ret = Description.Create()
values = Split(descString, "::")
For Each value In values
keyVal = Split(value, ":=")
ret(keyVal(0)).Value = keyVal(1)
Next
Set Desc = ret
End Function
' Usage
Browser("StackOverflow").Page("StackOverflow").WebElement(Desc("html tag:=H2::innertext:=some text")).Click
Further reading about descriptive programming.
As an alternative to Motti's excellent answer, you could also Set a variable to match your initial descriptive object and then extend it as required:
Set myPage = Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")
after which you can then use
myPage.WebEdit("name:=asdfgh")
throughout the rest of the code, so long as the myPage object stays in scope...

How to fix an import from AD

I'm not a developer, i'm trying to understand why my IT ticketing software is unable to import people from the windows AD. It was working ok a month ago. I have no access to the windows AD but they said "no changes or upgrades" recently made
I have isolated problem is related to the AD "manager" field, meaning, my ticketing software has an option not to try to import the manager field(or whatever it is) and it will work fine
Here is the code that performs the import
Option Explicit On ' Change to Off instead of On to disable the need to declare variables with Dim. (NOT recommended)
Option Strict Off ' Change to Off instead of On to cast variables automatically. (NOT recommended)
Option Compare Binary ' Change to Text instead of Binary to let string "A" equal to "a".
Imports System
Imports System.Data
Imports System.Collections
This is a transformation script class. Please take notice of the following rules:
' - You are not allowed to change the name of the class (must be "TransformationScript")
' - You are not allowed to change the namespace the class is in (the class "TransformationScript" must not be in a namespace).
' - You are not allowed to change the name of the method (must be "Transform")
' - You are not allowed to change the parameters of the "Transform" method
' This method is called by the transformation logic. The dt parameter contains the data table with the data to transform.
' Any additional arguments are passed in the additionalArguments list.
Public Class TransformationScript
Public Sub Transform (ByVal dt As System.Data.DataTable, ByVal additionalArguments As SortedList)
For Each row As DataRow In dt.Rows
Dim strValue As String
If Not String.IsNullorEmpty(row.Item("manager").ToString)
strValue = row.Item("manager").ToString
Dim strArray() As String
Dim strManager As String
Dim strManagerDisplayName As String
split the AD manager object on "OU" and the results to array
strArray = strValue.Split("OU")
get string array position "0" that contains the manager name
strManager = strArray(0)
clean up the Manager display and remove the "CN=" and "\" and the last ","
strManagerDisplayName = strManager.Replace("CN=","")
strManagerDisplayName = strManagerDisplayName.Replace("\","")
strManagerDisplayName = strManagerDisplayName.Remove(strManagerDisplayName.Length-1)
return the Manager Display Name
row.Item("manager") = strManagerDisplayName
End If
Next
End Sub
End Class
I want to understand what the code is trying to do and possible root causes of what could be causing the issue
Thanks in advance

Error "object doesn't support this property or method: dbrowser.GetRoProperty"

I was trying to run below script, but it's giving me an error that says:
object doesn't support this property or method: "dbrowser.GetRoProperty"
SystemUtil.Run "iexplore.exe","http://usps.com/"
Set dbrowser = description.Create
dbrowser ("micclass").value = "Browser"
dbrowser("openurl").value = "https://www.usps.com"
dbrowser("title").value = "USPS - The United States Postal Service (U.S. Postal Service)"
print(dbrowser.getroproperty("title"))
Your dbrowser object is of type Description not Browser you need to create a Browser object based on this description. Replace the last line with:
Print Browser(dbrowser).GetROProperty("title")
Note, there are two changes here
Using Browser(dbrowser)
Removing the parens from the print sub.
Edit: also note that descriptions are regular expressions by default so the parens in the title may cause problems, you should mark it as not regex.
dbrowser("title").RegularExpression = False
Description.Create is used to create a 0-based Properties collection object. The variable dbrowser is preceded by the Set statement. Usage of Set statement binds an object as a reference to another object. Therefore, dbrowser becomes an object reference to the description object represented by Description.Create
A description object does not have a stand-alone use, but coupled with the ChildObjects method, it becomes an extremely powerful approach in dealing with AUT’s objects .For More Info, check link
So the code should be like
SystemUtil.Run "iexplore.exe","http://usps.com/"
wait(10)
Set dbrowser = description.Create
dbrowser ("micclass").value = "Browser"
dbrowser("openurl").value = "https://www.usps.com"
dbrowser("title").value = "USPS.*" ''Using Regular Expression here
Set colObject = Desktop.ChildObjects( dbrowser )
Print (colObject(0).GetROProperty("title"))

Trouble with using multiple actions within a if statement

everytime i code using multiple actions, it doesnt produce and output and the code doesnt work at all until i give it one action alone instead of three, i dont know whats wrong with it, i tried to put this code in if statements and/or just left it alone as an action when pressing the save button
heres the code \ btw im using visual studio 2012
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim Path1 As String = "Backups\"
Dim Path2 As String = rtbTitle.Text + "\"
Dim FullPath As String = Path1 + Path2
Dim textfileTitle As String = "title.txt"
Dim textfileDescription As String = "description.txt"
Dim textfileTag As String = "tag.txt"
Dim textfileChannel As String = "channel.txt"
If Directory.Exists(FullPath) Then
rtbTitle.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
rtbDescription.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
rtbTag.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
End If
End Sub
Add Option Explicit and Option Strict to the top of your code file (or set them as defaults) and you'll soon see where you're getting into trouble.
One problem that will show up is that you're using '+' to concatenate strings, you really should use '&'. You may unwittingly have typos in your control names or be inputting bad data for the paths. All these sorts of errors are covered up until run time unless you set those options.
After that add a Try...Catch block around your code, run it and view the error.

Run time error 1004, Application defined or object defined error in excel vbs

I have written vbscript code to add chart in page 1 of excel for which the source is from other sheet of same excel which name is "CL.1.1" but i am getting the above error can any one help what was wrong in my below code.
Sub DispvsTime(Shname)
Sheets("Sheet1").Select
noofsheets = ActiveSheet.ChartObjects.Count
If noofsheets > 0 Then
ActiveSheet.ChartObjects.Select
ActiveSheet.ChartObjects.Delete
End If
Sheets("Sheet1").Pictures.Visible = False
ActiveSheet.Shapes.AddChart(1000, 420, 50, 500).Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Sheets(Shname).Range("G2:H2001")
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Displacement VS Time"
End Sub
here shname is name of the sheet where data is picked.
Can any one help me to find out the bug inside the code to get this error ?
If the sheet name is Shname then place it around quotation marks, if it is a variable name, which holds the sheet name, then ignore this post :P
Sheets("Shname")
If your code is really VBScript as you say, and not VBA as it looks like, then there are several issues:
Unlike VBA you cannot use objects like ActiveSheet or ActiveChart directly in VBScript. You need a reference to these objects, usually the application object:
Set xl = CreateObject("Excel.Application")
xl.ActiveSheet.Name = "foo"
VBScript doesn't know about Excel/Office constants like xlXYScatterSmoothNoMarkers or msoElementChartTitleAboveChart, so you have to either use literal values or define those constants yourself:
Const xlXYScatterSmoothNoMarkers = 73
You can't use named arguments like Source:=... in VBScript:
xl.ActiveChart.SetSourceData xl.Sheets(Shname).Range("G2:H2001")
See here for a more detailed explanation of the differences between VBA and VBScript.

Resources