-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlaywrightiumBasicTests.java
277 lines (230 loc) · 11.4 KB
/
PlaywrightiumBasicTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package org.brit.test.playwrightium;
import com.github.javafaker.Faker;
import com.microsoft.playwright.options.AriaRole;
import org.brit.additional.PlaywrightiumSelect;
import org.brit.driver.PlaywrightiumDriver;
import org.brit.locators.ArialSearchOptions;
import org.brit.locators.PlaywrightiumBy;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.ISelect;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.File;
import java.time.Duration;
import java.util.List;
import java.util.regex.Pattern;
import static com.codeborne.selenide.Selenide.sleep;
import static org.assertj.core.api.Assertions.assertThat;
public class PlaywrightiumBasicTests {
static WebDriver driver;
@BeforeClass
public void beforeClass() {
// PlaywrightiumOptions playwrightiumOptions = new PlaywrightiumOptions();
// playwrightiumOptions.setRecordsFolder(Paths.get("videos"));
driver = new PlaywrightiumDriver();
}
@AfterClass
public void afterClass() {
if (driver != null) {
driver.quit();
}
}
@Test
public void basicFunctionalTests() {
final String url = "https://testpages.eviltester.com/styled/basic-html-form-test.html";
Faker faker = new Faker();
driver.get(url);
String name = faker.name().firstName();
String password = faker.internet().password();
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);
String paragraph = faker.lorem().paragraph(5);
WebElement textarea = driver.findElement(By.cssSelector("textarea[name=comments]"));
textarea.clear();
textarea.sendKeys(paragraph);
int checkBoxNum = faker.number().numberBetween(1, 4);
List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@type='checkbox']"));
checkboxes.stream().filter(WebElement::isSelected).forEach(WebElement::click);
WebElement checkBoxChecked = checkboxes.get(checkBoxNum - 1);
String checkBoxValue = checkBoxChecked.getAttribute("value");
checkBoxChecked.click();
int radioButtonNum = faker.number().numberBetween(1, 4);
WebElement radioButton = driver.findElement(By.xpath("//input[@type='radio'][@value='rd%s']".formatted(radioButtonNum)));
String radioButtonValue = radioButton.getAttribute("value");
radioButton.click();
ISelect selectMulti = new PlaywrightiumSelect(driver.findElement(By.name("multipleselect[]")));
selectMulti.deselectAll();
int firstItem = faker.number().numberBetween(0, 4);
int secondItem = randomIntExcept(0, 4, firstItem);
selectMulti.selectByIndex(firstItem);
selectMulti.selectByIndex(secondItem);
List<String> selectMultyValues = selectMulti.getAllSelectedOptions().stream().map(p -> p.getAttribute("value")).toList();
ISelect select = new PlaywrightiumSelect(driver.findElement(By.name("dropdown")));
select.selectByValue("dd" + faker.number().numberBetween(1, 7));
String selectValue = select.getFirstSelectedOption().getAttribute("value");
File file = new File(getClass().getClassLoader().getResource("textLorem.txt").getFile());
driver.findElement(By.name("filename")).sendKeys(file.getAbsolutePath());
driver.findElement(By.cssSelector("input[value=submit][name=submitbutton]")).click();
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(.,'Processed Form Details')]")));
assertThat(getWebElementTextById("_valueusername", driver))
.isEqualTo(name);
assertThat(getWebElementTextById("_valuepassword", driver))
.isEqualTo(password);
assertThat(getWebElementTextById("_valuecomments", driver))
.isEqualTo(paragraph);
assertThat(getWebElementsTextsBy("_valuecheckboxes", driver))
.asList()
.isEqualTo(List.of(checkBoxValue));
assertThat(getWebElementTextById("_valueradioval", driver))
.isEqualTo(radioButtonValue);
assertThat(getWebElementsTextsBy("_valuemultipleselect", driver))
.asList()
.isEqualTo(selectMultyValues);
assertThat(getWebElementTextById("_valuedropdown", driver))
.isEqualTo(selectValue);
assertThat(getWebElementTextById("_valuefilename", driver))
.isEqualTo(file.getName());
((JavascriptExecutor) driver).executeScript("return alert();");
}
@Test(dataProvider = "dataProvider")
public void framesTest(String frameName, String frameText, int elementsCount) {
driver.get("https://testpages.eviltester.com/styled/frames/frames-test.html");
driver.switchTo().frame(frameName);
String text = driver.findElement(By.xpath("//h1")).getText();
int size = driver.findElements(By.xpath("//ul/li")).size();
assertThat(text).isEqualTo(frameText);
assertThat(size).isEqualTo(elementsCount);
driver.switchTo().defaultContent();
assertThat(driver.findElements(By.xpath("//frame")).size()).isEqualTo(5);
}
@Test
public void alertsTest() {
driver.get("https://testpages.eviltester.com/styled/alerts/alert-test.html");
Alert alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.id("alertexamples")).click();
assertThat(alert.getText()).isEqualTo("I am an alert box!");
alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.id("confirmexample")).click();
assertThat(alert.getText()).isEqualTo("I am a confirm alert");
assertThat(driver.findElement(By.id("confirmexplanation")).getText()).isEqualTo("You clicked OK, confirm returned true.");
assertThat(driver.findElement(By.id("confirmreturn")).getText()).isEqualTo("true");
alert = driver.switchTo().alert();
alert.dismiss();
driver.findElement(By.id("confirmexample")).click();
assertThat(alert.getText()).isEqualTo("I am a confirm alert");
assertThat(driver.findElement(By.id("confirmexplanation")).getText()).isEqualTo("You clicked Cancel, confirm returned false.");
assertThat(driver.findElement(By.id("confirmreturn")).getText()).isEqualTo("false");
String testString = "Test string";
alert = driver.switchTo().alert();
alert.sendKeys(testString);
alert.accept();
driver.findElement(By.id("promptexample")).click();
assertThat(alert.getText()).isEqualTo("I prompt you");
assertThat(driver.findElement(By.id("promptexplanation")).getText()).isEqualTo("You clicked OK. 'prompt' returned " + testString);
assertThat(driver.findElement(By.id("promptreturn")).getText()).isEqualTo(testString);
alert = driver.switchTo().alert();
alert.sendKeys(testString);
alert.dismiss();
driver.findElement(By.id("promptexample")).click();
assertThat(alert.getText()).isEqualTo("I prompt you");
assertThat(driver.findElement(By.id("promptexplanation")).getText()).isEqualTo("You clicked Cancel. 'prompt' returned null");
}
@Test
public void dragAndDropTest() {
driver.get("https://testpages.eviltester.com/styled/drag-drop-javascript.html");
assertThat(driver.findElement(By.id("droppable1")).getText().trim()).contains("Drop here");
new Actions(driver)
.dragAndDrop(driver.findElement(By.id("draggable1")), driver.findElement(By.id("droppable1")))
.pause(Duration.ofSeconds(2))
.build()
.perform();
assertThat(driver.findElement(By.id("droppable1")).getText().trim()).contains("Dropped!");
}
@Test
public void moveToTest() {
driver.get("https://testpages.eviltester.com/styled/csspseudo/css-hover.html");
Actions actions = new Actions(driver);
actions
.moveToElement(driver.findElement(By.id("hoverpara")))
.build()
.perform();
assertThat(driver.findElement(By.id("hoverparaeffect")).isDisplayed()).isTrue();
assertThat(driver.findElement(By.id("hoverparaeffect")).getText())
.isEqualTo("You can see this paragraph now that you hovered on the above 'button'.");
actions
.moveToElement(driver.findElement(By.id("hoverdiv")))
.perform();
driver.findElement(By.id("hoverlink")).click();
WebElement aReturn = driver.findElement(PlaywrightiumBy.byRole(AriaRole.LINK,
new ArialSearchOptions().setName(Pattern.compile("Return"))));
new WebDriverWait(driver, Duration.ofSeconds(5))
.until(ExpectedConditions.visibilityOf(aReturn));
}
@Test
public void actionTest() {
driver.get("https://testpages.herokuapp.com/styled/basic-html-form-test.html");
String testMessage = "Test message 1";
String testMessage2 = "Test 2 message";
WebElement comments = driver.findElement(By.name("comments"));
Actions actions = new Actions(driver);
Keys cmdCtrl = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;
comments.clear();
new Actions(driver)
.sendKeys(comments, "Selenium!")
.sendKeys(Keys.ARROW_LEFT)
.keyDown(Keys.SHIFT)
.sendKeys(Keys.ARROW_UP)
.keyUp(Keys.SHIFT)
.keyDown(cmdCtrl)
.sendKeys("xvv")
.keyUp(cmdCtrl)
.perform();
assertThat(comments.getAttribute("value")).isEqualTo("SeleniumSelenium!");
WebElement username = driver.findElement(By.name("username"));
actions
.keyDown(cmdCtrl)
.sendKeys("ac")
.keyUp(cmdCtrl)
.keyDown(Keys.DELETE)
.keyUp(Keys.DELETE)
.keyDown(username, cmdCtrl)
.sendKeys("v")
.keyUp(cmdCtrl)
.perform();
assertThat(username.getAttribute("value")).isEqualTo("SeleniumSelenium!");
}
@Test
public void sendKeysTest(){
driver.get("https://google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Playwright.", "dev", Keys.ENTER);
}
private Object[][] dataProvider() {
return new Object[][]{
{"left", "Left", 30},
{"middle", "Middle", 40},
{"right", "Right", 50}
};
}
private String getWebElementTextById(String id, WebDriver driver) {
return driver.findElement(By.id(id)).getText();
}
private List<String> getWebElementsTextsBy(String idMask, WebDriver driver) {
return driver.findElements(By.xpath("//*[contains(@id, '%s')]".formatted(idMask))).stream().map(WebElement::getText).toList();
}
private int randomIntExcept(int start, int finish, int except) {
Faker faker = new Faker();
int result = faker.number().numberBetween(start, finish);
if (result == except) {
randomIntExcept(start, finish, except);
}
return result;
}
}