
What’s Attract and what’s it used for
Attract is an open-source framework that’s designed to create lovely, interactive, and easy-to-read experiences for automated checks. It permits builders and testers to create detailed and significant experiences of their check outcomes and may be built-in with numerous check frameworks resembling JUnit, TestNG, and extra.
Why Attract particularly?
Attract is a well-liked selection amongst builders and testers for creating check experiences due to its ease of use and suppleness. The framework is very customizable and permits builders to create experiences which can be tailor-made to their particular wants. Moreover, Attract offers a variety of options, together with the power to create detailed check suites, the power to incorporate screenshots, and the power so as to add customized knowledge to the report.
On this article we’ll see how you can set up Attract to your challenge, how you can generate experiences and how you can use annotations to present your experiences extra which means. I’ll show attract’s capabilities on a small Selenium UI auto testing challenge.
Set up
Gradle
So as to use Attract with Gradle in your challenge you will have so as to add a number of dependencies to tasks’ construct.gradle file:
- testImplementation group: ‘org.aspectj’, title:
‘aspectjweaver’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, title:
‘allure-java-commons’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, title:
‘allure-junit5’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, title:
‘allure-commandline’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, title:
‘allure-assertj’, model: ‘wanted model’
You’ll want so as to add Attract plugin into plugins part of your construct.gradle file as nicely:
- id “io.qameta.attract” model “wanted model”

This can add “allureServe” and “allureReport” to your gradle verification duties.

After constructing the challenge and downloading all of the information from dependencies you can begin implementing Attract to your challenge.
Maven
For Maven it’s a fairly easy course of as nicely. You’ll want to change your pom.xml file:
<properties>
<aspectj.model>wanted model</aspectj.model>
<attract.model>wanted model</attract.model>
</properties>
<dependencies>
<dependency>
<groupId>io.qameta.attract</groupId>
<artifactId>allure-junit4</artifactId>
<model>$attract.model</model>
</dependency>
<dependency>
<groupId>io.qameta.attract</groupId>
<artifactId>allure-rest-assured</artifactId>
<model>$attract.model</model>
</dependency>
</dependencies>
<construct>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<model>wanted model</model>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
javaagent:”$settings.localRepository/org/aspectj/aspectjweaver/$aspectj.model/aspectjweaver-$aspectj.model.jar”
</argLine>
<properties>
<property>
<title>listener</title>
<worth>io.qameta.attract.junit4.AllureJunit4</worth>
</property>
</properties>
<systemProperties>
<property>
<title>attract.outcomes.listing</title>
<worth>$challenge.construct.listing/allure-results</worth>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<model>$aspectj.model</model>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>io.qameta.attract</groupId>
<artifactId>allure-maven</artifactId>
<model>wanted model</model>
<configuration>
<reportVersion>wanted model</reportVersion>
</configuration>
</plugin>
</plugins>
</construct>
Don’t overlook to interchange wanted model with the model you want.
This may even add all the required dependencies and plugins to your challenge, and after constructing the challenge Attract will likely be prepared.
Organising challenge
We’ll start with a small Selenium check that opens Google.com, enters textual content into the search discipline, clicks the search button, and verifies that the searched textual content is current on the outcomes web page. We’ll discover each the Web page Object Mannequin (POM) method and the non-POM method.
Let’s begin with the POM method, which is the popular technique for organizing UI automated testing frameworks. Moreover, we’ll be capable to showcase Attract annotations. We’ll create two courses for this function: GooglePage and GoogleSearchPomTest. The GooglePage class will include our components with locators and the strategies that take care of these components. The GoogleSearchPomTest class will include our check logic.
The challenge will likely be accessible in its entirety on GitHub repository. You’ll be able to obtain it and discover how all the pieces is completed.

GooglePage class.

GoogleSearchPomTest class.
To maintain issues easy, I gained’t be making a separate class for the search outcomes web page. Since we solely must work together with one ingredient on the outcomes web page, I’ll embrace it within the present GooglePage class.
Producing report
As soon as all the pieces is ready up, we will generate the primary Attract report by operating our checks for the primary time. This can create folders within the construct listing of our challenge the place the outcomes of the check runs will likely be recorded. Attract can then use these outcomes to generate a report. To generate an Attract report, run both “./gradlew allureServe” or choose the “allureServe” possibility in your Gradle duties.
Operating “allureServe” will launch the Attract internet service in your native machine and open an HTML web page in your browser. This web page will present an summary of the check run.

The overview web page shows numerous components showcasing the outcomes of your check run. An important of those is the “Suites” block, which exhibits the check courses that have been run. Moreover, there’s a graphical illustration of the amount of checks above it. Clicking on a collection will assist you to see which checks have been included and the steps they contained.

In our instance, there was just one check technique. Attract mechanically designates strategies annotated with “@BeforeEach” and “@AfterEach” as “Arrange” and “Tear down” steps, respectively. Nonetheless, it doesn’t show any steps inside the check technique itself.
Customizing report
One of many major options of Attract is its annotations, which permit customers to customise the report back to make it extra readable. At present, the report doesn’t present a lot info.
To enhance it, we will begin by utilizing the “@Proprietor(‘Proprietor’s title and place’)” annotation. We are able to annotate check courses with this annotation to point the proprietor of the checks.

