Authoring First Test
Let us take a simple scenario of Google Search. Let us author a test that performs Google search and verifies the title.
Typical steps to author the test
- Specify the URL of application under test
- Generate a Page class. Add locators and page methods
- Generate a Test class. Add test method
- Execute the test
Specify the URL of Application Under Test
The very first step is to configure the URL of the application under tests. To do so, open config/default.properties file & edit the web.url property as below
# Web Config
web.url: https://www.google.com/
Generate Page class
Let us generate Page Object corrosponding to Google Home Page.
We have a package called pages under web folder to hold all page classes (Page Object Model).
Please refer to Template Structure to understand the package structure in detail.
To generate a page class, right-click on the pages folder and choose New → Ekam Component.
In the Ekam Component menu, choose Page Object - Web. Enter name as GoogleHomePage
Hit enter.
This will create page class with below code.
package ekam.example.web.pages;
import com.testvagrant.ekam.reports.annotations.WebStep;
import com.testvagrant.ekam.atoms.web.WebPage;
import org.openqa.selenium.By;
public class GoogleHomePage extends WebPage {
@WebStep(keyword = "When", description = "I hit search button")
public GoogleHomePage search() {
return this;
}
}
Let us declare Selenium By statements for - Search Box and Google Search Button.
Ekam plugin provides mechanism to generate By statements. Type abbreviation and hit Tab to generate the live template & complete the code.
Declare By statements as below:
public class GoogleHomePage extends WebPage {
private By searchBox = queryByName("q");
private By searchButton = query("input[value='Google Search']");
@WebStep(keyword = "When", description = "I hit search button")
public GoogleHomePage search() {
return this;
}
}
Ekam Provides below live templates to locate elements.
Abbrevation | Locator Strategy |
---|---|
qbn | Query By Name |
query | Query by xpath or css |
qbi | Query By Id |
qba | Query By Attribute |
qbc | Query By Class |
qbt | Query By Text |
Implement the search method as below
@WebStep(keyword = "When", description = "I hit search button")
public GoogleHomePage search(String text) {
textbox(searchBox).setText(text);
element(searchButton).click();
return this;
}
Ekam provides abstractions to interact with WebElements gracefully.
Abstraction | |
---|---|
textbox | Represents a textbox element. |
element | Represents a generic element. |
This search method would perform a google search for a given text.
Lets add a new page class method which would return the title of page. To do this, Ekam provides a live template. type webstep and expand by hitting Tab to generate a page method.
Complete the template to generate code as below.
@WebStep(keyword = "And", description = "I return Title")
public String getTitle() {
return driver.getTitle();
}
The GoogleHomePage class would like as below:
package ekam.example.web.pages;
import com.testvagrant.ekam.reports.annotations.WebStep;
import com.testvagrant.ekam.atoms.web.WebPage;
import org.openqa.selenium.By;
public class GoogleHomePage extends WebPage {
private By searchBox = queryByName("q");
private By searchButton = query("input[value='Google Search']");
@WebStep(keyword = "When", description = "I hit search button")
public GoogleHomePage search(String text) {
textbox(searchBox).setText(text);
element(searchButton).click();
return this;
}
@WebStep(keyword = "And", description = "I return Title")
public String getTitle() {
return driver.getTitle();
}
}
Generate Test
Now that our Page class is ready, lets add a test.
Right click on web package choose New → Ekam Component
In the Ekam component popup, select Web Test & enter name as GoogleSearchTest
This will generate a test as shown below
package ekam.example.web.pages;
import com.testvagrant.ekam.testBases.testng.WebTest;
import static com.testvagrant.ekam.commons.LayoutInitiator.*;
import org.testng.annotations.Test;
import io.qameta.allure.TmsLink;
import static org.testng.Assert.*;
public class GoogleSearchTest extends WebTest {
@TmsLink("Test case id")
@Test(groups = "web", description = "Test case description")
public void webTest() {
// 1. Arrange
// 2. Act
// 3. Assert
}
}
Let us call the GoogleHomePage methods to complete the test as below
Here is complete code of test.
package ekam.example.web.pages;
import com.testvagrant.ekam.testBases.testng.WebTest;
import static com.testvagrant.ekam.commons.LayoutInitiator.*;
import org.testng.annotations.Test;
import io.qameta.allure.TmsLink;
import static org.testng.Assert.*;
public void shouldDoGoogleSearch() {
// 1. Arrange
String searchText = "TestVagrant";
String expectedTitle = "TestVagrant - Google Search";
// 2. Act
String actualTitle = Page(GoogleHomePage.class)
.search(searchText)
.getTitle();
// 3. Assert
assertEquals(actualTitle, expectedTitle);
}
}
Execute Test
We need to make a IntelliJ IDEA setting change to execute tests via IDE
Preferences → Build, Execution, Deployment → Build Tools → Gradle
Run tests using : IntelliJ IDEA
Execute the test !!
Congratulations!! on your first web test.