Reputation: 1939
I currently have a few scenarios where its unavoidable to use XPath to locate elements. One of not so neat things this brings along is that my specflow Gherkin code is becoming very messy.
For example, now I have:
And User inserts 'testvan' into field with Xpath '//* [@id="content"]/div/div/div[1]/table/tbody/tr[3]/td[3]/div[1]/table/tbody/tr[5]/td[2]/input'
I am hating this to be honest. Is there any way I can create a dataset in my SpecFlow and put all my XPaths in it, give all my XPaths a certain name and then use them in my scenarios?
Hope you can help!
Upvotes: 0
Views: 999
Reputation: 4343
It sounds like your Gherkin describes how things should be done rather than what should be done.
I think you would like to ask yourself what task the user is performing and describe that. This gives you a chance to hide stuff like ugly xpath in a support class, a page object or similar, to your steps.
This will allow you to work on a higher abstraction level and will make your Gherkin less fragile.
Upvotes: 3
Reputation: 5835
You can solve this in your binding class.
Create there a Dictionary and prefill it. Use the key of the dictionary in your features and make a lookup into the dictionary.
Dictionary<string,string> _xpaths = new Dictionary<string,string>() {{ "XPaht1", "//* [@id=""content""]/div/div/div[1]/table/tbody/tr[3]/td[3]/div[1]/table/tbody/tr[5]/td[2]/input"}}
[Given("And User inserts '(.*)' into field with Xpath '(*.')")]
public void GivenXPath(string user, string xpathKey)
{
var xpath = _xpaths[xpathKey];
...
}
Upvotes: 1