• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a better way to do this?

 
Ranch Hand
Posts: 337
6
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Is there a better way than using Java's switch case to handle this scenario?  

I'm using the Cucumber framework and creating some test cases to verify whether specific links exists on a webpage.  

For starters right now,  I'm verifying if I can find the link, if I can click on the link and after I click on the link, whether the page loads, what the webpage title is, and what the URL is.   In my example code below ( just an example ) each of the switch cases is handling an assert statement which verifies what the page's URL value is.    It gets the job done but I'm just curious if I can do better?

Thank You in advance.  


Example code

 
Marshal
Posts: 4525
572
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
This isn't that much of an improvement, but it is a bit nicer to read/maintain (Java17+).
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Number of slices to send:
    Optional 'thank-you' note:
Thank You so much!  I ALWAYS appreciate the help.  
 
Sheriff
Posts: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:
Any kind of conditional control in a test is a smell. The problem is that it makes the test brittle when more conditions need to be covered. In your case, additional word values will make it necessary to add another case to the switch statement. As the number of words grow, so will the complexity of that test step definition.

Another thing about conditionals in a test case is that there are multiple reasons for the test to fail. Ideally, a failing test should allow you to zero in quickly on a problem in the code. Multiple reasons for failing diminishes the usefulness of a test in finding problems quickly. This is in general. In your case, however, it's not too big of concern because any failure of that test can be generalized as "expected URL not found". But keep the general caution in mind for future reference.

What I would try to do instead is to create a map of (word ‐> URL) and the use the word to look up the expected URL. This way your test itself will have stable code and you only need to add entries to the map whenever a new word and its related URL are introduced.

This alternative approach may seem like "the same difference" but it's really not. The code with switch/case is a test with conditionals whereas the code that uses a map lookup is parameterized and more resilient to changes.
 
Junilu Lacar
Sheriff
Posts: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I was reading your OP on my phone earlier and I didn't see all of the code you posted. Now that I've seen it fully, however, I see you're making assertions in a step that is annotated with @And("user clicks on {word} hyperlink"). I wouldn't do that.

The problem is that this step is part of the "Act" portion of the test, not the "Assert" (from Arrange, Act, Assert pattern).

The annotation itself implies your Gherkin specification has statements like:

And User clicks on the Blog hyperlink
And User clicks on the Contact hyperlink
And User clicks on the Security hyperlink

a better formulation won't imply implementation details:

And User navigates to the Blog page
And User navigates to the Contact page
And User navigates to the Security page

A specification should be focused on behavior or intent, not implementation. The words "clicks" and "hyperlink" speak to the implementation details rather than the behavior or intent.

In a step that is annotated with an action like this, I would instead save some aspect of the state that I would be interested in asserting later on in an @Then step.

For example:

Caveat: the above is example code only that represents an idea off the top of my head; I haven't tried to actually run it but it outlines the general structure and organization of the step definitions as I would approach it.

Explanation of the NOT_FOUND value: I would set up a page specifically for redirecting errors to. This page would take a parameter that will help you figure out what the problem was. For example, I might establish a route for "/unmappedLink" and have it return a page that had a title (or header or whatever kind of element you prefer) of "Error: unmapped link for word: {word}". In that case, the declaration would be:

Be sure to enable this route for testing only and disable in production otherwise you'll inadvertently create a potential backdoor vulnerability.
 
Junilu Lacar
Sheriff
Posts: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:

Junilu Lacar wrote:
This alternative approach may seem like "the same difference" but it's really not. The code with switch/case is a test with conditionals whereas the code that uses a map lookup is parameterized and more resilient to changes.


Put another way, a parameterized test essentially creates a set of separate tests for different scenarios. The test code itself simply becomes the template for the logic. A test with conditionals puts all the eggs in one basket and if any one thing in there breaks it will look like everything broke.
 
Junilu Lacar
Sheriff
Posts: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
You can also use a data table with word and URL in your test specification (the Gherkin part) -- this allows you to parameterize at the specification level instead of in the step definition as I showed earlier.

