Skip to content

Commit 17c8985

Browse files
authored
feat: Allow taking of screenshots of Page Objects or elements (#236)
1 parent 2847390 commit 17c8985

File tree

9 files changed

+56
-3
lines changed

9 files changed

+56
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<groupId>com.salesforce.utam</groupId>
1313
<artifactId>utam-java</artifactId>
1414
<packaging>pom</packaging>
15-
<version>4.0.2-SNAPSHOT</version>
15+
<version>4.0.3-SNAPSHOT</version>
1616
<name>${project.groupId}:${project.artifactId}</name>
1717
<description>Java project for generating and using UTAM Page Objects</description>
1818
<url>http://github.com/salesforce/utam-java</url>

utam-compiler/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<artifactId>utam-java</artifactId>
1212
<groupId>com.salesforce.utam</groupId>
13-
<version>4.0.2-SNAPSHOT</version>
13+
<version>4.0.3-SNAPSHOT</version>
1414
</parent>
1515
<modelVersion>4.0.0</modelVersion>
1616

utam-core/pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<artifactId>utam-java</artifactId>
1212
<groupId>com.salesforce.utam</groupId>
13-
<version>4.0.2-SNAPSHOT</version>
13+
<version>4.0.3-SNAPSHOT</version>
1414
</parent>
1515
<modelVersion>4.0.0</modelVersion>
1616

@@ -27,6 +27,20 @@
2727
<dependency>
2828
<groupId>io.appium</groupId>
2929
<artifactId>java-client</artifactId>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>org.seleniumhq.selenium</groupId>
33+
<artifactId>selenium-api</artifactId>
34+
</exclusion>
35+
<exclusion>
36+
<groupId>org.seleniumhq.selenium</groupId>
37+
<artifactId>selenium-remote-driver</artifactId>
38+
</exclusion>
39+
<exclusion>
40+
<groupId>org.seleniumhq.selenium</groupId>
41+
<artifactId>selenium-support</artifactId>
42+
</exclusion>
43+
</exclusions>
3044
</dependency>
3145
<dependency>
3246
<groupId>org.slf4j</groupId>

utam-core/src/main/java/utam/core/element/Element.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ public interface Element {
160160
*/
161161
void dragAndDrop(DragAndDropOptions options);
162162

163+
/**
164+
* Gets a screenshot of the element represented by this object.
165+
*
166+
* @return a byte array containing a PNG image of the element
167+
*/
168+
byte[] getScreenshot();
169+
163170
/**
164171
* Get element coordinates and size. Can be used to calculate drop location inside a frame.
165172
*

utam-core/src/main/java/utam/core/framework/base/ContainerElementPageObject.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public boolean isPresent() {
6363
return false;
6464
}
6565

66+
@Override
67+
public byte[] getScreenshot() {
68+
throw new UtamCoreError(ERR_UNSUPPORTED_METHOD);
69+
}
70+
6671
@Override
6772
public void waitForAbsence() {
6873
throw new UtamCoreError(ERR_UNSUPPORTED_METHOD);

utam-core/src/main/java/utam/core/framework/base/UtamBase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public interface UtamBase {
5656
*/
5757
boolean isVisible();
5858

59+
/**
60+
* Gets a screenshot of the element represented by this object.
61+
*
62+
* @return the screenshot in PNG format as a byte array
63+
*/
64+
byte[] getScreenshot();
65+
5966
/**
6067
* check if current element contains another element with the given selector
6168
*

utam-core/src/main/java/utam/core/framework/base/UtamBaseImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public final boolean containsElement(Locator locator, boolean isExpandShadow) {
121121
return transformed.containsElements(locator) > 0;
122122
}
123123

124+
@Override
125+
public byte[] getScreenshot() {
126+
return getElement().getScreenshot();
127+
}
128+
124129
@Override
125130
public final boolean containsElement(Locator locator) {
126131
return containsElement(locator, false);

utam-core/src/main/java/utam/core/selenium/element/ElementAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.stream.Collectors;
1717
import org.openqa.selenium.By;
1818
import org.openqa.selenium.NoSuchElementException;
19+
import org.openqa.selenium.OutputType;
1920
import org.openqa.selenium.Rectangle;
2021
import org.openqa.selenium.StaleElementReferenceException;
2122
import org.openqa.selenium.WebDriver;
@@ -183,6 +184,11 @@ public void deprecatedClick() {
183184
driverAdapter.executeScript(CLICK_VIA_JAVASCRIPT, getWebElement());
184185
}
185186

187+
@Override
188+
public byte[] getScreenshot() {
189+
return getWebElement().getScreenshotAs(OutputType.BYTES);
190+
}
191+
186192
@Override
187193
public void scrollIntoView(ScrollOptions options) {
188194
if (options == ScrollOptions.TOP) {

utam-core/src/test/java/utam/core/framework/element/BasePageElementTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.hamcrest.CoreMatchers.equalTo;
1111
import static org.hamcrest.CoreMatchers.is;
1212
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.notNullValue;
1314
import static org.mockito.ArgumentMatchers.contains;
1415
import static org.mockito.ArgumentMatchers.refEq;
1516
import static org.mockito.Mockito.doThrow;
@@ -24,6 +25,7 @@
2425
import org.openqa.selenium.JavascriptException;
2526
import org.openqa.selenium.JavascriptExecutor;
2627
import org.openqa.selenium.Keys;
28+
import org.openqa.selenium.OutputType;
2729
import org.openqa.selenium.Rectangle;
2830
import org.openqa.selenium.StaleElementReferenceException;
2931
import org.openqa.selenium.TimeoutException;
@@ -312,4 +314,11 @@ public void testGetRectangle() {
312314
assertThat(test.getWidth(), equalTo(4));
313315
assertThat(test.getHeight(), equalTo(3));
314316
}
317+
318+
@Test
319+
public void testGetScreenshot() {
320+
MockUtilities mock = new MockUtilities();
321+
when(mock.getWebElementMock().getScreenshotAs(OutputType.BYTES)).thenReturn(new byte[0]);
322+
assertThat(mock.getUtamElement().getScreenshot(), is(notNullValue()));
323+
}
315324
}

0 commit comments

Comments
 (0)