Using XPath and Cucumber

2,403 views
Skip to first unread message

cnaimoli

unread,
Mar 13, 2012, 4:09:53 PM3/13/12
to Cukes
I am trying to put a bunch of XPath statements in a list to run.

I was able to successfully verify tests this way but it has to log in
every time:

Feature: Validating Layout of POL Practitioner Details Page

Background:
Given I am on the "Login" page
And I am logged in as "ad...@test.com" using password "Test123!"
And I fill in "lastName" with "Smith"
And I select "CALIFORNIA" from "states"
And I fill in "postalCode" with "92646"
And I click on "Search" button

Scenario Outline: Login
Then I should see xpath "<xpath>"

Examples: Validate Images & Layout
| xpath
|
| //img[@class='sss_logo'] |
| //img[@class='pol_name'] |
| //div[contains(text(), 'Practitioner Details')]
|
| //div[@class='detail-header-box']/h2[contains(text(),
'Details')] |
| //div[@id='detail-container']/div[@class='detail-box']/
div[@class='indivBasic'] |


-------------------------------------------------------------------------------------------------------------------------


I have tried it this way without luck:

Scenario:
Given I am on the "Login" page
And I am logged in as "ad...@test.com" using password "Test123!"
And I fill in "lastName" with "Smith"
And I select "CALIFORNIA" from "states"
And I fill in "postalCode" with "92646"
And I click on "Search" button
Then I should see xpath "<xpath>":
| xpath
|
| //img[@class='sss_logo'] |
| //img[@class='pol_name'] |
| //div[contains(text(), 'Practitioner Details')]
|
| //div[@class='detail-header-box']/h2[contains(text(),
'Details')] |
| //div[@id='detail-container']/div[@class='detail-box']/
div[@class='indivBasic'] |

Bill Wright

unread,
Mar 13, 2012, 4:35:48 PM3/13/12
to cu...@googlegroups.com
I wouldn't write this test as a Scenario Outline. It is just one test that checks for a variety of 
things on the page. I'd use a Data Table instead, so that the scenario only gets run once.

But I wouldn't even do that. I'd rewrite it like this:

Background:
Given I am on the "Login" page
And I am logged in as "ad...@test.com" using password "Test123!"
And I fill in "lastName" with "Smith"
And I select "CALIFORNIA" from "states"
And I fill in "postalCode" with "92646"
And I click on "Search" button

Scenario Outline: Login
Then I should see the home page

Then I'd write a step definition that actually verifies all that you want to see on the home page.
The low-level details of the text and class type of UI widgets should probably not be exposed in
the cucumber feature file.

Bill

Bill Wright, Senior QA/Software Engineer

5395 Pearl Parkway, Suite 100  |  Boulder, Colorado 80301
P 720.236.2035  |  F 303.926.1398
www.tendrilinc.com  |  blog





--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.


 
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender.
Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company.
Finally, the recipient should check this email and any attachments for the presence of viruses.
The company accepts no liability for any damage caused by any virus transmitted by this email.

Oscar Rieken

unread,
Mar 13, 2012, 4:48:38 PM3/13/12
to cu...@googlegroups.com
I am crazy so i would write it something like this
Given I am logged in as Admin Smith
When I perform whatever search is called
Then I should see images in the search results