Bottom line, if you find yourself writing multiple conditionals in a test or step definition, then think about changing to a data-driven parameterized test instead.
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Number of slices to send:
    Optional 'thank-you' note:

Junilu Lacar wrote:You can also use a data table with word and URL in your test specification (the Gherkin part) -- this allows you to parameterize at the specification level instead of in the step definition as I showed earlier.

Bottom line, if you find yourself writing multiple conditionals in a test or step definition, then think about changing to a data-driven parameterized test instead.



Thank you for all your help and advice. I'm going to try and implement the changes you suggested regarding the data table .  The steps are written by our product owners on a Jira xray test and then we implement the Gherkin , step defs etc.  So I try and follow what the product owners had for the xray tests.

So for one of the tests it's actually like

Scenario:  Verify user should navigate to Security page upon Clicking Security hyperlink
  When  User confirms they see the Security hyperlink
  And  User clicks on the Security hyperlink
  Then User has landed on the Security page


So if I understand correctly and  write it up the way you suggest ,


Scenario:  Verify user should navigate to Security page upon Clicking Security hyperlink
  When  User confirms they see the Security hyperlink
  Then User navigates to the Security page

Is that right?  


 
Junilu Lacar
Sheriff
Posts: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I suggest you go to https://school.cucumber.io and watch the training video they have up there. It's free and very informative.

What you're describing is a typical subversion of the BDD process. This happens when teams use the tools and the Gherkin format without understanding how they're supposed to be used and how the process actually works.

In summary, the BDD approach consists of three practices:

1. Discovery - this is where business, development, and test come together to have a "Three Amigos" conversation. They draw up examples of the different scenarios, without using the Gherkin format. There's a book on the Discovery phase written by Seb Rose and Gaspar Nagy. They also wrote other books on each of the other practices I describe here. Nagy and Rose advise against using the Gherkin format during discovery. Focus on gathering specific examples and the behaviors of the system as users interact with it.

2. Formulation - after discovery, the team will have several concrete examples to feed into this step. Developers and testers will get together and formulate the Gherkin specifications. They will use the examples from Discovery to draw up different scenarios. They will then go back to the larger group and review the formulations to verify that they got everything right and that there was nothing missed. They could also bring up new examples they might have thought about during formulation or make suggestions on how to simplify the scenario.  In the video on school.cucumber.io, they show examples of how the formulated specifications can actually be refactored as a result of the review.

3. Automation - this is when developers (and possibly testers) automate the tests by adding step definitions in the form of test code.

This is the overall BDD cycle and it feeds other parts of the development process, specifically the TDD cycle, where developers develop the functionality with the support of the BDD specifications, which basically become the acceptance tests.

How I would write your specifications:

Scenario: User navigates to the Security Page from the Home Page

  Given the user has logged in
  And is viewing the Home page
  And has access to the Security page
  When the user navigates to the Security page
  Then the title on the page should be "Security Settings"


Note again that any mention of "click" and "hyperlink" should be avoided. These are implementation details. You want to avoid implementation details so that the test will still be valid even though the implementation may have changed. For example, somewhere down the road, a decision might be made to use a hamburger menu option instead of a hyperlink.

Again, I encourage you to educate yourself on the proper BDD process by going through the free tutorials on school.cucumber.io

 
Junilu Lacar
Sheriff
Posts: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Here's what Nagy and Rose wrote in their "Discovery" book:

Gaspar Nagy and Seb Rose wrote:
BDD is an Agile approach that consists of three practices that have to be addressed in order. The first practice is discovery, a structured, collaborative activity that uses concrete examples to uncover the ambiguities and misunderstandings that traditionally derail software projects.

The second practice is formulation a creative process that turns the concrete examples produced during discovery into business-readable scenarios. The subsequent review of the scenarios delivers the confidence that the team really has understood what the business is asking for.

The third, and final, practice is automation where code is written that turns the scenarios into tests.

 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks for the suggestion!  I have on my ToDo to take a cucumber course so this helps me out a lot for a suggested course.   I will do that.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |