Bước 1: Tạo một project bằng maven
Bước 2: Cấu
hình POM cho project:
Tương tự như
hướng dẫn trong video:
Nhưng chúng
ta thêm vào các thư viện liên quan tới WebDriver như là:
selenium-api
selenium-chrome-driver
selenium-firefox-driver
selenium-java
selenium-server
selenium-support
Các bạn có
thể tìm thêm dependencies của selenium tại
http://mvnrepository.com/artifact/org.seleniumhq.selenium
Nội dung 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.cucumber</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cucumber_webdriver</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>
</project>
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.cucumber</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cucumber_webdriver</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>
</project>
Bước 3: Viết
kịch bản test bằng Gherkin
Feature:
Login gmail account
Scenario:
Login with blank fields
Given I open
webmail "http://accounts.google.com"
When I click
login button
Then I see a
error message appears, it requires type email or pass
Bước 4: Viết
các dòng lệnh tương ứng với những bước trong kịch bản trên
package com.cucumber;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
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 DefineSteps {
WebDriver driver = new FirefoxDriver();
@Given("^I open webmail \"(.*?)\"$")
public void i_open_webmail(String baseUrl) throws Throwable {
driver.get(baseUrl);
}
@When("^I click login button$")
public void i_click_login_button() throws Throwable {
driver.findElement(By.id("signIn")).click();
}
@Then("^I see a error message appears, it requires type email or pass$")
public void i_see_a_error_message_appears_it_requires_type_email_or_pass(){
String ErrMsg = driver.findElement(By.id("errormsg_0_Email")).getText();
Assert.assertTrue(ErrMsg.contains("Hãy nhập địa chỉ email của bạn."));
}
@After
public void Shutdown(){
driver.quit();
}
}
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
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 DefineSteps {
WebDriver driver = new FirefoxDriver();
@Given("^I open webmail \"(.*?)\"$")
public void i_open_webmail(String baseUrl) throws Throwable {
driver.get(baseUrl);
}
@When("^I click login button$")
public void i_click_login_button() throws Throwable {
driver.findElement(By.id("signIn")).click();
}
@Then("^I see a error message appears, it requires type email or pass$")
public void i_see_a_error_message_appears_it_requires_type_email_or_pass(){
String ErrMsg = driver.findElement(By.id("errormsg_0_Email")).getText();
Assert.assertTrue(ErrMsg.contains("Hãy nhập địa chỉ email của bạn."));
}
@After
public void Shutdown(){
driver.quit();
}
}
Chúng ta đã
hoàn thành một kịch bản tự động cơ bản
Cám ơn các bạn
đã theo dõi
Không có nhận xét nào:
Đăng nhận xét