1+ package io .github .mfaisalkhatri .tests ;
2+
3+ import com .microsoft .playwright .*;
4+ import com .microsoft .playwright .options .AriaRole ;
5+ import com .microsoft .playwright .options .BoundingBox ;
6+ import com .microsoft .playwright .options .KeyboardModifier ;
7+ import com .microsoft .playwright .options .MouseButton ;
8+ import org .testng .annotations .AfterMethod ;
9+ import org .testng .annotations .BeforeMethod ;
10+ import org .testng .annotations .Test ;
11+
12+ import java .util .Arrays ;
13+ import java .util .List ;
14+
15+ import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
16+ import static org .testng .Assert .assertEquals ;
17+
18+ public class ClickOperationTests {
19+
20+ private Playwright playwright ;
21+ private Page page ;
22+
23+ @ BeforeMethod
24+ public void setup () {
25+ this .playwright = Playwright .create ();
26+ Browser browser = this .playwright .chromium ()
27+ .launch (new BrowserType .LaunchOptions ().setHeadless (false )
28+ .setChannel ("chrome" ));
29+ this .page = browser .newPage ();
30+ }
31+
32+ @ Test
33+ public void testLeftClick () {
34+ this .page .navigate ("https://the-internet.herokuapp.com/" );
35+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Challenging DOM" ))
36+ .click ();
37+ //Locator challenginDomLink = page.getByRole (AriaRole.LINK,
38+ // new Page.GetByRoleOptions ().setName ("Challenging DOM"));
39+ //challenginDomLink.click ();
40+
41+ assertThat (this .page .getByRole (AriaRole .HEADING ,
42+ new Page .GetByRoleOptions ().setName ("Challenging DOM" ))).isVisible ();
43+ }
44+
45+ @ Test
46+ public void testRightClick () {
47+ this .page .navigate ("https://the-internet.herokuapp.com/context_menu" );
48+ this .page .locator ("#hot-spot" )
49+ .click (new Locator .ClickOptions ().setButton (MouseButton .RIGHT ));
50+ this .page .onDialog (dialog -> {
51+ String alertText = dialog .message ();
52+ assert alertText .equals ("You selected a context menu" );
53+ dialog .accept ();
54+ });
55+ }
56+
57+ @ Test
58+ public void testDoubleClick () {
59+ this .page .navigate ("https://demo.guru99.com/test/simple_context_menu.html" );
60+
61+ Locator alertBtn = this .page .locator ("#authentication > button" );
62+ this .page .onDialog (dialog -> {
63+ String text = "You double clicked me.. Thank You.." ;
64+ assertEquals (dialog .message (), text );
65+ dialog .accept ();
66+ });
67+ alertBtn .dblclick ();
68+ }
69+
70+ @ Test
71+ public void testMouseHover () {
72+ this .page .navigate ("https://automationteststore.com/" );
73+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName (" Apparel & accessories" ))
74+ .hover ();
75+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Shoes" )
76+ .setExact (true ))
77+ .click ();
78+ assertThat (
79+ this .page .getByRole (AriaRole .HEADING , new Page .GetByRoleOptions ().setName ("Shoes" ))).isVisible ();
80+ }
81+
82+ @ Test
83+ public void testForceMouseClick () {
84+ this .page .navigate ("https://automationteststore.com/" );
85+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Specials" ))
86+ .click (new Locator .ClickOptions ().setForce (true ));
87+ assertThat (this .page .getByRole (AriaRole .HEADING ,
88+ new Page .GetByRoleOptions ().setName ("Special Offers" ))).isVisible ();
89+ }
90+
91+ @ Test
92+ public void testPositionBasedClick () {
93+ this .page .navigate ("https://the-internet.herokuapp.com/horizontal_slider" );
94+ Locator slider = this .page .getByRole (AriaRole .SLIDER );
95+ BoundingBox box = slider .boundingBox ();
96+ double width = box .width ;
97+ double height = box .height ;
98+
99+ double x = width * 0.5 ;
100+ double y = height * 0.5 ;
101+
102+ slider .click (new Locator .ClickOptions ().setPosition (x , y ));
103+ this .page .waitForTimeout (3000 );
104+
105+ }
106+
107+ @ Test
108+ public void testClickWithModifiers () {
109+ this .page .navigate ("https://jqueryui.com/selectable/" );
110+ FrameLocator frame = this .page .frameLocator (".demo-frame" );
111+
112+ Locator items = frame .locator ("#selectable li" );
113+
114+ items .nth (0 )
115+ .click (new Locator .ClickOptions ().setModifiers (List .of (KeyboardModifier .CONTROL )));
116+
117+ items .nth (3 )
118+ .click (new Locator .ClickOptions ().setModifiers (List .of (KeyboardModifier .CONTROL )));
119+
120+ this .page .waitForTimeout (2000 );
121+
122+ this .page .navigate ("https://the-internet.herokuapp.com/" );
123+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("File Download" )
124+ .setExact (true ))
125+ .click (new Locator .ClickOptions ().setModifiers (List .of (KeyboardModifier .SHIFT )));
126+
127+ this .page .waitForTimeout (2000 );
128+
129+ }
130+
131+ @ Test
132+ public void testDelayClick () {
133+ this .page .navigate ("https://the-internet.herokuapp.com/" );
134+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Challenging DOM" ))
135+ .click (new Locator .ClickOptions ().setDelay (2000 ));
136+ assertThat (this .page .getByRole (AriaRole .HEADING ,
137+ new Page .GetByRoleOptions ().setName ("Challenging DOM" ))).isVisible ();
138+ }
139+
140+ @ Test
141+ public void testDragAndDropActions () {
142+ this .page .navigate ("https://the-internet.herokuapp.com/" );
143+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Drag and Drop" ))
144+ .click ();
145+ this .page .locator ("#column-a" )
146+ .dragTo (this .page .locator ("#column-b" ));
147+ assertThat (this .page .locator ("#column-a" )).containsText ("B" );
148+
149+ }
150+
151+ @ Test
152+ public void testDragAndDropManually () {
153+ this .page .navigate ("https://the-internet.herokuapp.com/" );
154+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Drag and Drop" ))
155+ .click ();
156+ this .page .locator ("#column-a" )
157+ .hover ();
158+ this .page .mouse ()
159+ .down ();
160+ this .page .locator ("#column-b" )
161+ .hover ();
162+ this .page .mouse ()
163+ .up ();
164+ assertThat (this .page .locator ("#column-a" )).containsText ("B" );
165+ }
166+
167+ @ Test
168+ public void testProgrammaticClick () {
169+ this .page .navigate ("https://the-internet.herokuapp.com/" );
170+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Checkboxes" ))
171+ .dispatchEvent ("click" );
172+
173+ assertThat (this .page .getByRole (AriaRole .HEADING )).containsText ("Checkboxes" );
174+
175+ }
176+
177+ @ AfterMethod
178+ public void tearDown () {
179+ this .page .close ();
180+ this .playwright .close ();
181+ }
182+ }
0 commit comments