0

I am trying to add a variable to an xpath but to no avail

This works below for java assertTrue("Failed", verifyElementPresent("//*[@class='StdLJText' and contains(.,'2 Employees selected')]"));

but when I add a variable , like below it does not

String CountEmp1="2"; assertTrue("Failed", verifyElementPresent("//*[@class='StdLJText' and contains(.,CountEmp1+'Employees selected')]"));

2
  • I think you just need to add a space, otherwise the expected value will be 2Employees selected Commented Nov 19, 2012 at 10:48
  • If you could give an example of the markup and possible values by editing the question, that would really help. What I meant by my earlier comment was that you have a successful match with 2 Employees selected whereas you want to parameterise the number part so you need to add the space between the number and the Employees selected part like this CountEmp1+' Employees selected' (note the space before the E). You are using contains which will try to match the text exactly. Commented Nov 19, 2012 at 16:45

1 Answer 1

1

You need to put CountEmp1 as a string addition in the main verifyElePresent func i.e verifyElementPresent("//*[@class='StdLJText' and contains(.,"+CountEmp1+"Employees selected')]"));

Note the replacement of "+CountEmp1+" at contains(.,CountEmp1+'

With your xpath the verifyElementPresent method basically looks for //*[@class='StdLJText' and contains(.,CountEmp1+'Employees selected')] where countEmp1 is taken as string value and not something that needs to be concatenadated.

Sign up to request clarification or add additional context in comments.

5 Comments

this found that it was a variable so thanks but still did not like it particular when concatenated with the string. I notice that the 'selected has no opening '. Could this be the problem
cannot anyone help a little further with this please
It might be the space that andyb is mentioning so it should be contains(.,"+CountEmp1+" Employees selected')]"..I didn't quite get the part of 'selected has no opening'
Hi, I am trying to subistute a string value with a variable namely the value 2
Here is the example assertTrue("Failed", verifyElementPresent("//*[@class='StdLJText' and contains(.,'2 Employees selected')]")); so I need to add a string variable to 2 for example String CountEmp=2;assertTrue("Failed", verifyElementPresent("//*[@class='StdLJText' and contains(.,"+CountEmp1+"Employees selected')]")); This worked below but not really happy with it assertTrue("Failed", verifyElementPresent("//*[@class='StdLJText' and contains(.,CountEmp)]")); assertTrue("Failed", verifyElementPresent("//*[@class='StdLJText' and contains(.,'Employees selected')]"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.