Introduction to Attract and its utilization in Java autotest frameworks – ISS Artwork Weblog | AI | Machine Studying
9 min read
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 studies for automated checks. It permits builders and testers to create detailed and significant studies of their check outcomes and might be built-in with varied check frameworks akin to JUnit, TestNG, and extra.
Why Attract particularly?
Attract is a well-liked selection amongst builders and testers for creating check studies due to its ease of use and suppleness. The framework is very customizable and permits builders to create studies which are tailor-made to their particular wants. Moreover, Attract offers a variety of options, together with the flexibility to create detailed check suites, the flexibility to incorporate screenshots, and the flexibility so as to add customized information to the report.
On this article we’ll see tips on how to set up Attract to your undertaking, tips on how to generate studies and tips on how to use annotations to offer your studies extra that means. I’ll show attract’s capabilities on a small Selenium UI auto testing undertaking.
Set up
Gradle
With a view to use Attract with Gradle in your undertaking you’ll need 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 will want so as to add Attract plugin into plugins part of your construct.gradle file as effectively:
- id “io.qameta.attract” model “wanted model”

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

After constructing the undertaking and downloading all of the recordsdata from dependencies you can begin implementing Attract to your undertaking.
Maven
For Maven it’s a fairly easy course of as effectively. You will want to switch 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>$undertaking.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 mandatory dependencies and plugins to your undertaking, and after constructing the undertaking Attract might be prepared.
Organising undertaking
We’ll start with a small Selenium check that opens Google.com, enters textual content into the search subject, 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 lessons for this goal: GooglePage and GoogleSearchPomTest. The GooglePage class will comprise our parts with locators and the strategies that cope with these parts. The GoogleSearchPomTest class will comprise our check logic.
The undertaking might be out there in its entirety on GitHub repository. You possibly can obtain it and discover how all the things 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 have to work together with one factor on the outcomes web page, I’ll embody it within the current GooglePage class.
Producing report
As soon as all the things is about 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 undertaking the place the outcomes of the check runs might 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” choice 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 outline of the check run.

The overview web page shows varied parts showcasing the outcomes of your check run. Crucial of those is the “Suites” block, which exhibits the check lessons that had been run. Moreover, there’s a graphical illustration of the amount of checks above it. Clicking on a collection will mean you can see which checks had been included and the steps they contained.

In our instance, there was just one check technique. Attract robotically designates strategies annotated with “@BeforeEach” and “@AfterEach” as “Arrange” and “Tear down” steps, respectively. Nevertheless, it doesn’t show any steps throughout the check technique itself.
Customizing report
One of many primary 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 will annotate check lessons with this annotation to point the proprietor of the checks.

After including the “@Proprietor” annotation to our check lessons, 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 lessons, 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 will add this annotation to every check class and check technique to supply a transparent, descriptive title for every suite and technique within the report.

We have to generate the report yet another time.

By including the “@DisplayName” and “@Proprietor” annotations to our check lessons and check strategies, the report has turn into extra readable for non-technical stakeholders akin to managers.
In some circumstances, it’s essential to supply an outline of how a check works and what it does. We will use the “@Description(‘description’)” annotation to realize this. This annotation might 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 permits any exceptions that happen throughout 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 effectively.

Attract additionally offers the length of every check technique, which may also 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 immediately, you need to use the static technique “step()” supplied by Attract. This technique lets you outline a step with a reputation and go the interplay with the motive force as a lambda via the ThrowableRunnableVoid (for interactions not returning any worth) or ThrowableRunnable<T> (for interactions returning one thing) practical interfaces as parameters. The ensuing steps might be included within the report, making it extra informative.


Even with this little quantity of customization our report already seems fairly informative and straightforward to learn and perceive. Now we will add a stage of severity to our checks. So as to add a stage 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 stage might be chosen from the SeverityLevel enum.

This can change the severity stage within the report. Observe that by default severity stage is about 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 applied a code block that features a 300-millisecond await the search button to turn 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 “factor not interactable” exception. Attract acknowledges this sort of error and marks the check as damaged, indicating that it’s most certainly a check engineering mistake quite than a bug.

After we encounter failed checks in our check runs, we frequently need to examine the particular reason for the failure. One efficient manner to do that is to connect a screenshot of the second the check failed. JUnit gives 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 might 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 might be captured and connected to your report.
However chances are high this answer goes to battle together with your @AfterEach strategies, since they’re run first. I managed to seek out a neater however much less elegant answer to keep away from such a scenario on this specific undertaking. With a view to keep away from conflicts with the @AfterEach strategies, I applied a attempt/catch block across the whole check code. If an exception is thrown, the catch block takes a screenshot, attaches it to the report, and rethrows the exception.

In fact In case you have lots of of checks, implementation of testing framework interfaces goes to profit you much more. Nevertheless, to know the overall concept of tips on how to seize and fix screenshots to the check report, the above instance is adequate.
Now at any time when an exception happens throughout check technique execution, a screenshot might be captured and added to the report.


This method offers a clearer understanding of any points that occurred throughout 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 coated on this article. There are quite a few different fascinating and helpful implementations of Attract in cooperation with CI/CD course of, undertaking and check administration techniques. 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 invaluable and informative software for you and your workforce. With the help of Attract, you may get a greater understanding of your check outcomes and make data-driven choices that improve the general high quality of your software program undertaking.