|
1 | 1 | package tests.integration; |
2 | 2 |
|
3 | 3 | 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; |
7 | 6 | import aquality.selenium.elements.interfaces.ITextBox; |
8 | | -import automationpractice.forms.ProductForm; |
9 | | -import automationpractice.forms.ProductListForm; |
10 | 7 | import org.openqa.selenium.Keys; |
11 | 8 | import org.testng.Assert; |
12 | 9 | import org.testng.annotations.BeforeMethod; |
13 | 10 | import org.testng.annotations.Test; |
14 | 11 | 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; |
16 | 16 |
|
17 | | -import static automationpractice.Constants.URL_AUTOMATIONPRACTICE; |
| 17 | +import java.util.concurrent.TimeoutException; |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
18 | 19 |
|
19 | 20 | public class ActionTests extends BaseTest { |
20 | 21 |
|
21 | 22 | @BeforeMethod |
22 | 23 | @Override |
23 | 24 | protected void beforeMethod() { |
24 | 25 | AqualityServices.getBrowser().getDriver().manage().window().maximize(); |
25 | | - if (!AqualityServices.getBrowser().getCurrentUrl().equals(URL_AUTOMATIONPRACTICE)) { |
26 | | - AutomationPracticeUtils.openAutomationPracticeSite(); |
27 | | - } |
28 | 26 | } |
29 | 27 |
|
30 | 28 | @Test |
31 | 29 | 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"); |
35 | 41 | } |
36 | 42 |
|
37 | 43 | @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"); |
43 | 57 | } |
44 | 58 |
|
45 | 59 | @Test |
46 | 60 | 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()"); |
49 | 65 | } |
50 | 66 |
|
51 | 67 | @Test |
52 | 68 | 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()"); |
63 | 74 | } |
64 | 75 |
|
65 | 76 | @Test |
66 | 77 | 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(), |
69 | 82 | "element text got via JsActions is not match to expected"); |
70 | 83 | } |
71 | 84 |
|
72 | 85 | @Test |
73 | 86 | 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("peter.parker@example.com"); |
| 92 | + secondTextBox.getJsActions().setFocus(); |
76 | 93 |
|
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); |
85 | 100 | } |
86 | 101 |
|
87 | 102 | @Test |
88 | 103 | 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"); |
100 | 110 | } |
101 | 111 | } |
0 commit comments