Əsas məzmuna keçin

Cucumber Testləmə

Cucumber maraqlı tərəflərin anlaya biləcəyi təbii dildə testlərin yazılmasına imkan verərək Davranış-Yönümlü İnkişafı (BDD) dəstəkləyir.

Cucumber-in Əsas Komponentləri

Gherkin Dili

Gherkin Cucumber testlərini yazmaq üçün istifadə edilən dildir. Test ssenarilərini strukturlaşdırmaq üçün açar sözlər toplusu istifadə edir:

  • Feature: Proqram funksiyasını və onun biznes dəyərini təsvir edir
  • Scenario: Xüsusi test halını təsvir edir
  • Given: Başlanğıc konteksti qurur (əvvəlki şərtlər)
  • When: Hərəkəti və ya hadisəni təsvir edir
  • Then: Gözlənilən nəticəni təsvir edir
  • And, But: Əlavə kontekst, hərəkətlər və ya gözləntilər əlavə etmək üçün istifadə edilir

Step Definitions

Step definitions Gherkin addımlarını kod ilə əlaqələndirir. Onlar sadə dili faktiki testləmə həyata keçirən icra edilən koda çevirirlər.

Runners

Runners Cucumber testlərini icra etmək üçün istifadə edilir və xüsusi xüsusiyyətlər və ya ssenariləri işlətmək üçün konfiqurasiya edilə bilər.

Nümunə: Cucumber Feature Faylı

Feature: User Authentication
As a user
I want to be able to log in to the system
So that I can access my account

Scenario: Successful login with valid credentials
Given I am on the login page
When I enter username "john.doe" and password "password123"
And I click the login button
Then I should be redirected to the dashboard
And I should see a welcome message with my name

Scenario: Failed login with invalid credentials
Given I am on the login page
When I enter username "john.doe" and password "wrongpassword"
And I click the login button
Then I should see an error message "Invalid credentials"
And I should remain on the login page

Java Step Definition-ları

Koda bax
@Given("I am on the login page")
public void i_am_on_the_login_page() {
driver = new ChromeDriver();
loginPage = new LoginPage(driver);
loginPage.navigate();
}

@When("I enter username {string} and password {string}")
public void i_enter_username_and_password(String username, String password) {
loginPage.enterUsername(username);
loginPage.enterPassword(password);
}

@Then("I should be redirected to the dashboard")
public void i_should_be_redirected_to_the_dashboard() {
dashboardPage = new DashboardPage(driver);
assertTrue(dashboardPage.isDisplayed());
}

Quraşdırma

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>

Runner Sinfi

Koda bax
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = "com.example.stepdefinitions"
)
public class CucumberRunner {
}

Ən Yaxşı Təcrübələr

  1. İstifadəçi perspektivindən yazın
  2. Ssenariləri müstəqil saxlayın
  3. Biznes dilindən istifadə edin
  4. Xüsusiyyətləri qısa saxlayın
  5. Step definition-ları təkrar istifadə edin

Qabaqcıl Cucumber Xüsusiyyətləri

Scenario Outline-lar

Scenario Outline: Login with different user types
Given I am on the login page
When I enter username "<username>" and password "<password>"
And I click the login button
Then I should be redirected to the "<landing_page>"

Examples:
| username | password | landing_page |
| admin | admin123 | admin-panel |
| customer | customer123 | dashboard |
| support | support123 | tickets |

Background-lar

Feature: Shopping Cart

Background:
Given I am logged in as a customer
And my shopping cart is empty

Scenario: Add item to cart
When I add product "Laptop" to my cart
Then my cart should contain 1 item

Scenario: Remove item from cart
Given I have added product "Laptop" to my cart
When I remove product "Laptop" from my cart
Then my cart should be empty