03.Writing Test Code
- 01.About Pitalium
- 02.Environment Setup
- 03.Writing Test Code
- 04.Running a Test
- 05.Taking a Screenshot of a Specific DOM Element
- 06.Taking a Screenshot of a DOM Element with Scroll
- 07.Excluding Elements from Comparison Targets
- 08.Redefining of Correct Images
- 09.Running a Test in Mobile Devices
- 10.Pitalium Explorer
This chapter describes how to write test code in Pitalium.
Since Pitalium is based on Selenium, the way to show pages of to manipulate elements are the same as that of Selenium. You can refer to Selenium Documentation.
How to Use Pitalium
Check out a Sample Project
Check out pitalium-sample project from GitHub,
and run ivy_build.xml as shown in README
Prepare capabilities.json
A file capabilities.json is written to define which browsers you want to test in. A sample of that is in pitalium-sample/src/main/resources.
Write browsers information to test in it by reference to A configuration example for PC browsers、A configuration example for mobile browseres. About detail information of properties, see Configurations of Selenium Grid (Capability).
A capabilites.json to test in Internet Explorer, Firefo and Google Chrome is as follow:
{
"browserName": "internet explorer",
"version": "11"
},
{
"browserName": "firefox"
},
{
"browserName": "chrome"
}
]
Create a Test Class
You create a class which extends PtlTestBase to use Pitalium.
Create a test class in pitalium-sample/src/main/java as follow:
import static org.hamcrest.MatcherAssert.*;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.htmlhifive.pitalium.core.PtlTestBase;
public class SampleTest extends PtlTestBase {
@Test
public void test() throws Exception {
// 1. Open the top page of hifive site
driver.get("https://www.htmlhifive.com/");
// 2. Take a screenshot of the top page of hifive site
assertionView.assertView("OpenhifiveTopPage");
// 3. Check whether the title in "#about" is correct or not
WebElement about = driver.findElementById("about");
WebElement title = about.findElement(By.tagName("h2"));
assertThat(title.getText(), is("hifiveとは"));
}
}
This test code executes as follows:
- Open the top page of hifive site.
To open page, use the get method of the driver field in PtlTestBase. - Take a screenshot of the top page of hifive site as id=OpenhifiveTopPage.
Use the assertView method of the assertionView field in PtlTestBase.。See here to get detail information. - Check whether the title in "#about" is correct or not
Get a text of a DOM element using the getText method of WebElement.
(You don't need to write a code to generate WebDriver or to scroll, which are exexuted by Pitalium.)
Move to Another page by clicking
The following example is to click a link of a page, to move another page and to take screenshots of before and after moving.
import org.openqa.selenium.WebElement;
import com.htmlhifive.pitalium.core.PtlTestBase;
public class SampleTest extends PtlTestBase {
@Test
public void testClickAndCapture() throws Exception {
// 1. Open the top page of hifive site
driver.get("https://www.htmlhifive.com/");
// 2. Take a screenshot of the top page of hifive site
assertionView.assertView("OpenhifiveTopPage");
// 3. Get "過去のお知らせ一覧" button element and click it.
WebElement infoHistoryButton = driver.findElementByCssSelector("#news a.btn");
infoHistoryButton.click();
// 4. Take a screenshot of a page after moving.
assertionView.assertView("OpenNewsListPage");
}
}
This test code executes as follows:
- Open the top page of hifive site
- Take a screenshot of the top page of hifive site as ID=OpenhifiveTopPage.
- Click a link.
Use the click method of WebElement to click a link. - Take a screenshot of the page after moving as ID =OpenNewsListPage.
Send a Form Data and Move to Other Page
Then the following example is to type a text into textbox and to send it.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.htmlhifive.pitalium.core.PtlTestBase;
public class SampleTest extends PtlTestBase {
@Test
public void testInputAndSubmitAndCapture() throws Exception {
// 1. Open the top page of hifive site
driver.get("https://www.htmlhifive.com/");
// 2. Take a screenshot of it
assertionView.assertView("OpenTopPage");
// 3. Type a text into the search box at the top right of screen, and search in this site
WebElement form = driver.findElementByCssSelector("#globallinks form");
WebElement searchBox = form.findElement(By.id("headerglobalsearchinput"));
searchBox.sendKeys("test search");
form.submit();
// 4. Take a screenshot after moving a page
assertionView.assertView("OpenSearchResultPage");
}
}
This test code executes as follows:
- Open the top page of hifive site
- Take a screenshot of the top page of hifive site as ID=OpenhifiveTopPage.
1.Type a text into textbox and send form data.
Use the sendKeys method of WebElement to enter a text.
Submit form data using the submit method.
1.Take a screenshot of the page after moving as ID =OpenSearchResultPage.
In this example, send characters into the text box by using the sendKyes method of a fetched element, and submit a form by the submit method. Search in this site, move to a page and run a test of a search result.
Run a Test
Check the following states and run the JUnit test.
- the Selenium Grid Hub server is running on localhost.
- the Selenium Grid Node server is running on localhost.
- target browsers are specifeid in nodeConfigBrowser.json of the Selenium Grid Node server.
- capabilities.json is created as in Prepare capabilities.json.
You can check browsers registered in Selenium Grid Hub server by http://localhost:4444/grid/console.
As you run a test, launch Internet Explorer, Firefox and Google Chrome browsers, manipulate elements in them and take screenshots of them.
Screenshots are saved in the pitalium-sample/results folder.
We explain settings of a test and how to see results in the next chapter.
Next Step ⇒ 04.Running a Test