Skip to content

Commit 4f33668

Browse files
committed
[junit4] update tags and custom data and build name
1 parent 456e295 commit 4f33668

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

java/junit4/src/main/java/com/saucelabs/saucebindings/junit4/SauceBindingsWatcher.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package com.saucelabs.saucebindings.junit4;
22

3+
import com.saucelabs.saucebindings.CITools;
34
import com.saucelabs.saucebindings.DataCenter;
45
import com.saucelabs.saucebindings.SauceSession;
56
import com.saucelabs.saucebindings.options.SauceOptions;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.lang.annotation.Annotation;
11+
import java.util.ArrayList;
612
import java.util.Arrays;
713
import java.util.HashMap;
14+
import java.util.List;
815
import java.util.Map;
916
import java.util.Optional;
17+
import java.util.Properties;
1018
import java.util.logging.Logger;
1119
import lombok.Getter;
20+
import org.junit.experimental.categories.Category;
1221
import org.junit.rules.TestWatcher;
1322
import org.junit.runner.Description;
1423
import org.openqa.selenium.Capabilities;
@@ -22,6 +31,7 @@ public class SauceBindingsWatcher extends TestWatcher {
2231
private final DataCenter dataCenter;
2332
private SauceSession session;
2433
private WebDriver driver;
34+
private static final String buildName = CITools.getBuildName() + ": " + CITools.getBuildNumber();
2535

2636
public SauceBindingsWatcher() {
2737
this(new SauceOptions(), DataCenter.US_WEST);
@@ -61,15 +71,48 @@ public SauceBindingsWatcher(DataCenter dataCenter) {
6171

6272
@Override
6373
protected void starting(Description description) {
64-
if (sauceOptions.sauce().getName() == null) {
65-
sauceOptions.sauce().setName(description.getMethodName());
66-
}
74+
updateOptions(description);
6775

6876
session = new SauceSession(sauceOptions);
6977
session.setDataCenter(dataCenter);
7078
driver = session.start();
7179
}
7280

81+
private void updateOptions(Description description) {
82+
String className = description.getClassName();
83+
String simpleClassName = className.substring(className.lastIndexOf(".") + 1);
84+
sauceOptions.sauce().setName(simpleClassName + ": " + description.getMethodName());
85+
sauceOptions.sauce().setBuild(buildName);
86+
87+
Properties prop = new Properties();
88+
try (InputStream input = getClass().getResourceAsStream("/app.properties")) {
89+
prop.load(input);
90+
String version = prop.getProperty("version", "unknown");
91+
sauceOptions.sauce().getCustomData().put("sauce-bindings-junit4", version);
92+
} catch (IOException ignored) {
93+
sauceOptions.sauce().getCustomData().put("sauce-bindings-junit4", "unknown");
94+
}
95+
96+
List<String> tags = sauceOptions.sauce().getTags();
97+
98+
List<Annotation> annotations = (List<Annotation>) description.getAnnotations();
99+
for (Annotation annotation : annotations) {
100+
if (annotation instanceof Category) {
101+
Category category = (Category) annotation;
102+
for (Class<?> categoryClass : category.value()) {
103+
if (tags == null) {
104+
tags = new ArrayList<>();
105+
}
106+
tags.add(categoryClass.getSimpleName());
107+
}
108+
}
109+
}
110+
111+
if (tags != null) {
112+
sauceOptions.sauce().setTags(tags);
113+
}
114+
}
115+
73116
@Override
74117
protected void succeeded(Description description) {
75118
try {

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/ToggleLocalExample.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ public static void disableSauce() {
2626

2727
// 1. Use the SauceBindingsWatcher rule
2828
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
29+
@Rule public TestWatcher localWatcher = new LocalTestWatcher();
2930

3031
// 2. Get variables created by Watcher
3132
@Before
3233
public void storeVariables() {
33-
if (SauceSession.isDisabled()) {
34-
this.session = sauceWatcher.getSession();
35-
this.driver = sauceWatcher.getDriver();
36-
} else {
37-
driver = new ChromeDriver();
38-
}
34+
this.session = sauceWatcher.getSession();
35+
this.driver = SauceSession.isDisabled() ? new ChromeDriver() : sauceWatcher.getDriver();
3936
}
4037

4138
@Test

0 commit comments

Comments
 (0)