I will now write the code for the beforeAll
and afterAll
methods.
As I was saying, in the beforeAll
method I want to start up the browser, therefore, I will call one of the methods from the browserGetter
class.
I will say browserGetter
as in the name of the field browserGetter
and I will type .
.
At this point, a list of suggested methods that I can call from the browserGetter
class is displayed.
All of these names in bold, the first 5 names of methods, are methods from the browserGetter
class, so I can choose to pick one of them.
I will choose the getChromeDriver
method, as this one will open my Chrome browser.
If I click on the name of this method, and I click control, and then I hover the name of the method I will see the definition of this method.
First of all, in this overlay I will see that the method comes from the BrowserGetter
class and that each returns a WebDriver
instance.
I can store the result of calling this method into the driver
field that I created a while ago.
I will just say driver
, and as I start typing you see that I get a suggestion to use the field called driver
.
I will just hit Enter here, =
, and this means that my driver field will receive the result of calling the getChromeDriver
method from the browserGetter
class.
@BeforeAll
public void beforeAll() {
driver = browserGetter.getChromeDriver();
}
At this point in, @BeforeAll
I start the browser — this is how I will start it by calling the getChromeDriver
method.
I also want to close the browser after I am done with all the work.
In the AfterAll
method I will just say driver
and I will say .
and, at this point, I will get suggestions of methods that can be called on this driver
field from the WebDriver
class.
These methods in bold are all from the WebDriver
class which belongs to the Selenium library.
I will type “q” because I want to use a different method, not those that were suggested there.
I want to choose the quit
method.
NOTE
Of course, if I didn't type “q” I would have had to scroll until I actually found that method that I was interested in, but by typing “q” I narrowed down the list of available options.
I will just say:
@AfterAll
public void afterAll() {
driver.quit();
}
At this point, you can notice 2 things.
First of all, I have written the body of the @BeforeAll
and the @AfterAll
methods.
At the same time, my fields are not gray anymore, so their names are not gray.
If I will just comment the method body from @BeforeAll
for a while you would see that the browserGetter
has become gray.
I will just uncomment this and you will see that my browserGetter
field is now, again, in use.
I have used the browserGetter
and the driver
fields somewhere in the class, so now I don't have any unused fields any more here.
What if I want to see the definition of the getChromeDriver
method?
What if I want to see what this method contains? What is the code inside this method?
What I can do is I can hold control and click on the name of the method.
As you see, currently it is underlined, it looks kind of like a link.
If I hold control and I click the name of the method, the BrowserGetter
class is open.
You can see it in the navigation bar, and you can actually see the code for this method.
If I want to go back to the SeleniumTest
class I can just hit control, alt and the left arrow (Ctrl+Alt+Left), and now I'm back in my test class.
If I expand the project screen for a second, and I tried to get to the definition of the getChromeDriver
method again, you will see that on the left side the navigation jumped to the BrowserGetter
class.
I can see that this class is present in my project under src> main>java>browser — so this is the package that holds the class.
If I go back to my SeleniumTest
, and I want to see the definition of the quit
a method, which is in the WebDriver
class, which belongs to Selenium
, I will also hit the control key and while I'm pressing it I will click on the name of the method.
And now I will see the code of the WebDriver
class, and I can browse up and down in this class to see what else I have here.
Also, on the left-hand side you can see that the WebDriver
class is open, only this time it is not from my project, but it is from an external library.
This is one of the dependencies I have in my project, and this is where I can find the code for it.
Now, I will get back to my SeleniumTest
, and I will just close the project screen.
I also want to have as few as possible tabs here, and I will just want to close the WebDriver
and the BrowserGetter
class.
In order to do this easily I can just right-click on the name of the class I want to remain open, so on Selenium test, and I can just choose Close Others.
For now, I will just have this open tab.
If I want to open any other class, for example, if I want to go back to the BrowserGetter
class, I can just control and click on the name of the getChromeDriver
method.
Or, if I want to go to the top level of the class, if I want to go directly to the class definition, I can say control and I will just click the name of the class, and I will just jump to the definition of the class.
I am now done with writing the BeforeAll
and the AfterAll
methods.
Quiz
The quiz for this chapter can be found in section 6.7