In this blog we will be utilizing Playwright Java to handle mouse right click and will see the usage of steam() and has-text() methods.

Topics that we will cover:

  • Mouse ‘Right click’ operation
  • Usage of stream() and has-text() methods
  • Source code (mouse right click)
  • Source code (stream() , has-text())

Mouse ‘Right click’ operation

Go to https://demo.automationtesting.in/Windows.html 

Our goal is to right click ‘click’ button so that the right click menu comes up

Inspect ‘click’ button and copy the xpath

 //*[@id="Tabbed"]/a/button

We can now use this xpath along with ‘MouseButton.RIGHT’ method as shown below 

 Execute and notice below that mouse right click operaion is successful and the right click menu comes up

 Usage of stream() and has-text() methods

Let us go to https://www.amazon.in/ 

Our task is to extract all the links from the page that have the text ‘Amazon’

 Now link is represented by ‘a’ tag. Now there is a ‘has-text()’ method that we can use to extract all the links having text ‘Amazon’, as shown below

 So our code logic would like as shown below

 Execute.

Notice below that all the link texts are printed that have the text ‘Amazon’

 So this is how we can use has-text() alongwith stream() method.

Source code (mouse right click)

package com.rsa.playwrightjava;


import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Locator;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

import com.microsoft.playwright.options.MouseButton;

public class Blog35_RightClick_PWJava {

private Browser browser;

private Page page;

@BeforeMethod

public void setUp() {

Playwright playwright = Playwright.create(); 

browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

page = browser.newPage();

}

@Test

public void test_1() {


page.navigate("https://demo.automationtesting.in/Windows.html");

page.locator("//*[@id=\"Tabbed\"]/a/button").click(new Locator.ClickOptions().setButton(MouseButton.RIGHT));

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Source code (stream() , has-text())

package com.rsa.playwrightjava;


import java.util.List;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog35_SteamHasText_PWJava {

private Browser browser;

private Page page;

@BeforeMethod

public void setUp() {

Playwright playwright = Playwright.create(); 

browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

page = browser.newPage();

}

@Test

public void test_1() {

page.navigate("https://www.amazon.in/");

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

List<String> linksListAmaz = page.locator("a:has-text('Amazon')").allInnerTexts();

linksListAmaz.stream().forEach(l -> System.out.println(l));

}

@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Thanks.


Tags


You may also like

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}