The next items I will create are two fields and one variable.
I am writing this SeleniumTest
right now, therefore, I will need a web driver instance which will hold my browser session.
When I want to initialize the browser, I have a dedicated class where I have some methods which allow me to start up the desired browser. So, I have a method to start up the Chrome driver. I have a method for Firefox, and so on.
In order to use that class where I have these browser initialization methods, I need to create a field and I will do this right now by typing “p” and selecting private
.
Then I will specify the name of the class that I want to initialize here, which is BrowserGetter
.
This is the name of the class where I have all my browser related methods.
This is a class I wrote, and it is present in this project. I will show it to you a bit later.
And then I need to give this field a name, so up to now I specified that it's private
, I specified the let's say type of this field, but now I need to also specify its name.
What I can do is I can type control + space and IntelliJ will offer a suggestion of names.
So based on the fact that the type of this field is BrowserGetter
, IntelliJ suggests that the name of the field is also browserGetter
, but in camel case.
I could also choose getter
if I wanted to, but I will just choose the first option, so I didn't need to type the name of the field myself.
Now I will just say = new BrowserGetter
.
private BrowserGetter browserGetter = new BrowserGetter();
I have created my first field, and this will be used to call the methods from the BrowserGetter
class, which include the method for starting the browser.
Next, I need to create a WebDriver
field where I will store the result of calling the browser initialization method.
I will do that in BeforeAll
but before I write the code for this, I will just create this field and I will say private WebDriver
.
Then for the suggested name, I just want to start with “d” so I will type “d” and out of the suggestions I will just choose driver
.
private WebDriver driver;
I will work with the driver
in the BeforeAll
method first, but I want to also create a variable.
So, inside the Test
method I want to create a variable which I will use to compare some property that I will read with Selenium and I want to compare that value to the variable that I have created here.
What I will do in that test is that I will open a page and I will read that page's title and I want to compare the title of the that page with an expected value of the title.
I will just create a String variable here, so I've typed “S” and String
is one of the options that I have available here.
So basically, I have a list of suggestions that I can choose from and for now I will choose String
and I will call the String “expectedTitle” and I will just give it a value.
I will say "Example title" and that is it.
@Test
public void openThePageAndCheckTheTitle”() {
String expectedTitle = “Example title”;
}
For now, I have created a variable, which as you can see by hovering the name of the variable that I am currently not using it.
So, I've created the variable, but its name is gray because it's not yet used anywhere.
NOTE
Whenever in your tests you will see something that has a gray name, that suggests that whatever that object you're looking at is, it is not used anywhere. It's the same with imports.
For example, if I would now create another import and I would just say, I don't know, whatever, as you can see this import is unused, so it's grayed out.
It is important not to have any grayed-out items in your test because that means you have declared something that is not being used.
You need to tidy up what you are doing in your tests. Don't leave anything that is unused sit in your test classes.
So, for now I will just delete this line by using the control+Y shortcut.
Quiz
The quiz for this chapter can be found in section 6.7