Skip to content

Commit

Permalink
Merge branch 'bugfix/thread-safe-localization' into testerra2
Browse files Browse the repository at this point in the history
# Conflicts:
#	integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/test/reporting/ScreenshotsTest.java
#	integration-tests/src/test/resources/Integration.xml
  • Loading branch information
Mike Reiche committed Oct 14, 2021
2 parents a9527ba + fea986c commit af88d62
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 52 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ allprojects {
apply plugin: 'java-library'
apply plugin: 'project-report'

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = "UTF-8"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,18 @@
import java.util.ResourceBundle;

public class LocalizedBundle {
private ResourceBundle resourceBundle;
private final ResourceBundle resourceBundle;
private static final ResourceBundle.Control resourceBundleController = new UTF8ResourceBundleControl();
/**
* When the bundle name is NULL, the {@link #resourceBundle} is fixed
*/
private final String bundleName;

public LocalizedBundle(String bundleName, Locale locale) {
this.bundleName = null;
this.resourceBundle = ResourceBundle.getBundle(bundleName, locale, resourceBundleController);
}

public LocalizedBundle(String bundleName) {
this.bundleName = bundleName;
recreateLocalizedResourceBundle();
}

private ResourceBundle getResourceBundle() {
/**
* When the locale is not fixed and differs from the default locale
* than recreate the bundle.
*/
if (this.bundleName != null && Locale.getDefault() != this.resourceBundle.getLocale()) {
recreateLocalizedResourceBundle();
}
return resourceBundle;
}

private void recreateLocalizedResourceBundle() {
this.resourceBundle = ResourceBundle.getBundle(this.bundleName, resourceBundleController);
this(bundleName, Locale.getDefault());
}

public String getString(String label) {
ResourceBundle resourceBundle = getResourceBundle();
if (resourceBundle.containsKey(label)) {
return resourceBundle.getString(label);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@
*/
package eu.tsystems.mms.tic.testframework.l10n;

import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final public class SimpleLocalization {
public static final String BUNDLE_NAME="lang";
private static final LocalizedBundle bundle = new LocalizedBundle(BUNDLE_NAME);
public static final String BUNDLE_NAME = "lang";
private static LocalizedBundle bundle;
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLocalization.class);

public static String getText(final String label) {
return bundle.getString(label);
return getDefaultBundle().getString(label);
}

public static LocalizedBundle getDefaultBundle() {
if (bundle == null) {
setDefault(Locale.getDefault());
}
return bundle;
}

public LocalizedBundle getBundle() {
public static LocalizedBundle setDefault(Locale locale) {
LOGGER.info("Change default locale to: " + locale);
bundle = new LocalizedBundle(BUNDLE_NAME, locale);
return bundle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private File addFile(File sourceFile, File directory, FileMode fileMode) {
break;
}
} catch (IOException e) {
log().error(e.getMessage());
log().error("Could not add file", e);
}
return new File(directory, sourceFile.getName());
}
Expand Down
11 changes: 4 additions & 7 deletions docs/src/docs/modules/localization.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ import eu.tsystems.mms.tic.testframework.l10n.SimpleLocalization;
UiElement loginBtn = find(By.linkText(SimpleLocalization.getText("BTN_LOGIN")));
----

But you can use your own localized bundles.

`SimpleLocalization` uses `Locale.getDefault()` by default, but
you can switch the default locale the following way.
[source, java]
----
import eu.tsystems.mms.tic.testframework.l10n.LocalizedBundle;
LocalizedBundle bundle = new LocalizedBundle("testdata");
bundle.getString("TEST_KEY");
LocalizedBundle bundle = SimpleLocalization.setDefault(Locale.GERMAN);
----

Or bundles with a fixed locale.
But you can use your own localized bundles:

[source, java]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public WebTestPage reloadPage() {
return new WebTestPage(this.getWebDriver());
}

public GuiElement getOpenAgain() {
return this.openAgainLink;
}

/**
* Click on not existing element
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class LocalizationTest extends TesterraTest {

@Test(dataProvider = "locales")
public void test_readUtf8FromResourceBundle(String locale, String expected) {
Locale.setDefault(Locale.forLanguageTag(locale));
Assert.assertEquals(SimpleLocalization.getText("TEST"), expected);
LocalizedBundle defaultBundle = SimpleLocalization.setDefault(Locale.forLanguageTag(locale));
Assert.assertEquals(defaultBundle.getString("TEST"), expected);
}

@Test
public void test_inexistentLocalizedProperty() {
Assert.assertEquals("NOT_EXISTENT", SimpleLocalization.getText("NOT_EXISTENT"));
Assert.assertEquals(SimpleLocalization.getText("NOT_EXISTENT"), "NOT_EXISTENT");
}

@DataProvider
Expand All @@ -53,12 +53,11 @@ public Object[][] locales() {

@Test
public void test_fixedLocale() {
Locale.setDefault(Locale.ENGLISH);
LocalizedBundle defaultBundle = SimpleLocalization.setDefault(Locale.ENGLISH);
LocalizedBundle germanBundle = new LocalizedBundle(SimpleLocalization.BUNDLE_NAME, Locale.GERMAN);
LocalizedBundle defaultBundle = new LocalizedBundle(SimpleLocalization.BUNDLE_NAME);
Assert.assertNotEquals(germanBundle.getString("TEST"), defaultBundle.getString("TEST"));

Locale.setDefault(Locale.GERMAN);
defaultBundle = SimpleLocalization.setDefault(Locale.GERMAN);
Assert.assertEquals(germanBundle.getString("TEST"), defaultBundle.getString("TEST"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@
import eu.tsystems.mms.tic.testframework.annotations.Fails;
import eu.tsystems.mms.tic.testframework.common.Testerra;
import eu.tsystems.mms.tic.testframework.core.pageobjects.testdata.BasePage;
import eu.tsystems.mms.tic.testframework.core.pageobjects.testdata.WebTestPage;
import eu.tsystems.mms.tic.testframework.execution.testng.AssertCollector;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
import eu.tsystems.mms.tic.testframework.report.Report;
import eu.tsystems.mms.tic.testframework.report.model.context.MethodContext;
import eu.tsystems.mms.tic.testframework.report.model.context.Screenshot;
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextController;
import eu.tsystems.mms.tic.testframework.test.PageFactoryTest;
import eu.tsystems.mms.tic.testframework.utils.AssertUtils;
import eu.tsystems.mms.tic.testframework.utils.UITestUtils;
import eu.tsystems.mms.tic.testframework.testing.AssertProvider;
import java.io.IOException;
import java.util.Optional;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.reporters.Files;

/**
* Tests if screenshots are added to the MethodContext when a test fails.
Expand Down Expand Up @@ -109,4 +114,18 @@ public void test_take_screenshot_via_collected_assertion() {
public void test_Screenshot_is_present_in_MethodContext_on_collected_assertion() {
this.screenshot_is_present_in_MethodContext("test_take_screenshot_via_collected_assertion");
}

@Test
public void test_DOMSource() throws IOException {
WebTestPage page = new WebTestPage(WebDriverManager.getWebDriver());

for (int s = 0; s < 3; ++s) {
page.getOpenAgain().click();
}
Screenshot screenshot = UITestUtils.takeScreenshot(page.getWebDriver(), false);
String screenshotSource = Files.readFile(screenshot.getPageSourceFile());

String expected = "<p id=\"99\">Open again clicked<br>Open again clicked<br>Open again clicked<br>";
AssertUtils.assertContains(screenshotSource, expected);
}
}
1 change: 1 addition & 0 deletions integration-tests/src/test/resources/Integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<suite-file path="PageObjects.xml"></suite-file>
<suite-file path="UiElement.xml"></suite-file>
<suite-file path="MethodParallel.xml"></suite-file>
<suite-file path="Localization.xml"></suite-file>
</suite-files>

<test name="Sequential tests" parallel="false">
Expand Down
14 changes: 14 additions & 0 deletions integration-tests/src/test/resources/Localization.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Localization " verbose="10" configfailurepolicy="continue" thread-count="10" parallel="false">

<test name="Core" parallel="methods">
<groups>
<run>
<exclude name="SEQUENTIAL"/>
</run>
</groups>
<packages>
<package name="eu.tsystems.mms.tic.testframework.test.l10n"/>
</packages>
</test>
</suite>
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@
<div class="flex vcenter">
<button mdc-icon-button icon="keyboard_arrow_left" click.delegate="_left()" if.bind="_screenshots.length>1"></button>
<div class="flex">
<ul class="mdc-list mdc-list--dense ml1 mr1">
<ul class="mdc-list mdc-list--dense ml1 mr1" mdc-caption>
<li class="mdc-custom-list-item">
<span class="secondary sr1">Title</span>${_current.meta.Title}
</li>
<li class="mdc-custom-list-item" if.bind="_current.meta.URL.length">
<span class="secondary sr1">URL</span><a href="${_current.meta.URL}" rel="noopener" target="_blank">${_current.meta.URL}</a>
</li>
<li class="mdc-custom-list-item" if.bind="_pageSourceFile">
<span class="secondary sr1">PageSource</span><a href="${_pageSourceFile.relativePath}" rel="noopener" target="_blank">${_pageSourceFile.relativePath}</a>
</li>
</ul>
<ul class="mdc-list mdc-list--dense ml1 mr1">
<ul class="mdc-list mdc-list--dense ml1 mr1" mdc-caption>
<li class="mdc-custom-list-item">
<span class="secondary sr1">Taken</span><span title="${_current.lastModified|dateFormat}">${_current.lastModified | dateFormat:"ddd MMM D HH:mm:ss"}</span>
</li>
Expand All @@ -47,6 +50,7 @@
</div>
<button mdc-icon-button class="icon-button--dense" icon="clear" title="Close dialog" data-mdc-dialog-action="cancel"></button>
</div>

<mdc-dialog-content class="pt0">
<img src="${_current.relativePath}" class="screenshot-image"/>
</mdc-dialog-content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
* specific language governing permissions and limitations
* under the License.
*/
import {autoinject, PLATFORM, useView} from 'aurelia-framework';
import {autoinject} from 'aurelia-framework';
import {data} from "../../services/report-model";
import {MdcDialog} from '@aurelia-mdc-web/dialog';
import './screenshot-dialog.scss'
import IFile = data.IFile;
import {StatisticsGenerator} from "../../services/statistics-generator";
import ISessionContext = data.ISessionContext;

export interface IScreenshotsDialogParams {
screenshotIds:string[],
current:IFile
current:data.IFile
}

@autoinject
export class ScreenshotsDialog {
private _screenshots:IFile[];
private _current:IFile;
private _screenshots:data.IFile[];
private _current:data.IFile;
private _index = 0;
private _sessionContext:ISessionContext;
private _pageSourceFile:data.IFile;

constructor(
private _dialog: MdcDialog,
Expand Down Expand Up @@ -66,7 +66,7 @@ export class ScreenshotsDialog {
// }
}

private _showScreenshot(file:IFile) {
private _showScreenshot(file:data.IFile) {
this._current = file;

const sessionKey = this._current.meta.SessionKey;
Expand All @@ -76,6 +76,14 @@ export class ScreenshotsDialog {
this._sessionContext = sessionContexts.find(value => value.contextValues.name === sessionKey);
});
}

this._pageSourceFile = null;
if (this._current.meta.sourcesRefId) {
this._statistics.getFilesForIds([this._current.meta.sourcesRefId]).then(files => {
this._pageSourceFile = files.find(value => true);
})
}

}

private _left() {
Expand Down
5 changes: 3 additions & 2 deletions report-ng/app/src/styles/mdc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ mdc-drawer-app-content {
padding: 0;
padding-left: 16px;
padding-right: 16px;
height: 30px;
line-height: normal;
height:24px;
line-height: 1em;
}

.mdc-wrapable-cell {
white-space: normal;
}
Expand Down

0 comments on commit af88d62

Please sign in to comment.