1. Create a maven project with Eclipse: Tạo một dự án bằng Maven trên Eclipse
Configure dependencies and plugins in maven pom.xml: Cấu hình dependencies và plugins trong POM file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rhino.auto</groupId>
<artifactId>cucumber_lesson1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Cucumber - WebDriver - Page Object Mode</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.version>1.1.7</cucumber.version>
<webdriver.version>2.42.0</webdriver.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.rubiconproject.oss</groupId>
<artifactId>jchronic</artifactId>
<version>0.2.6</version>
<scope>test</scope>
</dependency>
<!-- Webdriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>${webdriver.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${webdriver.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${webdriver.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${webdriver.version}</version>
</dependency>
<!-- Cucumber -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<useFile>false</useFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. Describe behaviour in plain text by writing test cases in a natural language that non-programmer can read. Users describe features and scenarios to test these features in plain text files using Gherkin language in Given, When and Then structure. : https://github.com/cucumber/cucumber/wiki
: Mô tả hoạt động ( hành vi ) của hệ thống bằng cách sử dụng văn bản để viết test case theo ngôn ngữ tự nhiên, Người không lập trình cũng có thể đọc hiểu. Users mô tả các tính năng và kịch bản kiểm tra bằng file văn bản. Việc mô tả này sử dụng ngôn ngữ Gherkin và tuân theo những quy chuẩn cấu trúc với những từ khóa Given, When, and Then: https://github.com/cucumber/cucumber/wiki
Feature: Send contact message
In order to contact with administrator
As a customer
I want to send contact message to administrator
Scenario: Send contact message
Given I am on home page
When I navigate to Contact page
And I fill form with data
And I submit my information
Then I get confirmation message
3. Write a step definition in Java: Viết mã bằng ngôn ngữ java để định nghĩa các bước cho kịch bản trên
package com.rhino.auto;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.rhino.pages.ContactPage;
import com.rhino.pages.ContactResultPage;
import com.rhino.pages.HomePage;
import cucumber.api.java.After;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class ContactMessgeTest {
WebDriver driver = new FirefoxDriver();
HomePage onHomepage = new HomePage(driver);;
ContactPage onContactPage = new ContactPage(driver);
ContactResultPage onContactResultPage = new ContactResultPage(driver);
@Given("^I am on home page$")
public void i_am_on_home_page() throws Throwable {
onHomepage.navigateToWebApp();
}
@When("^I navigate to Contact page$")
public void i_navigate_to_Contact_page() throws Throwable {
onContactPage = onHomepage.clickOnContact();
}
@When("^I fill form with data$")
public void i_fill_form_with_data() throws Throwable {
onContactPage.fillFormWithData();
}
@When("^I submit my information$")
public void i_submit_my_information() throws Throwable {
onContactResultPage = onContactPage.submitForm();
}
@Then("^I get confirmation message$")
public void i_get_confirmation_message() throws Throwable {
Assert.assertTrue(onContactResultPage.getConfirmationMessage().contains("Many thanks for your message. We will contact you about your query as soon as possible"));
}
@After
public void Shutdown(){
driver.quit();
}
}
4. Create a support class RunCukesTest that will define the Cucumber-JVM configurations : Tạo ra một lớp hỗ trợ RunCukesTest để xác định các cấu Cucumber-JVM:
package com.rhino.auto;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@SuppressWarnings("deprecation")
@RunWith(Cucumber.class)
@Cucumber.Options(
format = {"pretty", "html:target/cucumber-html-report"}
,features={"src/test/resources/"}
)
public class RunCukesTest {
}
5. Create a class ContactPage that using Page Object Mode and Page Factory: Tạo lớp ContactPage sử dụng mô hình Page Object và Page factory
package com.rhino.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class ContactPage extends AbstractPage{
@FindBy(how = How.NAME, using = "name_field")
WebElement nameField;
@FindBy(how = How.NAME, using = "address_field")
WebElement addressField;
@FindBy(how = How.NAME, using = "postcode_field")
WebElement postCodeField;
@FindBy(how = How.NAME, using = "email_field")
WebElement emailField;
@FindBy(how = How.ID, using = "submit_message")
WebElement submitBtn;
public ContactPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public ContactPage fillFormWithData() {
nameField.sendKeys("linh.vu");
addressField.sendKeys("HCMC Vietnam");
postCodeField.sendKeys("20000");
emailField.sendKeys("vuthelinh@outlook.com");
return new ContactPage(driver);
}
public ContactResultPage submitForm() {
submitBtn.click();
return new ContactResultPage(driver);
}
}
Download Zip Source Code:
https://github.com/vuthelinh/behaviour_template
Không có nhận xét nào:
Đăng nhận xét