Skip to content

Commit 356896e

Browse files
committed
Rework tests and add networkSpeedEmulationTest
1 parent 47b9825 commit 356896e

File tree

15 files changed

+332
-89
lines changed

15 files changed

+332
-89
lines changed

src/main/java/aquality/selenium/browser/devtools/NetworkHandling.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,32 @@ public NetworkInterceptor addResponseHandler(Predicate<HttpResponse> responseMat
313313
public void clearNetworkInterceptor() {
314314
resetNetworkFilter();
315315
}
316+
317+
/**
318+
* Activates emulation of network conditions.
319+
* @param offline True to emulate internet disconnection.
320+
* @param latency Minimum latency from request sent to response headers received (ms).
321+
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
322+
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
323+
*/
324+
public void emulateConditions(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput) {
325+
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty()));
326+
tools.sendCommand(emulateNetworkConditions(offline, latency, downloadThroughput, uploadThroughput, Optional.empty()));
327+
}
328+
329+
/**
330+
* Activates emulation of network conditions.
331+
* @param offline True to emulate internet disconnection.
332+
* @param latency Minimum latency from request sent to response headers received (ms).
333+
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
334+
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
335+
* @param connectionType Connection type if known.
336+
* Possible values: "none", "cellular2g", "cellular3g", "cellular4g", "bluetooth", "ethernet",
337+
* "wifi", "wimax", "other".
338+
*/
339+
public void emulateConditions(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput, String connectionType) {
340+
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty()));
341+
tools.sendCommand(emulateNetworkConditions(offline, latency, downloadThroughput, uploadThroughput,
342+
Optional.of(ConnectionType.fromString(connectionType))));
343+
}
316344
}

