Skip to content

Commit bdbddef

Browse files
committed
- move js to separate file
- simplify getScreenshot method, remove debug logging - add locator test
1 parent 4e82235 commit bdbddef

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

src/main/java/aquality/selenium/browser/JavaScript.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public enum JavaScript {
2222
GET_COMBOBOX_SELECTED_TEXT("getCmbText.js"),
2323
GET_COMBOBOX_TEXTS("getCmbValues.js"),
2424
GET_ELEMENT_BY_XPATH("getElementByXpath.js"),
25+
GET_ELEMENTS_FROM_POINT("getElementsFromPoint.js"),
2526
GET_ELEMENT_XPATH("getElementXPath.js"),
2627
GET_ELEMENT_TEXT("getElementText.js"),
2728
GET_TEXT_FIRST_CHILD("getTextFirstChild.js"),

src/main/java/aquality/selenium/elements/interfaces/ByImage.java

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

33
import aquality.selenium.browser.AqualityServices;
4+
import aquality.selenium.browser.JavaScript;
45
import org.opencv.core.Point;
56
import org.opencv.core.*;
67
import org.opencv.imgcodecs.Imgcodecs;
@@ -97,7 +98,8 @@ protected WebElement getElementOnPoint(Point matchLocation, SearchContext contex
9798
int centerX = (int) (matchLocation.x + (template.width() / 2));
9899
int centerY = (int) (matchLocation.y + (template.height() / 2));
99100
//noinspection unchecked
100-
List<WebElement> elements = (List<WebElement>) AqualityServices.getBrowser().executeScript("return document.elementsFromPoint(arguments[0], arguments[1]);", centerX, centerY);
101+
List<WebElement> elements = (List<WebElement>) AqualityServices.getBrowser()
102+
.executeScript(JavaScript.GET_ELEMENTS_FROM_POINT, centerX, centerY);
101103
elements.sort(Comparator.comparingDouble(e -> distanceToPoint(matchLocation, e)));
102104
return elements.get(0);
103105
}
@@ -121,16 +123,8 @@ protected static double distanceToPoint(Point matchLocation, WebElement element)
121123
* @return captured screenshot as byte array.
122124
*/
123125
protected byte[] getScreenshot(SearchContext context) {
124-
byte[] screenshotBytes;
125-
126-
if (!(context instanceof TakesScreenshot)) {
127-
AqualityServices.getLogger().debug("Current search context doesn't support taking screenshots. " +
128-
"Will take browser screenshot instead");
129-
screenshotBytes = AqualityServices.getBrowser().getScreenshot();
130-
} else {
131-
screenshotBytes = ((TakesScreenshot) context).getScreenshotAs(OutputType.BYTES);
132-
}
133-
134-
return screenshotBytes;
126+
return !(context instanceof TakesScreenshot)
127+
? AqualityServices.getBrowser().getScreenshot()
128+
: ((TakesScreenshot) context).getScreenshotAs(OutputType.BYTES);
135129
}
136130
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return document.elementsFromPoint(arguments[0], arguments[1]);

src/test/java/tests/integration/LocatorTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package tests.integration;
22

3+
import aquality.selenium.browser.AqualityServices;
4+
import aquality.selenium.elements.interfaces.ByImage;
35
import aquality.selenium.elements.interfaces.ILabel;
46
import automationpractice.forms.ChallengingDomForm;
57
import org.openqa.selenium.By;
68
import org.openqa.selenium.WebElement;
79
import org.openqa.selenium.support.locators.RelativeLocator;
10+
import org.testng.Assert;
811
import org.testng.annotations.BeforeMethod;
912
import org.testng.annotations.Test;
1013
import org.testng.asserts.SoftAssert;
1114
import tests.BaseTest;
1215
import theinternet.TheInternetPage;
16+
import theinternet.forms.BrokenImagesForm;
17+
1318
import java.util.List;
19+
1420
import static aquality.selenium.locators.RelativeBySupplier.with;
1521

1622
public class LocatorTests extends BaseTest {
@@ -26,6 +32,22 @@ public void beforeMethod() {
2632
navigate(TheInternetPage.CHALLENGING_DOM);
2733
}
2834

35+
@Test
36+
public void testByImageLocator() {
37+
BrokenImagesForm form = new BrokenImagesForm();
38+
Assert.assertFalse(form.getLabelByImage().state().isDisplayed(), "Should be impossible to find element on page by image when it is absent");
39+
getBrowser().goTo(form.getUrl());
40+
Assert.assertTrue(form.getLabelByImage().state().isDisplayed(), "Should be possible to find element on page by image");
41+
42+
List<ILabel> childLabels = form.getChildLabelsByImage();
43+
List<ILabel> docLabels = form.getLabelsByImage();
44+
Assert.assertTrue(docLabels.size() > 1, "List of elements should be possible to find by image");
45+
Assert.assertEquals(docLabels.size(), childLabels.size(), "Should be possible to find child elements by image with the same count");
46+
47+
ILabel screen = AqualityServices.getElementFactory().getLabel(new ByImage(AqualityServices.getBrowser().getScreenshot()), "full screen");
48+
Assert.assertTrue(screen.state().waitForDisplayed(), "Should be possible to find element by full page screenshot");
49+
}
50+
2951
@Test
3052
public void testAboveLocatorWithDifferentAboveParametersType() {
3153
ILabel cellInRow5Column5 = challengingDomForm.getCellInRow5Column5();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package theinternet.forms;
2+
3+
import aquality.selenium.elements.interfaces.ByImage;
4+
import aquality.selenium.elements.interfaces.ILabel;
5+
import org.openqa.selenium.By;
6+
import utils.FileUtil;
7+
8+
import java.util.List;
9+
10+
public class BrokenImagesForm extends TheInternetForm {
11+
private final By imageLocator = new ByImage(FileUtil.getResourceFileByName("brokenImage.png"));
12+
13+
public BrokenImagesForm(){
14+
super(By.id("content"), "Broken Images form");
15+
}
16+
17+
public ILabel getLabelByImage(){
18+
return getElementFactory().getLabel(imageLocator, "broken image");
19+
}
20+
21+
public List<ILabel> getLabelsByImage(){
22+
return getElementFactory().findElements(imageLocator, "broken image", ILabel.class);
23+
}
24+
25+
public List<ILabel> getChildLabelsByImage(){
26+
return getFormLabel().findChildElements(imageLocator, "broken image", ILabel.class);
27+
}
28+
29+
@Override
30+
protected String getUri() {
31+
return "/broken_images";
32+
}
33+
}

src/test/resources/brokenImage.png

765 Bytes
Loading

0 commit comments

Comments
 (0)