Use of dynamic xpath with local variable
Hello all,
I find this interesting. Maybe if someone have already faced this issue and have idea please share the experience.
There is same tests but 2 different method followed.(Both being Keyword test)
Test required: Page has multiple product in page. And need to verify that product name match. (Actual value = expected value).
method 1:
-data supplied through csv
-variable made at project level
-Object for checkpoint is xpath made dynamic with syntax: //div[contains(.,'%CurrentProduct%')]. CurrentProduct is project level variable.
-test gets pass in environment where it was prepared.
-test also gets pass in other environment. (as wildcard of url is maintained)
method 2:
-data supplied through csv
-variable made at keyword test level
-Object for checkpoint is xpath made dynamic with syntax: //div[contains(.,@CurrentProduct)]. CurrentProduct is local level variable.
-test gets pass in environment where it was prepared.
-test gets fail in other environment.
Fail reason: Actual value "" , Expected value: Black ball.
TC unable to get the text from div so Actual value is empty.
While observing Details of teststeps that successfully run, both have same structure.
i.e. Property, Condition, Actual value and Expected value has same data.
While doing log of xpath namemapping object during the execution time.
In method 1 it has FindElement("xpath=//div[contains(.,'Black ball')]") . In each loop contains value is changing with csv data.
and in method 2 it has FindElement("xpath=//div[contains(.,@CurrentProduct)]"). In each loop it prints same thing. However, I think internally it is refreshing value. As it works in recorded environment. eg. if I supply black ball, red ball, white ball in csv and run test. Log shows checkpoint pass for each product.
Questions:
1. Is it true that while doing data-driven loop to test, variable should be at Project level inorder to make dynamic xpath?
2. If yes, how is it running successfully with local variable in recorded environment ? It should fail here too in that case.
3. What is the difference between using %CurrentProduct% and @CurrentProduct ?
Note: I tried using %CurrentProduct% in method 2. But TC says Node '{nameMapping object name}' has reference to invalid project variable 'CurrentProduct'
Alternatively, I tried using @ CurrentProduct in method 1. It still passes without any issue.
From my understanding I see that %% can be used to access project level variable and @ for local variable.
Namemapping suggestion given by TC in %%: Selects a <div> elements that has a text containing '%CurrentProduct%'
Namemapping suggestion given by TC in @ : Selects a <div> elements that has a text containing CurrentProduct attribute