Skip to content

Commit 33470cb

Browse files
Merge pull request #322 from telekom/fix/log-webdriver-request
Fix logging webdriver request
2 parents 9cddb9d + ae2bb6b commit 33470cb

File tree

5 files changed

+110
-45
lines changed

5 files changed

+110
-45
lines changed

driver-ui/src/main/java/eu/tsystems/mms/tic/testframework/utils/DefaultCapabilityUtils.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121

2222
package eu.tsystems.mms.tic.testframework.utils;
2323

24-
import java.util.ArrayList;
24+
import com.google.gson.Gson;
25+
import com.google.gson.GsonBuilder;
26+
import eu.tsystems.mms.tic.testframework.logging.Loggable;
27+
import org.apache.commons.lang3.StringUtils;
28+
import org.openqa.selenium.Capabilities;
29+
import org.openqa.selenium.remote.DesiredCapabilities;
30+
2531
import java.util.Collections;
2632
import java.util.List;
2733
import java.util.Map;
28-
import java.util.TreeMap;
29-
import org.openqa.selenium.Capabilities;
30-
import org.openqa.selenium.chrome.ChromeOptions;
31-
import org.openqa.selenium.remote.DesiredCapabilities;
32-
import org.apache.commons.lang3.StringUtils;
3334

3435
/**
3536
* This is a simple helper to modify log messages of {@link Capabilities} to short long values or do other opertations
@@ -39,10 +40,10 @@
3940
*
4041
* @author Eric Kubenka
4142
*/
42-
public class DefaultCapabilityUtils {
43+
public class DefaultCapabilityUtils implements Loggable {
4344

4445
/**
45-
* Clean the given {@link Capabilities} and return a {@link Map}
46+
* Clean the given {@link Capabilities} from very long values and return a {@link Map}
4647
*
4748
* @param capabilities {@link Capabilities}
4849
* @return Map
@@ -52,59 +53,60 @@ public Map<String, Object> clean(Capabilities capabilities) {
5253
}
5354

5455
public Map<String, Object> clean(Map<String, Object> capabilityMap) {
55-
56-
// 1. make map modifiable.
57-
Map<String, Object> mutableCapabilityMap = new TreeMap<>(capabilityMap);
56+
// 1. clone and make map modifiable.
57+
// For deep cloning it is needed convert it to JSON and back because Firefox options also contain some immutable map objects
58+
Gson gson = new GsonBuilder().create();
59+
String json = gson.toJson(capabilityMap);
60+
Map<String, Object> clonedMap = (Map<String, Object>) gson.fromJson(json, Map.class);
5861

5962
// 2. do all the operations
60-
mutableCapabilityMap = shortChromeExtensionStrings(mutableCapabilityMap);
63+
shortMapValues(clonedMap);
6164

6265
// 3. make the map unmodifiable again.
63-
return Collections.unmodifiableMap(mutableCapabilityMap);
66+
return Collections.unmodifiableMap(clonedMap);
6467
}
6568

6669
/**
67-
* Extensions strings are very long, so therefore we will cut them off
68-
*
69-
* @param capabilityMap {@link Map}
70-
* @return Map
70+
* Some caps like Extensions strings are very long, so therefore we will cut them off
7171
*/
72-
private Map<String, Object> shortChromeExtensionStrings(Map<String, Object> capabilityMap) {
72+
private void shortMapValues(Map<String, Object> map2Short) {
73+
// Exception list of keys which should not shorten
74+
List<String> exceptionList = List.of(
75+
"path" // Absolute path for Firefox extension files
76+
);
7377

74-
final Object chromeOptionsObject = capabilityMap.get(ChromeOptions.CAPABILITY);
75-
final Object extensionsObject = capabilityMap.get("extensions");
76-
77-
if (chromeOptionsObject != null) {
78-
final Map chromeOptions = (Map) chromeOptionsObject;
79-
if (chromeOptions.containsKey("extensions")) {
80-
chromeOptions.put("extensions", shortAllStringsInLists(chromeOptions.get("extensions")));
78+
try {
79+
for (Map.Entry<String, Object> entry : map2Short.entrySet()) {
80+
Object value = entry.getValue();
81+
String key = entry.getKey();
82+
if (value instanceof Map) {
83+
Map<String, Object> subMap = (Map<String, Object>) value;
84+
shortMapValues(subMap);
85+
} else {
86+
String stringValue = String.valueOf(value);
87+
if (stringValue.length() > 40 && !exceptionList.contains(key)) {
88+
entry.setValue(stringValue.substring(0, 40) + "...");
89+
}
90+
}
8191
}
92+
} catch (Exception e) {
93+
log().debug("Cannot clean map: ", e);
8294
}
8395

84-
if (extensionsObject != null) {
85-
capabilityMap.put("extensions", shortAllStringsInLists(extensionsObject));
86-
}
87-
88-
return capabilityMap;
89-
}
90-
91-
private List<String> shortAllStringsInLists(final Object stringListObject) {
92-
final List<String> extList = new ArrayList<>();
93-
((List<String>) stringListObject).forEach(e -> extList.add(e.substring(0, 10) + "..."));
94-
return extList;
9596
}
9697

9798
/**
9899
* Sets a capability value if the existing value doesn't match the same type,
99100
* is an empty string or doesn't exist.
101+
*
100102
* @param capabilities
101103
* @param capabilityName
102104
* @param capability
103105
* @param <T>
104106
*/
105107
public <T> void putIfAbsent(DesiredCapabilities capabilities, String capabilityName, T capability) {
106108
Object existingCapability = capabilities.getCapability(capabilityName);
107-
if (!capability.getClass().isInstance(existingCapability) || (existingCapability instanceof String && StringUtils.isBlank((String)existingCapability))) {
109+
if (!capability.getClass().isInstance(existingCapability) || (existingCapability instanceof String && StringUtils.isBlank((String) existingCapability))) {
108110
capabilities.setCapability(capabilityName, capability);
109111
}
110112
}

integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/playground/DriverAndGuiElementTest.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,33 @@
1919
* under the License.
2020
*
2121
*/
22-
package eu.tsystems.mms.tic.testframework.playground;
22+
package eu.tsystems.mms.tic.testframework.playground;
2323

2424
import eu.tsystems.mms.tic.testframework.AbstractTestSitesTest;
2525
import eu.tsystems.mms.tic.testframework.constants.Browsers;
2626
import eu.tsystems.mms.tic.testframework.core.pageobjects.testdata.PageWithExistingElement;
27-
import eu.tsystems.mms.tic.testframework.core.pageobjects.testdata.PageWithNotExistingElement;
2827
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
2928
import eu.tsystems.mms.tic.testframework.pageobjects.UiElementFinder;
30-
import eu.tsystems.mms.tic.testframework.pageobjects.factory.PageFactory;
3129
import eu.tsystems.mms.tic.testframework.report.model.context.SessionContext;
3230
import eu.tsystems.mms.tic.testframework.testing.PageFactoryProvider;
33-
import eu.tsystems.mms.tic.testframework.testing.TesterraTest;
3431
import eu.tsystems.mms.tic.testframework.testing.UiElementFinderFactoryProvider;
35-
import eu.tsystems.mms.tic.testframework.testing.WebDriverManagerProvider;
32+
import eu.tsystems.mms.tic.testframework.useragents.ChromeConfig;
33+
import eu.tsystems.mms.tic.testframework.useragents.FirefoxConfig;
3634
import eu.tsystems.mms.tic.testframework.utils.UITestUtils;
3735
import eu.tsystems.mms.tic.testframework.webdrivermanager.DesktopWebDriverRequest;
3836
import eu.tsystems.mms.tic.testframework.webdrivermanager.WebDriverSessionsManager;
3937
import eu.tsystems.mms.tic.testframework.webdrivermanager.desktop.WebDriverMode;
40-
import java.util.Map;
41-
4238
import org.openqa.selenium.By;
4339
import org.openqa.selenium.WebDriver;
40+
import org.openqa.selenium.firefox.FirefoxProfile;
4441
import org.openqa.selenium.remote.DesiredCapabilities;
4542
import org.testng.Assert;
4643
import org.testng.annotations.Test;
4744

45+
import java.io.File;
46+
import java.util.Map;
47+
import java.util.Objects;
48+
4849
public class DriverAndGuiElementTest extends AbstractTestSitesTest implements UiElementFinderFactoryProvider, PageFactoryProvider {
4950

5051
@Test
@@ -90,6 +91,40 @@ public void testFailing() throws Exception {
9091
Assert.assertTrue(false);
9192
}
9293

94+
@Test
95+
public void testsimpleUiElement() {
96+
WebDriver driver = getWebDriver();
97+
98+
UiElementFinder uiElementFinder = UI_ELEMENT_FINDER_FACTORY.create(driver);
99+
UiElement element = uiElementFinder.find(By.id("1"));
100+
element.waitFor().attribute("href").getActual();
101+
element.waitFor().tagName().getActual();
102+
element.waitFor().classes().getActual();
103+
}
104+
105+
@Test
106+
public void testFirefoxExtension() throws Exception {
107+
108+
File chromeExtensionFile = new File(Objects.requireNonNull(
109+
Thread.currentThread().getContextClassLoader()
110+
.getResource("testfiles/Simple_Translate_2.8.1.0.crx")).getFile());
111+
112+
WEB_DRIVER_MANAGER.setUserAgentConfig(Browsers.chrome, (ChromeConfig) options -> {
113+
options.addExtensions(chromeExtensionFile);
114+
});
115+
116+
// File firefoxExtensionFile = new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("testfiles/simple_translate-2.8.1.xpi")).getFile());
117+
File firefoxExtensionFile = new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("testfiles/i_dont_care_about_cookies-3.4.6.xpi")).getFile());
118+
119+
WEB_DRIVER_MANAGER.setUserAgentConfig(Browsers.firefox, (FirefoxConfig) options -> {
120+
FirefoxProfile profile = new FirefoxProfile();
121+
profile.addExtension(firefoxExtensionFile);
122+
options.setProfile(profile);
123+
});
124+
125+
WebDriver driver = getWebDriver();
126+
}
127+
93128
@Test
94129
public void testT02_checkNotExistingElement() {
95130
WebDriver webDriver = this.getWebDriver();

integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/test/webdrivermanager/DesktopWebDriverFactoryTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,28 @@
2121

2222
package eu.tsystems.mms.tic.testframework.test.webdrivermanager;
2323

24+
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
25+
import eu.tsystems.mms.tic.testframework.pageobjects.UiElementFinder;
2426
import eu.tsystems.mms.tic.testframework.report.model.context.SessionContext;
2527
import eu.tsystems.mms.tic.testframework.testing.TesterraTest;
28+
import eu.tsystems.mms.tic.testframework.testing.UiElementFinderFactoryProvider;
2629
import eu.tsystems.mms.tic.testframework.testing.WebDriverManagerProvider;
2730
import eu.tsystems.mms.tic.testframework.webdrivermanager.DesktopWebDriverRequest;
2831
import eu.tsystems.mms.tic.testframework.webdrivermanager.WebDriverRequest;
32+
import org.openqa.selenium.By;
2933
import org.openqa.selenium.Platform;
3034
import org.openqa.selenium.WebDriver;
35+
import org.openqa.selenium.chrome.ChromeOptions;
3136
import org.openqa.selenium.remote.CapabilityType;
3237
import org.openqa.selenium.remote.DesiredCapabilities;
3338
import org.testng.Assert;
3439
import org.testng.annotations.Test;
3540

41+
import java.io.File;
3642
import java.util.Map;
43+
import java.util.Objects;
3744

38-
public class DesktopWebDriverFactoryTest extends TesterraTest implements WebDriverManagerProvider {
45+
public class DesktopWebDriverFactoryTest extends TesterraTest implements WebDriverManagerProvider, UiElementFinderFactoryProvider {
3946

4047
@Test
4148
public void testT01_BaseURL() throws Exception {
@@ -94,4 +101,25 @@ public void testT04_PlatformCaps() {
94101
Assert.assertEquals(webDriverRequest.getCapabilities().get(CapabilityType.PLATFORM_NAME), Platform.LINUX);
95102
}
96103

104+
// TODO: Selenium 3 cannot merge correctly caps. Issue is fixed in Selenium 4 (https://github.com/telekom/testerra/issues/285)
105+
@Test(enabled = false)
106+
public void testT05_ChromeExtensions() {
107+
108+
File chromeExtensionFile = new File(
109+
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("testfiles/Simple_Translate_2.8.1.0.crx")).getFile());
110+
111+
ChromeOptions options = new ChromeOptions();
112+
options.addExtensions(chromeExtensionFile);
113+
DesktopWebDriverRequest request = new DesktopWebDriverRequest();
114+
request.getDesiredCapabilities().merge(options);
115+
116+
WebDriver webDriver = WEB_DRIVER_MANAGER.getWebDriver(request);
117+
webDriver.get("chrome://extensions-internals/");
118+
UiElementFinder uiElementFinder = UI_ELEMENT_FINDER_FACTORY.create(webDriver);
119+
UiElement chromeExtensionJson = uiElementFinder.find(By.xpath("//pre"));
120+
String content = chromeExtensionJson.waitFor().text().getActual();
121+
Assert.assertTrue(content.contains("Simple Translate"));
122+
123+
}
124+
97125
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)