-
Notifications
You must be signed in to change notification settings - Fork 34
/
JavaScriptSnippetsTests.java
57 lines (49 loc) · 2.66 KB
/
JavaScriptSnippetsTests.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
package tests.usecases.devtools;
import aquality.selenium.browser.AqualityServices;
import aquality.selenium.browser.JavaScript;
import aquality.selenium.browser.devtools.JavaScriptHandling;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.JavascriptException;
import org.openqa.selenium.ScriptKey;
import org.testng.Assert;
import org.testng.annotations.Test;
import tests.BaseTest;
import theinternet.forms.FormAuthenticationForm;
import theinternet.forms.WelcomeForm;
public class JavaScriptSnippetsTests extends BaseTest {
private static JavaScriptHandling javaScriptEngine() {
return AqualityServices.getBrowser().javaScriptEngine();
}
@Test
public void testPinScriptAndUnpinIt() {
String script = JavaScript.GET_ELEMENT_XPATH.getScript();
WelcomeForm welcomeForm = new WelcomeForm();
ScriptKey pinnedScript = javaScriptEngine().pinScript(script);
Assert.assertTrue(javaScriptEngine().getPinnedScripts().contains(pinnedScript), "Should be possible to read pinned scripts");
getBrowser().goTo(welcomeForm.getUrl());
String xpath = (String) welcomeForm.getSubTitleLabel().getJsActions().executeScript(pinnedScript);
Assert.assertTrue(StringUtils.isNotEmpty(xpath), "Pinned script should be possible to execute");
String expectedValue = welcomeForm.getSubTitleLabel().getJsActions().getXPath();
Assert.assertEquals(xpath, expectedValue, "Pinned script should return the same value");
javaScriptEngine().unpinScript(pinnedScript);
Assert.assertThrows(JavascriptException.class,
() -> welcomeForm.getSubTitleLabel().getJsActions().executeScript(pinnedScript));
javaScriptEngine().reset();
}
@Test
public void testPinScriptWithoutReturnedValueAndUnpinAll() {
String valueToSet = "username";
String script = JavaScript.SET_VALUE.getScript();
ScriptKey pinnedScript = javaScriptEngine().pinScript(script);
FormAuthenticationForm authenticationForm = new FormAuthenticationForm();
getBrowser().goTo(authenticationForm.getUrl());
getBrowser().waitForPageToLoad();
authenticationForm.getTxbUsername().getJsActions().executeScript(pinnedScript, valueToSet);
Assert.assertEquals(authenticationForm.getTxbUsername().getValue(), valueToSet,
String.format("Text should be '%s' after setting value via pinned JS", valueToSet));
javaScriptEngine().clearPinnedScripts();
Assert.assertThrows(JavascriptException.class,
() -> authenticationForm.getTxbUsername().getJsActions().executeScript(pinnedScript));
javaScriptEngine().reset();
}
}