Taiko: Generate gauge specification and implementation's from recorded script

Created on 22 Nov 2018  路  10Comments  路  Source: getgauge/taiko

A user should be able to generate gauge specifications along with implemented steps from a recorded taiko script.
When a user records a complete workflow it can turn into a gauge scenario.
Example:-
The following taiko script

await openBrowser();
await goto("google.com");
await write("taiko test automation");
await click("Google Search");
await closeBrowser();
can be converted into following

googleSearch.spec

# Specification

* open browser

## Scenario
* go to "google.com"
* write "taiko test automation"
* click "Google Search"

___

* close browser

and

googleSearchImplementation.js

step("open browser", async () => openBrowser());

step("go to <url>", async (url) => {
   await goto(url);
});

step("write <query>", async (query) => {
    await write(query);
})

step("click <link>", async (link) => {
    await click(link)
});

step("close browser", async () => closeBrowser());

Most helpful comment

Other points.

  • .step will not create a spec file
  • .step will not initialize a gauge project.

Initializing a gauge project etc needs to be done outside taiko. Or on a separate command.

All 10 comments

DevNotes

Approach to achieve this

The decided solution is to generate the code from taiko

  • Have another command in taiko REPL (let's say .gauge)
  • User records a complete flow and use .gauge command
  • If a filename is not provided as an argument to .gauge command it will show the generated specification and implementations on the console.
  • if a filename if given figure out if it is a gauge project or not (based on file path).

    • if given name belongs to a gauge project Add the specification and implementations to the gauge project.

    • Or create a new gauge project and add spec to that.

Problems/Challanges

  • We might end up creating duplicate steps

How about recording will be converted to one method instead of one for each line.

With ability to load a script before recording https://github.com/getgauge/taiko/issues/272. One can record a logical chunk at a time. A scenario has many logical chunks.

Example - booking a movie ticket

  • This has movie selection, seat selection, payment...
  • Let's say you record script to select a movie first movie.js then selecting gold/executive seats are 2 separate logical chunks. Both can be recorded after playing back movie.js.
  • Finally payment flow can be recorded.
  • Finally when movie.js, goldseat.js, executiveseat.js payment.js are generated by taiko all can be converted to gauge steps for movieselection, seatselection,paymentoption... which user can work with and continue to refactor.

With this one can avoid generating code for duplicate steps as well. As the user can load a recorded script to avoid re-recording steps.

Thoughts?

@sguptatw This make sense.

  • Prompt user to enter spec/scenario/step name
  • Create scenario with only one step
  • exclude openBrowser and closeBrowser if the given path belongs to a gauge project
  • Add openBrowser and closeBrowser as beforeScenario and afterSceanrio if project is not gauge project (after creating a new project).

Please add your thoughts.

Here's what I think works best.

$ taiko
> openBrowser()
> goto("google.com")
> write("Gauge test automation")
> click("google search")
> closeBrowser()
> .step
step("<insert step name>", async () => {
    goto("google.com");
    write("Gauge test automation")
    press("Enter")
});

This is for copying it to a step implementation file or previewing the step. Note, that Taiko does not ask for the step name, use a place holder instead.

To save it to an external file

> .step tests/google.js

If the file does not exist generate the following.

/* globals gauge*/
"use strict";
const { openBrowser,write, closeBrowser, goto, press, text, contains } = require('taiko');

beforeSuite(async () => openBrowser());
afterSuite(async () => closeBrowser());

step("<insert step name>", async () => {
    goto("google.com");
    write("Gauge test automation")
    press("Enter")
});

If a file is already there, with steps, append to it. For e.g.

/* globals gauge*/
"use strict";
const { openBrowser,write, closeBrowser, goto, press, text, contains } = require('taiko');

beforeSuite(async () => openBrowser({ headless: headless }));
afterSuite(async () => closeBrowser());

step("Pre existing step", async () => {
  goto('foo');
  click('bar');
});

step("<insert step name>", async () => {
    goto("google.com");
    write("Gauge test automation")
    press("Enter")
});

Other points.

  • .step will not create a spec file
  • .step will not initialize a gauge project.

Initializing a gauge project etc needs to be done outside taiko. Or on a separate command.

Questions

  • If the generated code is having step text as "<insert step text here>" in the specification it is getting auto-completed as a step with only a static parameter.
    Should the step text being generated reconsidered?
  • When multiple .step are recorded for the same project and code is added to gauge project, multiple hooks with open and closeBrowser are generated. Should the code generation for the hook be separated like
/*hook to open and close browser. Do not add this if it already exists*/
"use strict";
const { openBrowser, closeBrowser } = require('taiko');

beforeSuite(async () => openBrowser());
afterSuite(async () => closeBrowser());

/* globals gauge*/
"use strict";
const { write, goto, press, text, contains } = require('taiko');

step("insert step name", async () => {
    goto("google.com");
    write("Gauge test automation")
    press("Enter")
});

@sguptatw

  • I think rather than having <insert step text here> as the text, it could be a comment, and the step text can be either an empty string or no value. No value will cause a syntax error.

Something like this:

// Insert step text below as first parameter
step("", async () => {
// generated code
})
  • its worth not duplicating the hooks, IMO. Even not generating a hook second time (irrespective of whether the previous hook has openBrowser etc calls is better than duplicating it.

A step is generated for a gauge project. A project initialized with the template(the default is taiko template) already has the hook with openBrowser and closeBrowser. So on second thoughts I think we need not generate the hook details at all. Thoughts?

As the openBrowser and closeBrowser happens in the beforeSuite and afterSuite .step must not generate hook code as it will be initialized during gauge init js

The required changes has been made and should be available in https://github.com/getgauge/taiko/commit/06bd1cd297ae27034f51f1c566202a563e3259de .

Was this page helpful?
0 / 5 - 0 ratings