Skip to content

Commit c02d145

Browse files
Merge pull request #19 from roy-de-kleijn/percy
Percy
2 parents 14e8ff5 + 56e5774 commit c02d145

6 files changed

Lines changed: 194 additions & 0 deletions

File tree

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,75 @@ In this section, we will run the test cases to test the internally hosted websit
497497
gradle allureServe
498498
```
499499
500+
# Percy
501+
502+
[Percy](https://percy.io) provides a visual review platform.
503+
504+
## Prerequisites
505+
506+
- Sign up with your BrowserStack account, or create a new [BrowserStack account](https://www.browserstack.com/users/sign_up).
507+
- Create a new Percy project.
508+
- Go to the Project's Settings page, identify your Percy Token, and export them as environment variables using the following commands.
509+
510+
### Installation
511+
512+
1. Install the Percy CLI.
513+
514+
```shell
515+
npm install @percy/cli
516+
```
517+
518+
### Export the Percy Token
519+
520+
- For \*nix based and Mac machines:
521+
522+
```sh
523+
export PERCY_TOKEN=[your-project-token]
524+
```
525+
526+
- For Windows:
527+
528+
```shell
529+
set PERCY_TOKEN=[your-project-token]
530+
```
531+
532+
## Run your first Visual Test
533+
534+
In this section, we will run the test cases to detect the visual differences. We run the test first, then toggle the property that instructs our test to change some CSS.
535+
536+
1. Run the test.
537+
Maven:
538+
```sh
539+
npx percy exec -- mvn clean test -P percy
540+
```
541+
542+
Gradle:
543+
```sh
544+
npx percy exec -- gradle clean percy
545+
```
546+
547+
2. Set the `changeCSS` property in the `src/test/java/com/browserstack/test/login/LoginVisuaalTest` directory to true.
548+
549+
3. Run the test again.
550+
551+
4. View your Project in the Percy dashboard and verify the differences.
552+
553+
## Run the Ignore Region Test
554+
555+
In this section, we will run the test that ignores a specific area of a webpage. In a real-world situation, you can think of dynamic content and advertisements where this use case applies.
556+
557+
1. Run the test.
558+
Maven:
559+
```sh
560+
npx percy exec -- mvn clean test -P percy-ignore
561+
```
562+
Gradle:
563+
```sh
564+
npx percy exec -- gradle clean percy-ignore
565+
```
566+
2. View your Project in the Percy dashboard and verify the differences.
567+
568+
500569
## Additional Resources
501570

502571
- View your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ buildscript {
1717
dependencies {
1818
implementation 'org.seleniumhq.selenium:selenium-java:3.141.59'
1919
implementation 'com.browserstack:browserstack-local-java:1.0.6'
20+
implementation 'io.percy:percy-java-selenium:1.0.0'
21+
implementation 'com.deque.html.axe-core:selenium:4.3.1'
2022
implementation 'org.testng:testng:7.1.0'
2123
implementation 'io.qameta.allure:allure-testng:2.13.8'
2224
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
@@ -96,6 +98,22 @@ tasks.register('bstack-single', Test) {
9698
}
9799
}
98100

101+
tasks.register('percy', Test) {
102+
useTestNG() {}
103+
include '**/LoginVisualTest.class'
104+
testLogging {
105+
events "PASSED", "FAILED", "SKIPPED"
106+
}
107+
}
108+
109+
tasks.register('percy-ignore', Test) {
110+
useTestNG() {}
111+
include '**/IgnoreRegionVisualTest.class'
112+
testLogging {
113+
events "PASSED", "FAILED", "SKIPPED"
114+
}
115+
}
116+
99117
tasks.register('bstack-local', Test) {
100118
useTestNG() {
101119
listeners.add("com.browserstack.test.utils.BrowserstackTestStatusListener")

pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
<artifactId>browserstack-local-java</artifactId>
7979
<version>1.0.6</version>
8080
</dependency>
81+
<dependency>
82+
<groupId>io.percy</groupId>
83+
<artifactId>percy-java-selenium</artifactId>
84+
<version>1.0.0</version>
85+
</dependency>
8186
<dependency>
8287
<groupId>com.deque.html.axe-core</groupId>
8388
<artifactId>selenium</artifactId>
@@ -225,6 +230,42 @@
225230
</plugins>
226231
</build>
227232
</profile>
233+
<profile>
234+
<id>percy</id>
235+
<build>
236+
<plugins>
237+
<plugin>
238+
<groupId>org.apache.maven.plugins</groupId>
239+
<artifactId>maven-surefire-plugin</artifactId>
240+
<configuration>
241+
<includes>
242+
<include>
243+
com.browserstack.test.suites.login.LoginVisualTest
244+
</include>
245+
</includes>
246+
</configuration>
247+
</plugin>
248+
</plugins>
249+
</build>
250+
</profile>
251+
<profile>
252+
<id>percy-ignore</id>
253+
<build>
254+
<plugins>
255+
<plugin>
256+
<groupId>org.apache.maven.plugins</groupId>
257+
<artifactId>maven-surefire-plugin</artifactId>
258+
<configuration>
259+
<includes>
260+
<include>
261+
com.browserstack.test.suites.login.IgnoreRegionVisualTest
262+
</include>
263+
</includes>
264+
</configuration>
265+
</plugin>
266+
</plugins>
267+
</build>
268+
</profile>
228269
<profile>
229270
<id>bstack-local</id>
230271
<build>

src/test/java/com/browserstack/test/suites/TestBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.browserstack.local.Local;
44
import com.browserstack.test.utils.DriverUtil;
55
import com.browserstack.test.utils.ScreenshotListener;
6+
import io.percy.selenium.Percy;
67
import org.json.simple.JSONObject;
78
import org.json.simple.parser.JSONParser;
89
import org.openqa.selenium.WebDriver;
@@ -34,6 +35,7 @@ public class TestBase {
3435
private Local local;
3536
protected WebDriverWait wait;
3637
private String environment;
38+
public static Percy percy;
3739

3840
public WebDriver getDriver() {
3941
return driver.get();
@@ -83,6 +85,7 @@ public void setUp(@Optional("on-prem") String environment, @Optional("single") S
8385
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
8486
driver.set(new RemoteWebDriver(new URL(DOCKER_SELENIUM_HUB_URL), dc));
8587
}
88+
percy = new Percy(getDriver());
8689
getDriver().get(url);
8790
getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
8891
wait = new WebDriverWait(getDriver(), 30);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.browserstack.test.suites.login;
2+
3+
import com.browserstack.test.suites.TestBase;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.JavascriptExecutor;
6+
import org.openqa.selenium.Keys;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
8+
import org.testng.Assert;
9+
import org.testng.annotations.Test;
10+
11+
public class IgnoreRegionVisualTest extends TestBase {
12+
13+
private boolean changeCSS = false;
14+
15+
@Test
16+
public void ignoreLogo() {
17+
JavascriptExecutor ex = ((JavascriptExecutor) getDriver());
18+
String percyCSS = ".Navbar_logo__26S5Y {visibility: hidden}";
19+
20+
percy.snapshot("Check Homepage with ignored logo", null, 1024, false, percyCSS);
21+
}
22+
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.browserstack.test.suites.login;
2+
3+
import com.browserstack.test.suites.TestBase;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.JavascriptExecutor;
6+
import org.openqa.selenium.Keys;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
8+
import org.testng.Assert;
9+
import org.testng.annotations.Test;
10+
11+
public class LoginVisualTest extends TestBase {
12+
13+
private boolean changeCSS = false;
14+
15+
@Test
16+
public void loginLockedUser() {
17+
JavascriptExecutor ex = ((JavascriptExecutor) getDriver());
18+
19+
percy.snapshot("Check Homepage");
20+
getDriver().findElement(By.id("signin")).click();
21+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#username input")));
22+
23+
if (changeCSS) {
24+
ex.executeScript("document.getElementById('login-btn').style.backgroundColor = '#FFA500';");
25+
ex.executeScript("document.getElementById('login-btn').style.width = '60%';");
26+
}
27+
percy.snapshot("Check Signin page");
28+
getDriver().findElement(By.cssSelector("#username input")).sendKeys("locked_user" + Keys.ENTER);
29+
getDriver().findElement(By.cssSelector("#password input")).sendKeys("testingisfun99" + Keys.ENTER);
30+
getDriver().findElement(By.id("login-btn")).click();
31+
32+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("api-error")));
33+
if (changeCSS) {
34+
ex.executeScript("document.getElementsByClassName('api-error')[0].style.fontSize = '200%';");
35+
}
36+
percy.snapshot("Check Login result");
37+
Assert.assertEquals(getDriver().findElement(By.className("api-error")).getText(), "Your account has been locked.");
38+
}
39+
40+
}

0 commit comments

Comments
 (0)