Skip to content

Commit 4095505

Browse files
added test examples for click operations (#94)
* added new class for creating click operations * added example code for left mouse click * added second approach to left click * fixed the dropdown test by refactoring the select by value test with th-internet website * replaced the demo website for multi dropdown tests, fixed multi dropdown tests * removed commented code * added example code for right mouse click * added force click and positio nbased click tests * click with modifier example wip * updated playwright dependency version, added test for delay click, drag and drop and click with modifiers * added test examples for programmatic click and drag and drop actions
1 parent 1dc4295 commit 4095505

3 files changed

Lines changed: 222 additions & 15 deletions

File tree

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}

src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package io.github.mfaisalkhatri.tests;
22

3-
import com.microsoft.playwright.*;
3+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
4+
import static org.testng.Assert.assertEquals;
5+
6+
import java.util.regex.Pattern;
7+
8+
import com.microsoft.playwright.Browser;
9+
import com.microsoft.playwright.BrowserType;
10+
import com.microsoft.playwright.Locator;
11+
import com.microsoft.playwright.Page;
12+
import com.microsoft.playwright.Playwright;
13+
import com.microsoft.playwright.options.AriaRole;
414
import com.microsoft.playwright.options.SelectOption;
515
import org.testng.annotations.AfterClass;
616
import org.testng.annotations.BeforeClass;
717
import org.testng.annotations.Test;
818

9-
import java.util.regex.Pattern;
10-
11-
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
12-
import static org.testng.Assert.assertEquals;
13-
1419
public class DropdownTests {
1520

1621
private Playwright playwright;
@@ -65,23 +70,25 @@ public void testSelectByIndex() {
6570
@Test
6671
public void testSelectByValue() {
6772

68-
this.page.navigate("https://www.lambdatest.com/selenium-playground/select-dropdown-demo");
69-
final Locator dropdownField = this.page.locator("#select-demo");
73+
this.page.navigate ("https://the-internet.herokuapp.com/dropdown");
7074

71-
dropdownField.selectOption(new SelectOption().setValue("Wednesday"));
75+
this.page.getByRole (AriaRole.COMBOBOX)
76+
.selectOption ("Option 2");
7277

73-
final Locator daySelected = this.page.locator(".pt-10 p");
74-
assertThat(daySelected).hasText("Day selected :- Wednesday");
78+
assertThat (page.getByRole (AriaRole.COMBOBOX)).containsText ("Option 2");
7579
}
7680

7781
@Test
7882
public void testMultiSelectOptions() {
79-
this.page.navigate("https://www.lambdatest.com/selenium-playground/select-dropdown-demo");
83+
this.page.navigate("https://testautomationpractice.blogspot.com/");
8084

81-
final Locator dropdownField = this.page.locator("#multi-select");
82-
dropdownField.selectOption(new SelectOption[]{new SelectOption().setLabel("New York"), new SelectOption().setLabel("Texas"), new SelectOption().setValue("California"), new SelectOption().setIndex(7)});
85+
final Locator dropdownField = this.page.getByRole (AriaRole.LISTBOX, new Page.GetByRoleOptions ().setName (
86+
"Sorted List:"));
87+
dropdownField.selectOption (new SelectOption [] {new SelectOption ().setLabel ("Cat"),
88+
new SelectOption ().setLabel ("Cheetah"), new SelectOption ().setIndex (2)});
8389

84-
assertThat(dropdownField).hasValues(new Pattern[]{Pattern.compile("California"), Pattern.compile("New York"), Pattern.compile("Texas"), Pattern.compile("Washington")});
90+
assertThat(dropdownField).hasValues (new Pattern[]{Pattern.compile ("cat"),
91+
Pattern.compile ("cheetah"), Pattern.compile ("deer")});
8592
}
8693

8794
@AfterClass
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Playwright Demo Tests ">
4+
<test name="Click operation tests">
5+
<classes>
6+
<class name="io.github.mfaisalkhatri.tests.ClickOperationTests">
7+
<methods>
8+
<include name="testLeftClick"/>
9+
<include name="testRightClick"/>
10+
<include name="testDoubleClick"/>
11+
<include name="testMouseHover"/>
12+
<include name="testForceMouseClick"/>
13+
<include name="testPositionBasedClick"/>
14+
</methods>
15+
</class>
16+
</classes>
17+
</test>
18+
</suite>

0 commit comments

Comments
 (0)