Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Ability to customize Test File Name Template #737 (#738)
Browse files Browse the repository at this point in the history
* Ability to customize Test File Name Template #737

This feature will allow to customize "test file name template" adding a new capability to the browser named "testFileNameTemplate".

How this works?

Template accepts variables like:
{proxyName} - Zalenium|SauceLabs|BrowserStack
{testName} - The one added as "name" on capabilities, otherwise a session key
{browser} - The browser name
{platform} - OS where test runs
{timestamp} - Timestamp of test inizialitation
{testStatus} - Test result: COMPLETED|TIMEOUT|SUCCESS|FAILED

You can add a string to the capability adding static text and any of the variables above mentioned.

Default template:
{proxyName}_{testName}_{browser}_{platform}_{timestamp}_{testStatus}

* Documentation added for new feature: Ability to customize Test File Name Template
  • Loading branch information
gaiquelsf authored and diemol committed Oct 10, 2018
1 parent 20d3ea2 commit 0eb8544
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 44 deletions.
119 changes: 76 additions & 43 deletions docs/_posts/2000-01-07-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ fa-icon: check-square-o
* Auto-refresh, add `?refresh=numberOfSeconds` to refresh the view automatically.
<br>
<br>
* E.g. [http://localhost:4444/grid/admin/live?refresh=20](http://localhost:4444/grid/admin/live?refresh=20){:target="_blank"}
* E.g. [http://localhost:4444/grid/admin/live?refresh=20](http://localhost:4444/grid/admin/live?refresh=20){:target="_blank"}
will refresh the page every 20 seconds.

#### Showing the test name on the live preview
Having a `name` capability with the test name will display it in the live preview.
Having a `name` capability with the test name will display it in the live preview.
See `test name` for more information.

#### Filtering tests by build name
If more than one person is using the same instance of Zalenium, with a `build`
capability in your tests, the live preview can be filtered to show only the tests that belong to a specific build.
Pass `?build=myTestBuild` at the end of the url. E.g.
If more than one person is using the same instance of Zalenium, with a `build`
capability in your tests, the live preview can be filtered to show only the tests that belong to a specific build.
Pass `?build=myTestBuild` at the end of the url. E.g.
[http://localhost:4444/grid/admin/live?build=myTestBuild](http://localhost:4444/grid/admin/live?build=myTestBuild){:target="_blank"}

See more details at `build name`.
Expand All @@ -42,7 +42,7 @@ See more details at `build name`.
<img id="dashboard" src="img/dashboard.gif" />
</p>

* Go to [http://localhost:4444/grid/dashboard](http://localhost:4444/dashboard){:target="_blank"}
* Go to [http://localhost:4444/grid/dashboard](http://localhost:4444/dashboard){:target="_blank"}
<br>
<br>
* You can replace `localhost` for the IP/machine name where Zalenium is running
Expand All @@ -61,8 +61,8 @@ See more details at `build name`.
<summary>Click for details.</summary>

<div class="container m-2 p-2">
Adding a <code>name</code> capability with the test name will do two things; it will
be displayed in the live preview to help you identify where your test is running, and the video file will also
Adding a <code>name</code> capability with the test name will do two things; it will
be displayed in the live preview to help you identify where your test is running, and the video file will also
use it in the file name. Example code in Java for the capability:

{% highlight java %}
Expand All @@ -73,7 +73,7 @@ See more details at `build name`.
{% endhighlight %}

</div>

</details>


Expand All @@ -93,7 +93,7 @@ See more details at `build name`.
{% endhighlight %}

</div>

</details>


Expand All @@ -103,10 +103,10 @@ See more details at `build name`.
<summary>Click for details.</summary>

<div class="container m-2 p-2">
By default, Zalenium allows a test to be idle up to 90 seconds. After that elapsed time, the session will be terminated,
the node will be shutdown and the recorded video will be saved (if video recording is enabled). This prevents a test to
run indefinitely after something went wrong. If you need to have a longer idle timeout, just set an
<code>idleTimeout</code> capability in your test. Example code in Java for the
By default, Zalenium allows a test to be idle up to 90 seconds. After that elapsed time, the session will be terminated,
the node will be shutdown and the recorded video will be saved (if video recording is enabled). This prevents a test to
run indefinitely after something went wrong. If you need to have a longer idle timeout, just set an
<code>idleTimeout</code> capability in your test. Example code in Java for the
capability (it sets the <code>idleTimeout</code> to 150 seconds):

{% highlight java %}
Expand All @@ -117,18 +117,18 @@ See more details at `build name`.
{% endhighlight %}

</div>

</details>

#### Screen Resolution
<details>
<summary>Click for details.</summary>

<div class="container m-2 p-2">
You can pass a custom screen resolution for your test, just include a <code>screenResolution</code>
with the desired value. E.g. <code>screenResolution=1280x1024</code>. Also supported for
the same purpose <code>resolution</code> and
<code>screen-resolution</code>. Example code in Java for the capability
You can pass a custom screen resolution for your test, just include a <code>screenResolution</code>
with the desired value. E.g. <code>screenResolution=1280x1024</code>. Also supported for
the same purpose <code>resolution</code> and
<code>screen-resolution</code>. Example code in Java for the capability
<code>screenResolution</code>

{% highlight java %}
Expand All @@ -139,16 +139,16 @@ See more details at `build name`.
{% endhighlight %}

</div>

</details>

#### Disable Video Recording
<details>
<summary>Click for details.</summary>

<div class="container m-2 p-2">
It is possible to disable video recording (enabled by default) via test capabilities. Add a
<code>recordVideo=false</code> capability and no video will be recorded. Example code
It is possible to disable video recording (enabled by default) via test capabilities. Add a
<code>recordVideo=false</code> capability and no video will be recorded. Example code
in Java for the capability <code>recordVideo</code>

{% highlight java %}
Expand All @@ -159,7 +159,7 @@ See more details at `build name`.
{% endhighlight %}

</div>

</details>


Expand All @@ -168,9 +168,9 @@ See more details at `build name`.
<summary>Click for details.</summary>

<div class="container m-2 p-2">
Run your test in a different time zone from the default one <code>Europe/Berlin</code>,
just pass a capability <code>tz</code> with the desired value. E.g.
<code>tz=America/Montreal</code>Example code in Java for the capability
Run your test in a different time zone from the default one <code>Europe/Berlin</code>,
just pass a capability <code>tz</code> with the desired value. E.g.
<code>tz=America/Montreal</code>Example code in Java for the capability
<code>tz</code>.

{% highlight java %}
Expand All @@ -181,7 +181,40 @@ See more details at `build name`.
{% endhighlight %}

</div>


</details>

#### Test File Name Template
<details>
<summary>Click for details.</summary>

<div class="container m-2 p-2">
Adding a <code>testFileNameTemplate</code> capability will save logs and video recording with file name format you prefer.
Resulting file name can have invariable and variable text. You can find the list of variable text below:

{proxyName} - Zalenium|SauceLabs|BrowserStack
{testName} - The one added as "name" on capabilities, otherwise a session key
{browser} - The browser name
{platform} - OS where test runs
{timestamp} - Timestamp of test initialization
{testStatus} - Test result: COMPLETED|TIMEOUT|SUCCESS|FAILED

E.g.
<code>myID_{browser}_{testStatus}</code> will result on video file name as "myID_chrome_COMPLETED.mp4".

Default file name template is: {proxyName}_{testName}_{browser}_{platform}_{timestamp}_{testStatus}

Example code in Java for the capability <code>testFileNameTemplate</code>.

{% highlight java %}
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
desiredCapabilities.setCapability(CapabilityType.PLATFORM_NAME, Platform.LINUX);
desiredCapabilities.setCapability("testFileNameTemplate", "myID_{browser}_{testStatus}");
{% endhighlight %}

</div>

</details>

#### Marking the test as passed or failed
Expand All @@ -191,17 +224,17 @@ See more details at `build name`.
<div class="container m-2 p-2">
By default, tests in Zalenium are marked in the dashboard either as COMPLETED (session finishes normally) or TIMEOUT
(session was ended due to inactivity). You can mark the test as passed or failed based on the assertions you do on
your side with your test framework, add a cookie from with the name <code>zaleniumTestPassed</code>
with a value of <code>true</code> (if the test passes) or false (if the test fails).
This could be done in the after method where you already know if the test passed or failed. Here is an example in Java:
your side with your test framework, add a cookie from with the name <code>zaleniumTestPassed</code>
with a value of <code>true</code> (if the test passes) or false (if the test fails).
This could be done in the after method where you already know if the test passed or failed. Here is an example in Java:

{% highlight java %}
Cookie cookie = new Cookie("zaleniumTestPassed", "true");
webDriver.manage().addCookie(cookie);
{% endhighlight %}

</div>

</details>

#### Referencing test steps in the recorded video
Expand All @@ -218,29 +251,29 @@ See more details at `build name`.
Cookie cookie = new Cookie("zaleniumMessage", "Go to home page");
webDriver.manage().addCookie(cookie);
webDriver.get("http://www.homepage.com");

cookie = new Cookie("zaleniumMessage", "Search and add article to the basket");
webDriver.manage().addCookie(cookie);
/*
Code performing WebDriver actions to search and add article to the basket.
*/

cookie = new Cookie("zaleniumMessage", "Go to the checkout");
webDriver.manage().addCookie(cookie);
/*
Code performing WebDriver actions to go to the checkout.
*/

cookie = new Cookie("zaleniumMessage", "Pay");
webDriver.manage().addCookie(cookie);
/*
Code performing WebDriver actions to pay.
*/

{% endhighlight %}

</div>

</details>


Expand All @@ -249,7 +282,7 @@ See more details at `build name`.
<summary>Click for details.</summary>

<div class="container m-2 p-2">
You can set the browser language when using Google Chrome, just pass the <code>ChromeOptions</code>
You can set the browser language when using Google Chrome, just pass the <code>ChromeOptions</code>
variable with the language argument. Example code in Java :

{% highlight java %}
Expand All @@ -260,7 +293,7 @@ See more details at `build name`.
{% endhighlight %}

</div>

</details>

#### Send video and logs to some external system for storage
Expand All @@ -281,7 +314,7 @@ Thanks [adrichem](https://github.com/adrichem) for implementing this feature! Mo
{% highlight java %}
this.Driver.Manage().Cookies.AddCookie(new Cookie("zaleniumMetadata", >JsonConvert.SerializeObject(Metadata)))
{% endhighlight %}

Here is an <a href="https://github.com/adrichem/artifact-store">example</a> of the endpoint that can receive the requests.

</details>
Expand All @@ -291,10 +324,10 @@ Thanks [adrichem](https://github.com/adrichem) for implementing this feature! Mo

### Waiting for Zalenium to be ready
A simple way to check if Zalenium is ready to receive test requests is to use the built-in status url that Selenium
Grid already provides: [http://localhost:4444/wd/hub/status](http://localhost:4444/wd/hub/status){:target="_blank"}
Grid already provides: [http://localhost:4444/wd/hub/status](http://localhost:4444/wd/hub/status){:target="_blank"}

If the endpoint returns an HTTP code 200 and the value for the key `ready` is `true`, it means that Zalenium is ready
to receive tests requests. Here is an example how the JSON payload looks like:
If the endpoint returns an HTTP code 200 and the value for the key `ready` is `true`, it means that Zalenium is ready
to receive tests requests. Here is an example how the JSON payload looks like:

`{% highlight json %}
{
Expand Down Expand Up @@ -323,4 +356,4 @@ You could `grep` the value using `jq` in a bash function like this:

{% highlight shell %}
curl -sSL http://localhost:4444/wd/hub/status | jq .value.ready | grep true
{% endhighlight %}
{% endhighlight %}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class TestInformation {
private String screenDimension;
private String timeZone;
private String build;
private String testFileNameTemplate;
private Date retentionDate;
private TestStatus testStatus;
private boolean videoRecorded;
Expand Down Expand Up @@ -147,7 +148,11 @@ public void buildVideoFileName() {
buildName = this.build.replaceAll("[^a-zA-Z0-9]", "_") + "/";
}

this.testNameNoExtension = TEST_FILE_NAME_TEMPLATE
if(Strings.isNullOrEmpty(this.testFileNameTemplate)) {
this.testFileNameTemplate = TEST_FILE_NAME_TEMPLATE;
}

this.testNameNoExtension = this.testFileNameTemplate
.replace("{proxyName}", this.proxyName.toLowerCase())
.replace("{testName}", getTestName())
.replace("{browser}", this.browser)
Expand Down Expand Up @@ -228,6 +233,7 @@ private TestInformation(TestInformationBuilder builder) {
this.screenDimension = Optional.ofNullable(builder.screenDimension).orElse("");
this.timeZone = Optional.ofNullable(builder.timeZone).orElse("");
this.build = Optional.ofNullable(builder.build).orElse("");
this.testFileNameTemplate = Optional.ofNullable(builder.testFileNameTemplate).orElse("");
this.retentionDate = Optional.ofNullable(builder.retentionDate).orElse(new Date());
this.testStatus = builder.testStatus;
this.videoRecorded = true;
Expand All @@ -249,6 +255,7 @@ public static class TestInformationBuilder {
private String screenDimension;
private String timeZone;
private String build;
private String testFileNameTemplate;
private Date retentionDate;
private TestStatus testStatus;
private JsonObject metadata;
Expand Down Expand Up @@ -318,6 +325,11 @@ public TestInformationBuilder withBuild(String build) {
return this;
}

public TestInformationBuilder withTestFileNameTemplate(String testFileNameTemplate) {
this.testFileNameTemplate = testFileNameTemplate;
return this;
}

public TestInformationBuilder withretentionDate(Date retentionDate) {
this.retentionDate = retentionDate;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class DockerSeleniumCapabilityMatcher extends DefaultCapabilityMatcher {
private static final List<String> ZALENIUM_CUSTOM_CAPABILITIES_NO_PREFIX = Arrays.asList(
ZaleniumCapabilityType.TEST_NAME_NO_PREFIX,
ZaleniumCapabilityType.BUILD_NAME_NO_PREFIX,
ZaleniumCapabilityType.TEST_FILE_NAME_TEMPLATE_NO_PREFIX,
ZaleniumCapabilityType.IDLE_TIMEOUT_NO_PREFIX,
ZaleniumCapabilityType.SCREEN_RESOLUTION_NO_PREFIX,
ZaleniumCapabilityType.RESOLUTION_NO_PREFIX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface ZaleniumCapabilityType {
String TEST_NAME = CUSTOM_CAPABILITY_PREFIX.concat(TEST_NAME_NO_PREFIX);
String BUILD_NAME_NO_PREFIX = "build";
String BUILD_NAME = CUSTOM_CAPABILITY_PREFIX.concat(BUILD_NAME_NO_PREFIX);
String TEST_FILE_NAME_TEMPLATE_NO_PREFIX = "testFileNameTemplate";
String TEST_FILE_NAME_TEMPLATE = CUSTOM_CAPABILITY_PREFIX.concat(TEST_FILE_NAME_TEMPLATE_NO_PREFIX);
String IDLE_TIMEOUT_NO_PREFIX = "idleTimeout";
String IDLE_TIMEOUT = CUSTOM_CAPABILITY_PREFIX.concat(IDLE_TIMEOUT_NO_PREFIX);
String SCREEN_RESOLUTION_NO_PREFIX = "screenResolution";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private TestSession createNewSession(Map<String, Object> requestedCapability) {
boolean videoRecording = Boolean.parseBoolean(getCapability(requestedCapability, ZaleniumCapabilityType.RECORD_VIDEO, "true"));
setVideoRecordingEnabledSession(videoRecording);
}
String testFileNameTemplate = getCapability(requestedCapability, ZaleniumCapabilityType.TEST_FILE_NAME_TEMPLATE, "");
String screenResolution = getCapability(newSession.getSlot().getCapabilities(), ZaleniumCapabilityType.SCREEN_RESOLUTION, "N/A");
String browserVersion = getCapability(newSession.getSlot().getCapabilities(), CapabilityType.VERSION, "");
String timeZone = getCapability(newSession.getSlot().getCapabilities(), ZaleniumCapabilityType.TIME_ZONE, "N/A");
Expand All @@ -243,6 +244,7 @@ private TestSession createNewSession(Map<String, Object> requestedCapability) {
.withPlatform(Platform.LINUX.name())
.withScreenDimension(screenResolution)
.withTimeZone(timeZone)
.withTestFileNameTemplate(testFileNameTemplate)
.withBuild(testBuild)
.withTestStatus(TestInformation.TestStatus.COMPLETED)
.build();
Expand Down

0 comments on commit 0eb8544

Please sign in to comment.