Transcripted Summary

Before I proceed to run this test, I want to create a few more because I want to show you how to run more test classes and more test methods in parallel in the Test Run chapter.

I will just go to Project and I will Create a New Class in the taudemo package.


I'm going to call the class “WithConfigurationTest” because here, when we will run the test, we will also take a look at the configuration section that I mentioned from the navigation bar.



So, I will just create a test.

And I will also want to Add it to Git.


In this test, similarly to what I had in the SeleniumTest, I will need a BrowserGetter instance and a WebDriver because I will also open the browser here.

So, I will say: private BrowserGetter browserGetter = new BrowserGetter().

And then I'm just going to create the private WebDriver driver field.

I will create a BeforeAll method — public void beforeAll.

And I will initialize a browser here but this time, I will say driver =, and I'm not going to call the getChromeDriver method, but instead I'm going to call a method which is called getDriver.



And I will explain what this method does when we will run the test. Basically, this will open a browser according to a parameter that we specify.

Okay in AfterAll I just want to close the browser once I had opened it, so just say driver.quit

public class WithConfigurationTest{

    private BrowserGetter browserGetter = new BrowserGetter();
    private WebDriver driver;

    @BeforeAll
    public void beforeAll() {
        driver = browserGetter.getdriver();
    }

    @AfterAll
    public void afterAll() {
        driver.quit();
    }
}

Then I just want to create a new, very basic test.


I don't want to do much with it, because here I just want to prove how this getDriver method will be used with the configuration; so that's the only purpose of this test.

I will call it “justATest”, using camel case.

I will say driver.get and use the same URL.

Then we'll do some check afterwards, using assertEquals. Shall we do the page title again? Maybe driver.getTitle, and then I'm just going to type the expected value here I'm going to say: “Example domain”.

This time I will just hit and Alt+Enter after I typed the signature of the method.



So here I already filled in the method signature. Therefore, when I'm going to type assertEquals, the import will automatically be done with the correct method.

So, I have imported statically, the assertEquals method here.

This is just the test that I'm going to run.


I will create another test — this time I'm going to create a simple test that does not have any Selenium in it.

I just want to show you how to run a mixture of test classes and test methods.

I will just create a new class and I'm going to call it “WithAssertionsTest” because this is going to be a test class where I will just create some assertions. I don't want any other things to be created.

I'm just going to say @Test and public void firstAssertion.

And I'm going to put a mix of assertions that need to pass and assertions that will fail, just so we see what the results look like when we run the test.

So here I'm just going to say assertEquals(1,2) which we know definitely is going to fail, because 1 and 2 are not the same.

And then I'm going to say @Test and public void secondAssertion.

And we want this one to pass, so I'm going to say assertEquals(45,45).


public class WithAssertionsTest {

    @Test
    public void firstAssertion() {
        assertEquals(1, 1);
    }

    @Test
    public void secondAssertion() {
        assertEquals(45, 45);
    }
}

This is just an example, so this is not going to be a real test.

Okay, so for now I have created 3 test classes.

I still need to do something in the tests, namely in the browser tests where I have Selenium.

I also need to add an annotation before the method, actually before the class definition and I need to say @TestInstance(PER_CLASS).

And here, I will also say Alt+Enter > Add static import PER_Class, so I don't have to write TestInstance.Lifecycle.PER_CLASS.



So, I will just use this _Add static import _and this will shorten the coded I wrote here.



I will also copy/paste this @TestInstance(PER_CLASS) and I will paste it into the other Selenium test I have.

And here I also get a suggestion from IntelliJ regarding what imports to automatically do.



So, I will say OK to import both of these things and I am done with my test set up.

Currently, I have three tests that I created.


Actually, I just realized, when I created an assertion in this test class and in this test method justATest, I didn't put in correct order the expected and actual values.

If I use Ctrl+P as a shortcut, I can see that the first item in this assertion is the expected one.



So, I would expect the String to be the first parameter in this method call. I would expect “Example domain” to be the first item here.

Therefore, I can just switch the 2 easily by putting the cursor right before the comma and typing Alt+Enter — and here I have the option to Flip.



If I select Flip, you can see that the actual and expected values have changed their places, without me having to type anything.


@Test
public void justATest() {
    driver.get("https://www.example.com");
    assertEquals("Example domain", driver.getTitle());
}

NOTE

This is pretty neat if you are coming from testing with TestNG and now you're working with JUnit or vice versa. In order to get the order of the parameters right, if you wrote them the other way around, initially you can just Flip the 2 without having to type them again.

For now, I'm done with creating all of the tests and I just want to close them all because I just want to have this screen empty.

So, I will just go to one of the open tabs, and I will right click on them, and then I will just choose Close All.

I could have chosen to Close Others or to just Close this one, but I want to close all of them.



I will just say Close All for now and this concludes this chapter.