src/main/java/aquality/selenium/elements/actions/JsActions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ public boolean isElementOnScreen() {
140140
*/
141141
public String getElementText() {
142142
logElementAction("loc.get.text.js");
143-
return (String) executeScript(JavaScript.GET_ELEMENT_TEXT);
143+
String value = (String) executeScript(JavaScript.GET_ELEMENT_TEXT);
144+
logElementAction("loc.text.value", value);
145+
return value;
144146
}
145147

146148
/**

src/main/java/aquality/selenium/elements/actions/MouseActions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package aquality.selenium.elements.actions;
22

33
import aquality.selenium.browser.AqualityServices;
4-
import aquality.selenium.browser.Browser;
54
import aquality.selenium.core.utilities.IElementActionRetrier;
65
import aquality.selenium.elements.interfaces.IElement;
76
import org.openqa.selenium.interactions.Actions;
@@ -51,8 +50,10 @@ public void moveMouseToElement() {
5150
*/
5251
public void moveMouseFromElement() {
5352
infoLoc("loc.movingFrom");
54-
performAction(actions -> actions.moveToElement(element.getElement(),
55-
-element.getElement().getSize().width / 2, -element.getElement().getSize().height / 2));
53+
AqualityServices.get(IElementActionRetrier.class).doWithRetry(() ->
54+
new Actions(getBrowser().getDriver())
55+
.moveToElement(element.getElement(), -element.getElement().getSize().width, -element.getElement().getSize().height)
56+
.build().perform());
5657
}
5758

5859
/**

src/test/java/tests/BaseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ protected Browser getBrowser() {
4444
}
4545

4646
@SuppressWarnings("unchecked")
47-
protected <T> T getScriptResultOrDefault(String scriptName, T defaultValue) {
47+
protected <T> T getScriptResultOrDefault(String scriptName, T defaultValue, Object... arguments) {
4848
try {
49-
return ((T) getBrowser().executeScript(getResourceFileByName(scriptName)));
49+
return ((T) getBrowser().executeScript(getResourceFileByName(scriptName), arguments));
5050
} catch (IOException e) {
5151
return defaultValue;
5252
}
Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,111 @@
11
package tests.integration;
22

33
import aquality.selenium.browser.AqualityServices;
4-
import aquality.selenium.browser.JavaScript;
5-
import aquality.selenium.elements.actions.JsActions;
6-
import aquality.selenium.elements.interfaces.IButton;
4+
import aquality.selenium.elements.interfaces.ILabel;
5+
import aquality.selenium.elements.interfaces.ILink;
76
import aquality.selenium.elements.interfaces.ITextBox;
8-
import automationpractice.forms.ProductForm;
9-
import automationpractice.forms.ProductListForm;
107
import org.openqa.selenium.Keys;
118
import org.testng.Assert;
129
import org.testng.annotations.BeforeMethod;
1310
import org.testng.annotations.Test;
1411
import tests.BaseTest;
15-
import utils.AutomationPracticeUtils;
12+
import theinternet.forms.FormAuthenticationForm;
13+
import theinternet.forms.InfiniteScrollForm;
14+
import theinternet.forms.JQueryMenuForm;
15+
import theinternet.forms.WelcomeForm;
1616

17-
import static automationpractice.Constants.URL_AUTOMATIONPRACTICE;
17+
import java.util.concurrent.TimeoutException;
18+
import java.util.concurrent.atomic.AtomicReference;
1819

1920
public class ActionTests extends BaseTest {
2021

2122
@BeforeMethod
2223
@Override
2324
protected void beforeMethod() {
2425
AqualityServices.getBrowser().getDriver().manage().window().maximize();
25-
if (!AqualityServices.getBrowser().getCurrentUrl().equals(URL_AUTOMATIONPRACTICE)) {
26-
AutomationPracticeUtils.openAutomationPracticeSite();
27-
}
2826
}
2927

3028
@Test
3129
public void testScrollToTheCenter() {
32-
JsActions jsActions = new ProductListForm().getLblLastProduct().getJsActions();
33-
jsActions.scrollToTheCenter();
34-
Assert.assertTrue(jsActions.isElementOnScreen(), "element is not on the screen after scrollToTheCenter()");
30+
final int accuracy = 1;
31+
WelcomeForm welcomeForm = new WelcomeForm();
32+
getBrowser().goTo(welcomeForm.getUrl());
33+
ILink link = welcomeForm.getExampleLink(WelcomeForm.AvailableExample.HOVERS);
34+
link.getJsActions().scrollToTheCenter();
35+
36+
Long windowHeight = getScriptResultOrDefault("getWindowSize.js", 10L);
37+
double currentY = getScriptResultOrDefault("getElementYCoordinate.js", 0.0, link.getElement());
38+
double coordinateRelatingWindowCenter = windowHeight.doubleValue() / 2 - currentY;
39+
Assert.assertTrue(Math.abs(coordinateRelatingWindowCenter) <= accuracy,
40+
"Upper bound of element should be in the center of the page");
3541
}
3642

3743
@Test
38-
public void testScrollIntoView() {
39-
AqualityServices.getBrowser().executeScript(JavaScript.SCROLL_TO_BOTTOM);
40-
JsActions jsActions = new ProductListForm().getLblLastProduct().getJsActions();
41-
jsActions.scrollIntoView();
42-
Assert.assertTrue(jsActions.isElementOnScreen(), "element is not on the screen after scrollIntoView()");
44+
public void testScrollIntoView() throws TimeoutException {
45+
InfiniteScrollForm infiniteScrollForm = new InfiniteScrollForm();
46+
getBrowser().goTo(infiniteScrollForm.getUrl());
47+
infiniteScrollForm.waitForMoreExamples();
48+
int defaultCount = infiniteScrollForm.getExampleLabels().size();
49+
AtomicReference<ILabel> lastExampleLabel = new AtomicReference<>(infiniteScrollForm.getLastExampleLabel());
50+
AqualityServices.getConditionalWait().waitForTrue(() -> {
51+
lastExampleLabel.set(infiniteScrollForm.getLastExampleLabel());
52+
lastExampleLabel.get().getJsActions().scrollIntoView();
53+
return infiniteScrollForm.getExampleLabels().size() > defaultCount;
54+
}, "Some examples should be added after scroll");
55+
Assert.assertTrue(lastExampleLabel.get().getJsActions().isElementOnScreen(),
56+
"Element should be on screen after scroll into view");
4357
}
4458

4559
@Test
4660
public void testMoveMouseToElement() {
47-
IButton button = new ProductListForm().getBtnLastProductMoreFocused();
48-
Assert.assertTrue(button.getText().contains("More"), "element is not focused after moveMouseToElement()");
61+
JQueryMenuForm menuForm = new JQueryMenuForm();
62+
getBrowser().goTo(menuForm.getUrl());
63+
menuForm.getEnabledButton().getMouseActions().moveMouseToElement();
64+
Assert.assertTrue(menuForm.isEnabledButtonFocused(), "element is not focused after moveMouseToElement()");
4965
}
5066

5167
@Test
5268
public void testMoveMouseFromElement() {
53-
ProductListForm productListForm = new ProductListForm();
54-
55-
Assert.assertTrue(AqualityServices.getConditionalWait().waitFor(() -> {
56-
IButton button = productListForm.getBtnLastProductMoreFocused();
57-
return button.getText().contains("More");
58-
}, "element is not focused after moveMouseToElement()"));
59-
60-
IButton button = productListForm.getBtnLastProductMoreFocused();
61-
productListForm.getLblLastProduct().getMouseActions().moveMouseFromElement();
62-
Assert.assertFalse(button.state().isDisplayed(), "element is still focused after moveMouseFromElement()");
69+
JQueryMenuForm menuForm = new JQueryMenuForm();
70+
testMoveMouseToElement();
71+
menuForm.getEnabledButton().getMouseActions().moveMouseFromElement();
72+
boolean isUnfocused = AqualityServices.getConditionalWait().waitFor(() -> !menuForm.isEnabledButtonFocused());
73+
Assert.assertTrue(isUnfocused, "element is still focused after moveMouseFromElement()");
6374
}
6475

6576
@Test
6677
public void testGetElementText() {
67-
IButton button = new ProductListForm().getBtnLastProductMoreFocused();
68-
Assert.assertEquals(button.getText().trim(), button.getJsActions().getElementText().trim(),
78+
WelcomeForm welcomeForm = new WelcomeForm();
79+
getBrowser().goTo(welcomeForm.getUrl());
80+
ILink link = welcomeForm.getExampleLink(WelcomeForm.AvailableExample.HOVERS);
81+
Assert.assertEquals(link.getText().trim(), link.getJsActions().getElementText().trim(),
6982
"element text got via JsActions is not match to expected");
7083
}
7184

7285
@Test
7386
public void testSetFocus() {
74-
Runnable testSetFocus = () -> {
75-
new ProductListForm().getBtnLastProductMoreFocused().getJsActions().clickAndWait();
87+
FormAuthenticationForm form = new FormAuthenticationForm();
88+
getBrowser().goTo(form.getUrl());
89+
ITextBox textBox = form.getTxbUsername();
90+
ITextBox secondTextBox = form.getTxbPassword();
91+
textBox.clearAndType("[email protected]");
92+
secondTextBox.getJsActions().setFocus();
7693

77-
ITextBox txbQuantity = new ProductForm().getTxbQuantity();
78-
txbQuantity.getJsActions().setFocus();
79-
txbQuantity.sendKeys(Keys.DELETE);
80-
txbQuantity.sendKeys(Keys.BACK_SPACE);
81-
Assert.assertEquals(txbQuantity.getValue(), "",
82-
"value is not empty after sending Delete keys, probably setFocus() didn't worked");
83-
};
84-
AutomationPracticeUtils.doOnAutomationPracticeWithRetry(testSetFocus);
94+
String currentText = textBox.getValue();
95+
String expectedText = currentText.substring(0, currentText.length() - 1);
96+
textBox.getJsActions().setFocus();
97+
textBox.sendKeys(Keys.DELETE);
98+
textBox.sendKeys(Keys.BACK_SPACE);
99+
Assert.assertEquals(textBox.getValue(), expectedText, "One character should be removed from " + expectedText);
85100
}
86101

87102
@Test
88103
public void testSetValue() {
89-
Runnable testSetValue = () -> {
90-
new ProductListForm().getBtnLastProductMoreFocused().getJsActions().clickAndWait();
91-
92-
ProductForm productForm = new ProductForm();
93-
ITextBox txbQuantity = productForm.getTxbQuantity();
94-
txbQuantity.getJsActions().setValue("2");
95-
productForm.getBtnPlus().click();
96-
Assert.assertEquals(txbQuantity.getValue(), "3",
97-
"value of textbox is not correct, probably setValue() didn't worked");
98-
};
99-
AutomationPracticeUtils.doOnAutomationPracticeWithRetry(testSetValue);
104+
final String expectedValue = "2";
105+
FormAuthenticationForm form = new FormAuthenticationForm();
106+
getBrowser().goTo(form.getUrl());
107+
ITextBox textBox = form.getTxbUsername();
108+
textBox.getJsActions().setValue(expectedValue);
109+
Assert.assertEquals(textBox.getValue(), expectedValue, "Text is not set to value");
100110
}
101111
}

src/test/java/tests/integration/BrowserTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import aquality.selenium.browser.JavaScript;
66
import aquality.selenium.core.utilities.ISettingsFile;
77
import aquality.selenium.core.utilities.JsonSettingsFile;
8-
import automationpractice.forms.SliderForm;
98
import org.openqa.selenium.*;
109
import org.openqa.selenium.logging.LogType;
1110
import org.testng.Assert;
@@ -14,8 +13,8 @@
1413
import theinternet.TheInternetPage;
1514
import theinternet.forms.DynamicContentForm;
1615
import theinternet.forms.FormAuthenticationForm;
16+
import theinternet.forms.WelcomeForm;
1717
import utils.DurationSample;
18-
import utils.AutomationPracticeUtils;
1918
import utils.Timer;
2019

2120
import java.io.IOException;
@@ -182,12 +181,12 @@ public void testShouldBePossibleToSetWindowSize() {
182181

183182
@Test
184183
public void testShouldBePossibleToScrollWindowBy(){
185-
AutomationPracticeUtils.openAutomationPracticeSite();
186-
SliderForm sliderForm = new SliderForm();
187-
int initialY = sliderForm.getFormPointInViewPort().getY();
188-
int formHeight = sliderForm.getSize().getHeight();
184+
WelcomeForm scrollForm = new WelcomeForm();
185+
getBrowser().goTo(scrollForm.getUrl());
186+
int initialY = scrollForm.getFormPointInViewPort().getY();
187+
int formHeight = scrollForm.getSize().getHeight();
189188
getBrowser().scrollWindowBy(0, formHeight);
190-
Assert.assertEquals(initialY - sliderForm.getFormPointInViewPort().getY(), formHeight);
189+
Assert.assertEquals(initialY - scrollForm.getFormPointInViewPort().getY(), formHeight);
191190
}
192191

193192
@Test

src/test/java/tests/integration/HiddenElementsTests.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import aquality.selenium.core.elements.ElementState;
44
import aquality.selenium.core.elements.ElementsCount;
55
import aquality.selenium.elements.interfaces.ILabel;
6-
import automationpractice.forms.ProductTabContentForm;
76
import automationpractice.forms.SliderForm;
87
import org.testng.Assert;
98
import org.testng.annotations.BeforeMethod;
109
import org.testng.annotations.DataProvider;
1110
import org.testng.annotations.Test;
1211
import tests.BaseTest;
13-
import utils.AutomationPracticeUtils;
12+
import theinternet.forms.HoversForm;
1413

1514
import java.util.ArrayList;
1615
import java.util.Collections;
@@ -19,34 +18,31 @@
1918

2019
public class HiddenElementsTests extends BaseTest {
2120

22-
private static final ProductTabContentForm productsForm = new ProductTabContentForm();
21+
private final HoversForm hoversForm = new HoversForm();
2322

2423
@BeforeMethod
2524
@Override
2625
protected void beforeMethod() {
27-
AutomationPracticeUtils.openAutomationPracticeSite();
26+
getBrowser().goTo(hoversForm.getUrl());
2827
}
2928

30-
3129
@DataProvider
3230
private Object[] getHiddenElementListProviders() {
3331
List<Function<ElementsCount, List<ILabel>>> providers = new ArrayList<>();
3432
ElementState state = ElementState.EXISTS_IN_ANY_STATE;
35-
providers.add(count -> productsForm.getListElements(state, count));
36-
providers.add(count -> productsForm.getListElementsById(state, count));
37-
providers.add(count -> productsForm.getListElementsByName(state, count));
38-
providers.add(count -> productsForm.getListElementsByIdOrName(state, count));
39-
providers.add(count -> productsForm.getListElementsByClassName(state, count));
40-
providers.add(count -> productsForm.getListElementsByCss(state, count));
41-
providers.add(count -> productsForm.getListElementsByDottedXPath(state, count));
42-
providers.add(count -> productsForm.getChildElementsByDottedXPath(state, count));
43-
providers.add(count -> Collections.singletonList(productsForm.getChildElementByNonXPath(state)));
33+
providers.add(count -> hoversForm.getListElements(state, count));
34+
providers.add(count -> hoversForm.getListElementsByName(state, count));
35+
providers.add(count -> hoversForm.getListElementsByClassName(state, count));
36+
providers.add(count -> hoversForm.getListElementsByCss(state, count));
37+
providers.add(count -> hoversForm.getListElementsByDottedXpath(state, count));
38+
providers.add(count -> hoversForm.getChildElementsByDottedXpath(state, count));
39+
providers.add(count -> Collections.singletonList(hoversForm.getChildElementByNonXpath(state)));
4440
return providers.toArray();
4541
}
4642

4743
@Test
4844
public void testHiddenElementExist() {
49-
Assert.assertTrue(new SliderForm().getBtnAddToCart(ElementState.EXISTS_IN_ANY_STATE).state().isExist());
45+
Assert.assertTrue(hoversForm.getHiddenElement(HoversForm.HoverExample.THIRD, ElementState.EXISTS_IN_ANY_STATE).state().isExist());
5046
}
5147

5248
@Test(dataProvider = "getHiddenElementListProviders")

src/test/java/tests/usecases/ElementExistNotDisplayedTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22

33
import aquality.selenium.browser.AqualityServices;
44
import aquality.selenium.core.elements.ElementState;
5-
import aquality.selenium.elements.interfaces.IButton;
6-
import automationpractice.forms.SliderForm;
5+
import aquality.selenium.elements.interfaces.ILabel;
76
import org.testng.Assert;
87
import org.testng.annotations.BeforeMethod;
98
import org.testng.annotations.Test;
109
import tests.BaseTest;
11-
import utils.AutomationPracticeUtils;
10+
import theinternet.forms.HoversForm;
1211

1312
public class ElementExistNotDisplayedTest extends BaseTest {
13+
private final HoversForm hoversForm = new HoversForm();
1414

1515
@BeforeMethod
1616
@Override
1717
public void beforeMethod() {
18-
AutomationPracticeUtils.openAutomationPracticeSite();
18+
getBrowser().goTo(hoversForm.getUrl());
1919
}
2020

2121
@Test
2222
public void testElementExistNotDisplayed() {
23-
IButton button = new SliderForm().getBtnAddToCart(ElementState.EXISTS_IN_ANY_STATE);
24-
Assert.assertTrue(AqualityServices.getConditionalWait().waitFor(() -> button.state().isExist() && !button.state().isDisplayed(), "Button should exists in the DOM but should not be displayed "));
23+
ILabel label = hoversForm.getHiddenElement(HoversForm.HoverExample.FIRST, ElementState.EXISTS_IN_ANY_STATE);
24+
Assert.assertTrue(AqualityServices.getConditionalWait().waitFor(() -> label.state().isExist() && !label.state().isDisplayed(),
25+
"Label should exists in the DOM but should not be displayed"));
2526
}
2627
}

src/test/java/tests/usecases/ShoppingCartTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import automationpractice.modals.ProceedToCheckoutModal;
77
import org.testng.Assert;
88
import org.testng.annotations.BeforeMethod;
9-
import org.testng.annotations.Test;
109
import org.testng.asserts.SoftAssert;
1110
import tests.BaseTest;
1211
import utils.AutomationPracticeUtils;
@@ -29,7 +28,9 @@ protected void beforeMethod() {
2928
}
3029
}
3130

32-
@Test
31+
/*@Test
32+
test skipped because automationpractice.com site is down
33+
*/
3334
public void testShoppingCart() {
3435
Runnable testShoppingCart = () -> {
3536
SoftAssert softAssert = new SoftAssert();

0 commit comments

Comments
 (0)