Introduction to Attract and its utilization in Java autotest frameworks – ISS Artwork Weblog | AI | Machine Studying

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 reviews for automated assessments. It permits builders and testers to create detailed and significant reviews of their check outcomes and could be built-in with numerous check frameworks similar to JUnit, TestNG, and extra.
Why Attract particularly?
Attract is a well-liked selection amongst builders and testers for creating check reviews due to its ease of use and adaptability. The framework is extremely customizable and permits builders to create reviews which might be tailor-made to their particular wants. Moreover, Attract gives 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 reviews and how you can use annotations to offer your reviews extra which means. I’ll exhibit 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’ll need so as to add a number of dependencies to initiatives’ construct.gradle file:
- testImplementation group: ‘org.aspectj’, identify:
‘aspectjweaver’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, identify:
‘allure-java-commons’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, identify:
‘allure-junit5’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, identify:
‘allure-commandline’, model: ‘wanted model’ - testImplementation group: ‘io.qameta.attract’, identify:
‘allure-assertj’, model: ‘wanted model’
You’re going to want so as to add Attract plugin into plugins part of your construct.gradle file as properly:
- 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 recordsdata from dependencies you can begin implementing Attract to your challenge.
Maven
For Maven it’s a fairly easy course of as properly. You’re going to 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>
<identify>listener</identify>
<worth>io.qameta.attract.junit4.AllureJunit4</worth>
</property>
</properties>
<systemProperties>
<property>
<identify>attract.outcomes.listing</identify>
<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 neglect to switch 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 can be prepared.
Establishing challenge
We’ll start with a small Selenium check that opens Google.com, enters textual content into the search area, 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 have the ability to showcase Attract annotations. We’ll create two courses for this goal: GooglePage and GoogleSearchPomTest. The GooglePage class will include our components with locators and the strategies that cope with these components. The GoogleSearchPomTest class will include our check logic.
The challenge can be accessible in its entirety on GitHub repository. You possibly can obtain it and discover how the whole lot is finished.

GooglePage class.

GoogleSearchPomTest class.
To maintain issues easy, I received’t be making a separate class for the search outcomes web page. Since we solely have to work together with one aspect on the outcomes web page, I’ll embody it within the current GooglePage class.
Producing report
As soon as the whole lot is ready up, we are able to generate the primary Attract report by working our assessments for the primary time. This can create folders within the construct listing of our challenge the place the outcomes of the check runs can 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.
Working “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 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 assessments above it. Clicking on a set will will let you see which assessments have been included and the steps they contained.

In our instance, there was just one check technique. Attract routinely 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 predominant options of Attract is its annotations, which permit customers to customise the report back to make it extra readable. Presently, the report doesn’t present a lot data.
To enhance it, we are able to begin by utilizing the “@Proprietor(‘Proprietor’s identify and place’)” annotation. We will annotate check courses with this annotation to point the proprietor of the assessments.

After including the “@Proprietor” annotation to our check courses, we have to rerun the assessments and generate a brand new Attract report utilizing “allureServe”. As soon as the brand new report is generated, we’ll have the ability to see the proprietor part with the identify 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 assessments.
Subsequent, we are able to enhance the report even additional by utilizing the “@DisplayName(‘Take a look at identify’)” annotation. We will add this annotation to every check class and check technique to offer a transparent, descriptive identify 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 change into extra readable for non-technical stakeholders similar to managers.
In some circumstances, it’s vital to offer an outline of how a check works and what it does. We will use the “@Description(‘description’)” annotation to attain this. This annotation could be added to each check technique in a set, 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 are able to add the “@Step(‘step description’)” annotation to every technique in our web page object. This enables any exceptions that happen in the course of the check run to be displayed within the corresponding step.
To implement this, we are able to return to the GooglePage class and add the “@Step” annotation with a descriptive textual content to every technique.

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

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

Attract additionally gives 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 move 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) useful interfaces as parameters. The ensuing steps can be included within the report, making it extra informative.


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

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

Now let’s see what a failed check goes to seem like. Let’s change our isSearchedTextInHeader technique to anticipate false as an alternative of true.

Now we are able to see we’ve got failed assessments in our run. Let’s see what data we’ve got inside.

The Attract report shows the variety of failed assessments 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 assessments. We applied a code block that features a 300-millisecond anticipate the search button to change into clickable in each assessments. If we remark out this step in one of many assessments, the motive force could also be too fast to click on the button, leading to an “aspect not interactable” exception. Attract acknowledges this sort of error and marks the check as damaged, indicating that it’s more than likely a check engineering mistake fairly than a bug.

Once we encounter failed assessments in our check runs, we frequently need to examine the particular explanation for the failure. One efficient method to do that is to connect a screenshot of the second the check failed. JUnit provides the TestWatcher interface, whereas TestNG gives the ITestListener interface, each of which permit builders to override the testFailed() and onTestFailure() strategies. In these strategies, a screenshot attachment could 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 manner each time the check fails, a screenshot can be captured and connected to your report.
However likelihood is 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 scenario on this explicit challenge. So as to keep away from conflicts with the @AfterEach strategies, I applied a strive/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 When you have a whole lot of assessments, implementation of testing framework interfaces goes to profit you much more. Nonetheless, to grasp the final thought of how you can seize and connect screenshots to the check report, the above instance is enough.
Now at any time when an exception happens throughout check technique execution, a screenshot can be captured and added to the report.


This method gives a clearer understanding of any points that occurred in the course of the check execution, permitting you, your supervisor, or every other involved occasion to establish what went fallacious.
Conclusion
The capabilities of Attract prolong far past what has been coated on this article. There are quite a few different attention-grabbing and helpful implementations of Attract in cooperation with CI/CD course of, challenge and check administration methods. By implementing the easy hints mentioned right here, it is possible for you to to arrange and customise your Attract report back to an amazing extent, thereby making it a invaluable and informative instrument for you and your staff. With assistance from Attract, you will get a greater understanding of your check outcomes and make data-driven choices that improve the general high quality of your software program challenge.