After including the “@Proprietor” annotation to our check courses, we have to rerun the checks and generate a brand new Attract report utilizing “allureServe”. As soon as the brand new report is generated, we’ll be capable to see the proprietor part with the title included.

By including the “@Proprietor” annotation to our check courses, anybody who views the report will know who to contact if there are any points with the checks.
Subsequent, we will enhance the report even additional by utilizing the “@DisplayName(‘Take a look at title’)” annotation. We are able to add this annotation to every check class and check technique to offer a transparent, descriptive title for every suite and technique within the report.

We have to generate the report another time.

By including the “@DisplayName” and “@Proprietor” annotations to our check courses and check strategies, the report has develop into extra readable for non-technical stakeholders resembling managers.
In some circumstances, it’s essential to offer an outline of how a check works and what it does. We are able to use the “@Description(‘description’)” annotation to attain this. This annotation may be added to each check technique in a collection, identical to “@DisplayName”.

This can present the outline on the allures web page of the check case.

To make the Attract report much more informative, we will add the “@Step(‘step description’)” annotation to every technique in our web page object. This enables any exceptions that happen through the check run to be displayed within the corresponding step.
To implement this, we will return to the GooglePage class and add the “@Step” annotation with a descriptive textual content to every technique.

Now within the report we will see that our steps are displayed.

And if our step strategies have any parameters they are going to be displayed as nicely.

Attract additionally offers the period of every check technique, which may help in optimizing check efficiency.
If you don’t want to use the web page object mannequin in your framework and need to work together with WebDriver instantly, you should utilize the static technique “step()” offered by Attract. This technique means that you can outline a step with a reputation and cross the interplay with the motive force as a lambda by the ThrowableRunnableVoid (for interactions not returning any worth) or ThrowableRunnable<T> (for interactions returning one thing) purposeful interfaces as parameters. The ensuing steps will likely be included within the report, making it extra informative.


Even with this little quantity of customization our report already seems to be fairly informative and straightforward to learn and perceive. Now we will add a degree of severity to our checks. So as to add a degree of severity to our checks, we will use the @Severity() annotation, which permits us to specify the extent of severity for every check. The severity degree may be chosen from the SeverityLevel enum.

This can change the severity degree within the report. Be aware that by default severity degree is ready to “Regular”.

Now let’s see what a failed check goes to appear to be. Let’s change our isSearchedTextInHeader technique to anticipate false as a substitute of true.

Now we will see we have now failed checks in our run. Let’s see what info we have now inside.

The Attract report shows the variety of failed checks in our suite, identifies the step the place an exception occurred, and presents a related a part of the stack hint. Moreover, Attract distinguishes between failed and damaged checks. We carried out a code block that features a 300-millisecond watch for the search button to develop into clickable in each checks. If we remark out this step in one of many checks, the motive force could also be too fast to click on the button, leading to an “ingredient not interactable” exception. Attract acknowledges such a error and marks the check as damaged, indicating that it’s almost definitely a check engineering mistake quite than a bug.

Once we encounter failed checks in our check runs, we frequently need to examine the particular reason behind the failure. One efficient method to do that is to connect a screenshot of the second the check failed. JUnit affords the TestWatcher interface, whereas TestNG offers the ITestListener interface, each of which permit builders to override the testFailed() and onTestFailure() strategies. In these strategies, a screenshot attachment may be made by including the suitable code. Right here’s an instance:
byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
Attract.getLifecycle().addAttachment(“Screenshot”, “picture/png”, “png”, screenshot );
Attract.getLifeCycle().addAttachment() accepts an array of bytes as one of many parameters.
And Selenium is ready to take screenshots as an array of bytes, as proven within the snippet above. This fashion each time the check fails, a screenshot will likely be captured and hooked up to your report.
However chances are high this resolution goes to battle along with your @AfterEach strategies, since they’re run first. I managed to search out a better however much less elegant resolution to keep away from such a state of affairs on this explicit challenge. So as to keep away from conflicts with the @AfterEach strategies, I carried out a attempt/catch block across the total check code. If an exception is thrown, the catch block takes a screenshot, attaches it to the report, and rethrows the exception.

In fact If in case you have tons of of checks, implementation of testing framework interfaces goes to learn you much more. Nonetheless, to know the overall concept of how you can seize and connect screenshots to the check report, the above instance is ample.
Now at any time when an exception happens throughout check technique execution, a screenshot will likely be captured and added to the report.


This method offers a clearer understanding of any points that occurred through the check execution, permitting you, your supervisor, or every other involved occasion to determine what went incorrect.
Conclusion
The capabilities of Attract prolong far past what has been lined on this article. There are quite a few different fascinating and helpful implementations of Attract in cooperation with CI/CD course of, challenge and check administration programs. By implementing the straightforward hints mentioned right here, it is possible for you to to arrange and customise your Attract report back to an incredible extent, thereby making it a useful and informative instrument for you and your staff. With assistance from Attract, you may get a greater understanding of your check outcomes and make data-driven selections that improve the general high quality of your software program challenge.