then all that crazy xpath can stay in the step_definitions 
you could create a table for all the stuff you fill in on the form but im lazy so I would make a method to fill out the form and accept a hash
then I would have a hash in the method and do a reverse update on it and only change the things that needed to (ill assume that what is in the steps currently is "default"
you could assume the same for the login
for the Then you can just check to see if those elements are the ones you expect them to be or even if they should exist
tendril_email_logo_hiring.png

Bill Ross

unread,
Mar 13, 2012, 5:14:22 PM3/13/12
to cu...@googlegroups.com
cnaimoli <cnaim...@gmail.com> wrote:

This way should be fixable, and is more efficient. Rather than trying
to include an Examples table, you should use a regular table:

Then I see xpaths as follows:
| xpath |
| xpath1 |
| xpath2 |
| ... |

And figure out how to parse this kind of table.

That said, it is questionable to put something as low-level as an
xpath into BDD-speak.

Bill

Chris Naimoli

unread,
Mar 13, 2012, 7:24:39 PM3/13/12
to cu...@googlegroups.com
Bill, I definitely appreciate your feedback.  I figured there was a better way to do these things, but I only started using ruby/cucumber 2 weeks ago.  I got this job because I am familiar with Java and Selenium (but using it with Maven).  Any chance you can give me a short way of how to put this in the step definition?  I haven't been able to find out how to get around from the window closing every time and having to login in every time?



On Mar 13, 2012, at 4:35 PM, Bill Wright <bwr...@tendrilinc.com> wrote:

I wouldn't write this test as a Scenario Outline. It is just one test that checks for a variety of 
things on the page. I'd use a Data Table instead, so that the scenario only gets run once.

But I wouldn't even do that. I'd rewrite it like this:

Background:
Given I am on the "Login" page
And I am logged in as "ad...@test.com" using password "Test123!"
And I fill in "lastName" with "Smith"
And I select "CALIFORNIA" from "states"
And I fill in "postalCode" with "92646"
And I click on "Search" button

Scenario Outline: Login
Then I should see the home page

Then I'd write a step definition that actually verifies all that you want to see on the home page.
The low-level details of the text and class type of UI widgets should probably not be exposed in
the cucumber feature file.

Bill

Bill Wright, Senior QA/Software Engineer

5395 Pearl Parkway, Suite 100  |  Boulder, Colorado 80301
P 720.236.2035  |  F 303.926.1398
www.tendrilinc.com  |  blog

Jon Kern

unread,
Mar 18, 2012, 1:03:24 PM3/18/12
to cu...@googlegroups.com
Chris,

Here is how I do a simple smoke test for the entire UI... https://gist.github.com/2077500

I wrote these long ago, when I was more of a noob, but it might help get you started.
jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Chris Naimoli said the following on 3/13/12 7:24 PM:

cnaimoli

unread,
Mar 23, 2012, 11:33:12 AM3/23/12
to Cukes
Hey Bill,

I was thinking more about what you said here. So you would recommend
that it is not good practice to show the xpath in the actual cucumber
step? Would something like this

Cucumber Step
I click on "Expand" xpath

Step Definition
/^I click on "(.*)" xpath$/ do |xpath|
if xpath == Expand
find(:xpath, "//div[xxxx]").click
end
end

That way, the xpath is hidden and I am able to accomplish the same
thing... or would it be too much in the step definition to have a
bunch of if statements?

On Mar 13, 4:35 pm, Bill Wright <bwri...@tendrilinc.com> wrote:
> I wouldn't write this test as a Scenario Outline. It is just one test that checks for a variety of
> things on the page. I'd use a Data Table instead, so that the scenario only gets run once.
>
> But I wouldn't even do that. I'd rewrite it like this:
>
> > Background:
> >    Given I am on the "Login" page
> >    And I am logged in as "ad...@test.com" using password "Test123!"
> >    And I fill in "lastName" with "Smith"
> >    And I select "CALIFORNIA" from "states"
> >    And I fill in "postalCode" with "92646"
> >    And I click on "Search" button
>
> > Scenario Outline: Login
> >    Then I should see the home page
>
> Then I'd write a step definition that actually verifies all that you want to see on the home page.
> The low-level details of the text and class type of UI widgets should probably not be exposed in
> the cucumber feature file.
>
> Bill
>
> Bill Wright, Senior QA/Software Engineer
>
> 5395 Pearl Parkway, Suite 100  |  Boulder, Colorado 80301
> P 720.236.2035  |  F 303.926.1398www.tendrilinc.com |  blog
>
> > For more options, visit this group athttp://groups.google.com/group/cukes?hl=en.
Reply all
Reply to author
Forward
0 new messages