Now I want to create a new test method, but it will be fairly identical to the one I have already created here. It's just that the checks will be done on a different page, not example.com.
I want to create a new test that will run on example.org — the rest of the method will be exactly the same.
I will just do a very simple copy/paste by doing a selection on the entire method and hitting Ctrl+D.
Then I will hit Enter twice because I want to organize the method to be below the initial one.
As you can see, the 2 method names are exactly the same, so I get an underline in red that I have two methods with the same name here.
What I will do is just to rename the second method and I will call it openTheOrgPage
.
So now I have a method called openThePageAndCheckTheTitle
and the other one is openTheOrgPageAndCheckTheTitle
.
However, I would like to change the name of the first method also, because I want to keep it a little bit in sync with the other one.
I will just hit the Shift+F6 shortcut for my keyboard and I will just change this.
I will just the type "Com" and hit Enter and now I have renamed the name of the method. Shift+F6 just means rename.
Now that I've done this, I will just adjust the second test so that it opens example.org.
At this point, I have 2 tests that are basically pretty much the same. It's just that they are on a different domain.
I might want to change the name of the variables also because I want them to also reflect the domain that they are on.
I'm going to say Shift+F6 while I have my cursor on the first variable, from the first method.
So, I will click Shift+F6 and I will rename it to expectedComTitle
.
You can see that I have highlighted the other place except for the definition where this name comes up and this is in the assertEquals
.
And as I was typing, the change was not made only at the variable definition level, but also where the variable is used inside the test.
I have changed the name and I will just hit Enter right now, and that will do the same with the second variable.
And I will say Shift+F6 and I will say that I want this variable in our second test to be named expectedOrgTitle
.
In this case, not only the definition of the variable was changed, but also the place where this is used.
So, I will hit Enter and now I have refactored my method names and also the variables that I have inside my test.
@Test
public void openTheComPageAndCheckTheTitle() {
String expectedComTitle = “Example title”;
driver.get("https://www.example.com");
assertEquals(expectedComTitle, driver.getTitle());
}
@Test
public void openTheOrgPageAndCheckTheTitle() {
String expectedOrgTitle = "Example title";
driver.get("https://www.example.org");
assertEquals(expectedOrgTitle, driver.getTitle());
}
Quiz
The quiz for this chapter can be found in section 6.7