I'm currently stumped trying to figure this out, and I could use someone elses thought process to help me with this.
I'm trying to check that a certain value from a response in my test case exists in a table, so that I can then store the value of one of the other columns in that response to use it to issue a command to my device. The problem, however, is that this value isn't always going to exist.
An example response looks something like this...
col1 col2 col3 col4
switch1 g1 2345 A
switch2 g2 123 A B
switch3 g3 245 B
Example situation, is that I want to find the first row with a 'B' in col4, and there is a 'B' in any of the columns, I want to then store the value of col1 into a variable. However, if there isn't a 'B' in any of the rows in col4, I just want to skip all the remaning steps because the test isn't valid.
After the help you guys gave me the last time I had analysis trouble, I thought for sure using an XPATH query would make this simpler. Using fn:contains seems like it would be the right idea, but if I do something like fn:contains (//row/col4, 'B'), it isn't comparing a string to a string if there are multiple rows that have value in col4, is it? I'm rather stumped on how I can do this now, so can anyone point me in the right direction?
Answer by KumarS · May 12, 2009 at 11:40 PM
One solution can be to simply count the number of matching elements.
count(//col4[.='B'])
will return the count of rows whose value is B. Now you can use an if statement to skip rest of the test. (You will store the response of the step in a variable and then use query command in an if statement to control the flow).
PaulD wrote:
I think he needs not "equals" but actually "contains" -- which makes it slightly more complicated. But in combination with the contains xpath function, you should be able to do the same thing.
Correct, but as I stated in the original post, when I try to use the contains function it always returns false for whatever reason.
fn:contains(//col4, 'B') is returning false even though //col4 returns back the value of 2 rows of col4 where one of the values is just 'A' and the other value is 'A B'
I'm attaching a sample test case showing how to do this.
To keep it simple, the first "eval" step simulates a step that produces a response containing your table. That step is configured to store its response in a variable called table1. (Note that this example just uses automatic response mapping, but you probably want to create a formal response map for this -- and you can then add some custom queries to that map to make it easier in future test cases to do something similar.)
The "if" step does a query against the stored response. It uses an XPATH query that finds all of the "row" nodes that contains a "col4" node containing "B" -- //row[contains(col4,'B')] -- and then counts these rows by wrapping this in a "count" function.
You may prefer to just iterate over the rows of interest, and so the example also shows a "foreach" step that uses a query to extract the value of the key colum for each of the rows containing "B' in col4.
Hopefully this will help you. Let me know if you need further clarification.
I was actually trying to do something quite similar to this before I saw your response, except I ran into a block when I realized I wasn't able to store a value and substitute the variable into a query command.I was about to post my testcase to see if any of you could help me, but now I don't need to!
This works pefectly. Changing up the testcase I was playing around with and adding some of your steps and logic, I was able to correctly extract the necessary information from the response. This is definitely the most involved analysis I've had to do for any of the test cases I've done.
That being said, I had read up quite a bit about XPATH since the last analysis issue I had help from you all on, but I still didn't know you could add functions like "//row[contains(col4,'B')]" within the query. That's extremely useful, but before seeing this, I never saw an example of a function used like this. I guess I have some more reading to do :)
Thanks Paul!
KumarS wrote:
One solution can be to simply count the number of matching elements.
count(//col4[.='B'])
will return the count of rows whose value is B. Now you can use an if statement to skip rest of the test. (You will store the response of the step in a variable and then use query command in an if statement to control the flow).
If the column's only value was ever only going to be a single letter, then this would work, however, the column can have a 'B' in it as well as an A or a C or a D. It can have any number of letters, as well as B.
So if I had a column with the values "A B C", if I used the query that you stated, would it not return a count of 0 because that row contains 'B', but doesn't equal 'B'?
Cannot launch flash interface through web session 1 Answer
VNC to EXFO Interwatch R14 on Solaris 1 Answer
Is IE 8 supported for iTest 4.0? 1 Answer
Screen Capture for entire web page. 1 Answer
Web capture issue -Captured japanese page using EUC-JP is broken in 3.4GA (it worked in 3.4 beta 1) 1 Answer
Copyright 2008- Spirent Communications, all rights reserved. Terms and Conditions.