Fill Form Automatically using Json file in Playwright-Java

Tutorial 9​​ ​​ Fill Form Automatically using Json file​​ in​​ Playwright-Java​​ 

What you will Learn​​ in this​​ blog:

  • Create simple login script

  • Store login steps in a json

  • Execute script using json to automatically login

  • Code snippets

Create simple login script

Launch​​ http://www.automationpractice.com/​​ 

Click Sign-in​​ 

Login with dummy email and password

At the top right hand side, we can see the ‘Sign out’ button along with username (here ‘hello hello’)

You can create your own account

Let us automate these simple steps

Run​​ the script, the login process is successful

Store login steps in a json

Let us now create a json file to store this automation info (to​​ fill the form and logging in). We can do this by adding the standard code snippet​​ at​​ line#26

Now when we run the script, the json gets generated (refresh the project to see it)

Open this file, the entire login info gets stored in a json file

Close the json.

Execute script using json to automatically login

Comment​​ lines#21-27

So now, our script does​​ not​​ have automation steps to fillup the form.

Also comment line#16, see below.

We will now re-write a modified form of above line viz line#17​​ 

BrowserContext​​ browserContext​​ =​​ browser.

 newContext(new​​ Browser.NewContextOptions().setStorageStatePath(Paths.get("fillForm.json")));

So we are asking playwright to read the stored json file at runtime.

Run the script.​​ 

Notice below that, the playwright reads the json file at runtime and automatically fills the form and gets logged in

This is a useful feature to re-use the login code inside multiple scripts. We don’t have to write the same code​​ again and again in different sripts. We can simply call the json file.

Code snippet​​ (login​​ without json file)

package​​ com.w2a.pwjava;

 

import​​ java.nio.file.Paths;

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ FillFormAutomaticallyUsingJsonInPW {

 

public​​ static​​ void​​ main(String[]​​ args) {

 Playwright​​ pw​​ = Playwright.create();

 Browser​​ browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

 BrowserContext​​ browserContext​​ =​​ browser.newContext();

 Page​​ page​​ =​​ browserContext.newPage();

page.navigate("https://www.automationpractice.com/");

 

page.click("a:text('Sign in')");

  ​​ ​​ ​​ ​​ ​​​​ page.fill("#email","nnwa@gmail.com");

  ​​ ​​ ​​ ​​ ​​​​ page.fill("#passwd","dummy");

  ​​ ​​ ​​ ​​ ​​​​ page.click("#SubmitLogin");

  ​​ ​​ ​​ ​​ ​​​​ 

 }

}

 

Code snippet (to generate​​ json file)

package​​ com.w2a.pwjava;

 

import​​ java.nio.file.Paths;

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ FillFormAutomaticallyUsingJsonInPW {

 

public​​ static​​ void​​ main(String[]​​ args) {

 Playwright​​ pw​​ = Playwright.create();

 Browser​​ browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

 BrowserContext​​ browserContext​​ =​​ browser.newContext();

 Page​​ page​​ =​​ browserContext.newPage();

page.navigate("https://www.automationpractice.com/");

 

page.click("a:text('Sign in')");

  ​​ ​​ ​​ ​​ ​​​​ page.fill("#email","nnwa@gmail.com");

  ​​ ​​ ​​ ​​ ​​​​ page.fill("#passwd","dummy");

  ​​ ​​ ​​ ​​ ​​​​ page.click("#SubmitLogin");

  ​​ ​​ ​​ ​​ ​​​​ 

  ​​ ​​ ​​ ​​ ​​​​ browserContext.storageState(new​​ BrowserContext.StorageStateOptions().setPath(Paths.get("fillForm.json")));

 }

}

 

Code snippet (using json file​​ to automatically login)

package​​ com.w2a.pwjava;

 

import​​ java.nio.file.Paths;

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ FillFormAutomaticallyUsingJsonInPW {

 

public​​ static​​ void​​ main(String[]​​ args) {

 Playwright​​ pw​​ = Playwright.create();

 Browser​​ browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

 BrowserContext​​ browserContext​​ =​​ browser.newContext(new​​ Browser.NewContextOptions().setStorageStatePath(Paths.get("fillForm.json")));

//BrowserContext browserContext = browser.newContext();

 Page​​ page​​ =​​ browserContext.newPage();

page.navigate("https://www.automationpractice.com/");

/*

 page.click("a:text('Sign in')");

  ​​​​  ​​ ​​ ​​​​ page.fill("#email","nnwa@gmail.com");

  ​​ ​​ ​​ ​​ ​​​​ page.fill("#passwd","dummy");

  ​​ ​​ ​​ ​​ ​​​​ page.click("#SubmitLogin");

  ​​ ​​ ​​ ​​ ​​​​ 

  ​​ ​​ ​​ ​​ ​​​​ browserContext.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("fillForm.json")));

*/

 }

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024