简体   繁体   中英

Cucumber/Automation testing: How to approach the overall concept using javascript functions?

Firstly let me say that I'm new to javascript and just starting out. This may be a broad question, but I would really like to get an answer so I can understand the whole concept/approach. I'm reading this book called More Agile Testing and there's a paragraph about automation testing and page object pattern and how it's very useful to write your automated test cases in a way so if something changes on the actual UI, I would only need to change that object/function and every other test cases that uses that object/function will continue to work after that one change.

Here's an example of what I'm thinking, let's say I have the following cucumber scenario

Given I'm on some page
When I login
Then I must see some other page

So that's a scenario above.

Should I implement my code for login like some kind of function that receives two parameters for login and password and click on needed fields/buttons on the UI. And if I have that function I would simply use When('something'), function login() etc.

And for future if I would need to use login in other test cases, I would use that function in given or when statements.

Is this the right way to go about it ?

You should implement your login code as helper function and then call that from login steps.

When you start developing your systems ability to login you will need a When step to login eg

Given I am registered
When I login
Then I should be logged in

After you have done that you can reuse this functionality in steps like

Given I am logged in

As for the implementation, I'll do it in ruby, you'll have to adapt to js.

# features/step_definitions/login_steps.rb
module LoginStepHelper
  def login_as(user:)
    # assumes user is an object that knows its email, password etc.
    ...
  end
end
World LoginStepHelper

When I login
  login_as user: @i # assumes you have setup who you are earlier
end

Given I am logged in
  login_as user: @i
end

Note it is perfectly fine to have lots of different step definitions to login, so long as they use the same helper method to do the actual work.

I strongly recommend that you don't use before hooks or tags to control login. Instead use Background if you are writing a number of scenarios where you need to be logged in to do something.

eg

Feature: Registering gives benefits

Background:
  Given I am registered
  And I am logged in


Scenario: Have a history
  When I view my history
  Then 
...

rather than

Feature: Registering gives benefits

  @logged_in
  Scenario: Have a history
    When I view my history
    ...

@hooks are designed to control the technical side of how things are run eg @wip, @javascript don't use them for business functionality.

Finally you can see this in more detail in this set of features from a sample project ( https://github.com/diabolo/cuke_up/tree/master/features )

Authentication is usually a precondition to all of your tests. From the Cucumber Wiki it is recommended as a precondition.

Given John has a Logged in 
Then Navigate the Home Page 

As such you would use a Before Hook for the scenarios, possibly with a tag so you can filter out scenarios that don't call for Authentication as a precondition such as a Failed Authentication test/scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM