Transcripted Summary

Let's now write the body of the test.

If I go to my Test method, I've already created the variable which I will need to compare to something.



As I mentioned, I initially opened the browser and of course after all the tests I will quit the browser, but what happens in between is that I want to open a page.

I will just type Selenium commands here and I'm going to say driver.get(), which opens a page and here I will specify what URL I want to open.

Notice that after I typed get and I hit Enter, after a few seconds of not doing anything I got a hint regarding what parameter I need to pass into this method.



There is only one definition of this method in the WebDriver class so I can only call the get method with one specific parameter and that is a String url.

So here I will just type the URL I want to open; so, I'm going to say:


driver.get("https://www.example.com"");

Okay so now I specified the String that I want to represent the page I'm opening.

At this point, the browser will open the following URL — the one that I typed here as a parameter to the get method.


After the page opens, my test has the purpose of checking that the title is the expected one; therefore, I would need an assertion.

I would need to say, “assert equals something with something” — meaning, an expected title versus and actual title.

I have typed assertEquals() as this is the method I will be using. But currently IntelliJ doesn't know what assertEquals method I'm referring to.

I'm going to hit Alt + enter and they have some options here including to Create method in the current class or to Import static method (to import a method that already exists somewhere in a library that I have as a dependency).



I'm going to choose the Import static method and I will see that there are 2 possible methods I can choose from, two assertEquals method.



The one I need is the one from the Assertions class — Assertions.assertEquals — so I will choose this one.

What happened here is that my method name is not red anymore, so I can actually start typing what I want in my assertEquals method — so I could start typing the expected and actual values.

And what I did was that I went in between the parenthesis and I typed Ctrl+P.



This gives me the list of possible parameters I can specify here

Now why do I have such a long list of possible parameters? Well, because the assertEquals method comes in various flavors.

If I click control, and then hovering the name of this method hovering assertEquals, I will click with my mouse I will see all of the implementations of this method.



The assertEquals can be used with all kinds of parameters including Strings, bytes, integers, Long, and so on.

I can pass in different types of parameters to these methods, therefore there are more than just one implementation of this method. There are many methods it's just that they have a different signature.

In my case, I want to compare 2 Strings, so I will again click Ctrl+P while between the parenthesis, because I want to see in which order I need to specify the expected and actual values shared.


The first parameter I need to specify is the expected value.

My expected value is the one I defined as a variable here, so I will say assertEquals the expectedTitle, and I need this expected title to equal a value which will be read from the page I'm opening.

The page title will be obtained via Selenium by calling driver.getTitle.



This returns the String, which I can see by holding control and hovering the name of the method.

Now we just type my semicolon here and my test is complete.


@Test
public void openThePageAndCheckTheTitle() {

    String expectedTitle = "Example title";
    driver.get("https://www.example.com");
    assertEquals(expectedTitle, driver.getTitle());
}

In this test I did everything I needed to do — I created a variable holding an expected value, I opened the page from where I needed to read a certain property, and then I used an assertion to compare the expected and the actual values.

If I go to the top level of the class, I can see that there is a new import here which was made when I imported the assertEquals method from the Assertions class.



This is from JUnit.

If I opened this class and I will navigate in the left-hand menu, in the project screen, I will see that the Assertions class is part of the JUnit library which I have as an external dependency.

Okay, so here if I search by just typing Ctrl+ F, I can see that there are several assertEquals methods here (I have 279 results). If I use Match Case, I will see that there are actually 186 such words in this file.



So these are probably all of them, the method implementations, so these many variants are implemented in this class regarding the assertEquals method.

What's different with this import in regard to the others is that we have the static word here.

Let’s see what happens if I type Assertions.assertEquals().



I am asked to import the Assertions class from the JUnit library.

So, I can type Alt+Enter and if I go to Home, I could see that this import was made here.



So basically, here I imported the entire class.

Here (in our first one) I only imported a method and I imported a static (I will show you in a bit).

So here I'm just saying Assertions.assertEquals and here I can say, “1” and “2” for example.



If I go to this method, now that I specify the parameters, if I click control and I click on the method name, I will go directly to this implementation of the method because I specified the type of each parameter.



So, I know exactly which assertEquals method I am referring to.

You can see here that this method is static, so because of this I can call it "name of the class” then `.assertEquals".


However, I don't want to say Assertions.assertEquals every time I'm calling this method, it's too long.

Therefore, as is the case with this assertEquals method, the import for it was done as Import static instead of Import and the name of the class. So, I just imported the static method assertEquals.

If I need to use another method from the assertions class which is assertNotEquals, I will need to perform another import for it.

For now, I will delete this unused import because I am not using the Assertions.assertEquals method; that was just an example,

I can do that by saying Ctrl+Alt+L.

What I expected was that the import, which is not used, would disappear when I'm using the shortcut however I first need to configure this shortcut a little bit.

So, I will say Ctrl +Alt+Shift+L, and this is the reformat screen.



Ctrl+Alt+L is a shortcut which reorganizes the code in your class.


If I want to optimize the import when I'm using this shortcut this means that all of the unused imports will go away once I hit that shortcut.

So, let's see, I will click Run here, and you can see that the used import was deleted.



If I do another import here that I'm not going to use, I'm just going to type an import just so you see how it will go away. So now I've typed an import that I'm not using and if I'm saying Ctrl+Alt+L it will automatically be removed.

This is how you configure your auto-arrange of the code and the removal of the unused imports.

Jumping back to my test, this is my Test method and I can soon run it.



Quiz

The quiz for this chapter can be found in section 6.7

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