Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question:Re useable of step defination method #237

Open
prasadroks opened this issue Oct 29, 2024 · 3 comments
Open

Question:Re useable of step defination method #237

prasadroks opened this issue Oct 29, 2024 · 3 comments
Labels
question Further information is requested

Comments

@prasadroks
Copy link

Is there any way where we can use the already defined step defination method in any other step defination, like the same way we can do in cucumber java bdd framework.

cucumber java bdd example:
image

There are 2 different steps but I am able to use that step defination in another step defination.
Can i achieve this in playwright-bdd, without creating any seperate function.

@prasadroks prasadroks added the question Further information is requested label Oct 29, 2024
@gitToSantosh
Copy link

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

@prasadroks
Copy link
Author

prasadroks commented Oct 30, 2024

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

test

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

30 Oct 2024
But here you have created a method in the commonPage, which was not my question, i am aware of this condition and also if i have to use that method the straight forward way would be directly calling it.

Assume I have some steps defined in
Given( 'Open {string} in browser', async ( { page }, url:string ) => {
await page.goto(url);
await page.perform_display_page_name();
} );

I have another method which would be doing the same
Given( 'navigate to {string}', async ( { page }, url:string ) => {
Here i want to use the same steps that have been given in above methods, so instead of rewriting the same again i want to make of reusability just by calling that method and passing url as an argument
} );

@gitToSantosh
Copy link

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

test

You can save the entire step into a const and export it and then call the const into the step where you want to re-use

const GO_TO_PLAYWRIGHT_HOME = Given( 'I am on Playwright home page', async ( { commonPage } ) => {
	await commonPage.navigateToUrl( 'https://playwright.dev' );
} );

When( 'I click link {string}', async ( { commonPage }, linkname: string ) => {
	await GO_TO_PLAYWRIGHT_HOME( { commonPage } );
	await commonPage.clickOnLink( linkname );
} );

30 Oct 2024 But here you have created a method in the commonPage, which was not my question, i am aware of this condition and also if i have to use that method the straight forward way would be directly calling it.

Assume I have some steps defined in Given( 'Open {string} in browser', async ( { page }, url:string ) => { await page.goto(url); await page.perform_display_page_name(); } );

I have another method which would be doing the same Given( 'navigate to {string}', async ( { page }, url:string ) => { Here i want to use the same steps that have been given in above methods, so instead of rewriting the same again i want to make of reusability just by calling that method and passing url as an argument } );

GO_TO_PLAYWRIGHT_HOME is NOT a method in commonPage. Your Steps would look like this:

export const OPEN_URL = Given( 'Open {string} in browser', async ( { page }, url:string ) => {
await page.goto(url);
await page.perform_display_page_name();
} );

\\ I have another method which would be doing the same
Given( 'navigate to {string}', async ( { page }, url:string ) => {
\\Here i want to use the same steps that have been given in above methods, so instead of rewriting the same again i want to \\make of reusability just by calling that method and passing url as an argument
\\ call the const, pass the fixtures first then the remaining args
await OPEN_URL({page}, url);
} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants