Transcripted Summary

Now that we have a test class, we can start creating the actual tests. These will be stored as methods.

I will just close the project screen for now because I want to focus on the class.

NOTE

In order to write tests, I have imported the JUnit library in my project, so all the annotation and all the assertions I will be using regarding testing are from the JUnit library.

The purpose of the test that I'm currently creating is to open a browser, open a page in the browser, then interact with that page, and after the test has run, I want to close the browser.

  • For that I will use a method annotated with the @BeforeAll annotation, which will execute before any other test method will execute. In this method I want to put the code which opens my browser.

  • After all the tests have been run, I want to also close the browser. So, for this purpose, I will create a method annotated with @AfterAll. This annotation, also from JUnit, specifies that the method, which is annotated with this annotation, will run after all of the test methods from this class have run.


# Let's start by creating the BeforeAll method.

In order to do that, I will just hit Enter right after this bracket, and I will type the @ character.



Once I do this, IntelliJ offers some suggestions regarding the possible annotations I could use.

I cannot find BeforeAll in any of these suggestions, so I will type “B”.



You can see that the second option that I'm offered in this list of options is the BeforeAll annotation, so I don't need to type BeforeAll; I can just select this value from the options list and the text will be typed automatically for me.


Notice that also because I wrote, let's say, the BeforeAll annotation, I also got an automatic import.



So, IntelliJ automatically imported the BeforeAll class for me from its JUnit library, which is a dependency of the current project.


The next thing I need to do is to specify the method name, and visibility, and what it will return.

Because it is a test, I will have to create a public void method.

I will type “p” and the list of suggested attributes that I can type here is displayed.



I can either say private, protected, or public.

But because this is a test method, I will select public from these available options.

The next thing I need to specify is the return type of the method.

As I mentioned, I want to return void, so I will say “v”, and again IntelliJ will suggest what I can type here.



I will select void, and this already saved me from having to write these entire words.

NOTE

I didn't have to write public void myself. I just wrote the first letters of these words and, based on the suggestions list, I just picked what I needed from those lists.

Now I will specify the name of the method, which I want to be beforeAll.

And I will type the parentheses and the brackets, and I will hit Enter.


# Placeholder Code – BeforeAll Method

public class SeleniumTest {

    @BeforeAll
    public void beforeAll() {

    }
}

At this point we've created the placeholder for the beforeAll method — I will fill in the method a bit later as I want to first create all of the methods.


# Now I want to create the AfterAll method.

So, I will just type the @ character and, as you can see, I have AfterAll in the list of suggestions.

I will just select it, and then I will just hit Enter, and again define the method signature by typing “P” and selecting public.

Then typing “V” and selecting void, and then typing the name that I want for this method, and it will be afterAll.

Add the parentheses and the brackets, and then I will hit Enter for a new line to be displayed.


# Placeholder Code – afterAll Method

@AfterAll
public void afterAll() {

}

# Now I also want to create one Test method.

I will say @ and I will type “T”, and the Test annotation is the first one that I have in the suggestions list, so I will just pick this one. This is perfectly valid because I am creating a test method here.

I will then type public void, and I will just name this test “openThePageAndCheckTheTitle”.


# Placeholder Code – Test Method

@Test
public void openThePageAndCheckTheTitle() {

}

And now I have created the outline for this test class.



So, in my test I will have a BeforeAll method, an AfterAll method, and for now just one test method.

Because I created the BeforeAll annotation, I will probably create some other tests as well.

But all of my tests will use only one browser instance because in the BeforeAll method I will open the browser and I will only close it in the AfterAll Method.

If we go up, we will see that IntelliJ automatically imported all 3 classes corresponding to the annotation that we specify for each of these methods.



It imported the AfterAll, BeforeAll, and Test classes from the JUnit library so that we don't have to do this operation ourselves.



Quiz

The quiz for this chapter can be found in section 6.7

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR