-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] : Cucumber + Java

, 10 2017 . 11:55 +
, , , , .

BDD- Cucumber. , .

Cucumber Ruby-, . Cucumber Java.

Gherkin


BDD , , (), .

Cucumber Gherkin-, . *.feature , .

Gherkin:

# language: ru
@all
:   
       PIN-  
     ,     PIN-
   ,     PIN-

  :
          
           PIN-

  @correct
  :  
        PIN-
             

  @fail
  :  
        PIN-
       ,   PIN- 

, , , .

:

1. ;
2. - ;
3. .

. :

  1. , , , ;
  2. , ;
  3. , , ;
  4. , ;
  5. , ;
  6. , , . ;
  7. ;
  8. , , ;
  9. , , .

, 1-5, , Cucumber . *, . , .

:

# ;
@ ;
| ;
""" .

# language: ru. Cucumber, . , , , LexingError . .


Cucumber- (*.feature) ( *.java).

Apache Maven.
cucumber Maven:


    info.cukes
    cucumber-java
    1.2.4

JUnit ( TestNG), :


    junit
    junit
    4.12


    info.cukes
    cucumber-junit
    1.2.4

cucumber-junit cucumber.api.junit.Cucumber, , JUnit RunWith. , , .

, .

import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/features",
        glue = "ru.savkk.test",
        tags = "@all",
        dryRun = false,
        strict = false,
        snippets = SnippetType.UNDERSCORE,
//        name = "^|.*"
)
public class RunnerTest {
}

, Test, .

Cucumber:

  1. features .feature . . , : features = {src/test/features, src/test/feat};
  2. glue , . , , : glue = {ru.savkk.test, ru.savkk.hooks};
  3. tags . . ~ , ~@fail;
  4. dryRun true, , , , . false . false.
  5. strict true, . False . false.
  6. snippets . : SnippetType.CAMELCASE, SnippetType.UNDERSCORE.
  7. name .

tags name.



src/test/features . .

# language: ru
@withdrawal
:    

  @success
  :     
         120000 
         20000 
         100000 

  @fail
  :     -  
         100 
         120 
       "   "



RunnerTest :

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/features",
        glue = "ru.savkk.test",
        tags = "@withdrawal",
        snippets = SnippetType.CAMELCASE
)
public class RunnerTest {
}

:

Undefined scenarios:
test.feature:6 # :     
test.feature:12 # :     -  

2 Scenarios (2 undefined)
6 Steps (6 undefined)
0m0,000s

You can implement missing steps with the snippets below:

@("^    (\\d+) $")
public void (int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@("^    (\\d+) $")
public void (int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@("^  \"([^\"]*)\"$")
public void (String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

Cucumber .
MyStepdefs ru.savkk.test , :

import cucumber.api.PendingException;
import cucumber.api.java.ru.*;

public class MyStepdefs {

    @("^    (\\d+) $")
    public void (int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @("^    (\\d+) $")
    public void (int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @("^  \"([^\"]*)\"$")
    public void (String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
}

Cucumber . , Java- glue , . , . , .

, Cucumber , , , :

@("^    (\\d+) $")



@("^    (\\d+) $")

.

, . , . (\\d+). , \"([^\"]*)\". .

, :
:




.
(
)

2
j
.*
0
( )
Abracadabra
789-160-87


,
.+

( )
,
,
.
.{2}
(
)

22
$
JJ
.{1,3}

(
)


!
^

^aaa aaa
^aaa aaabbb
^aaa bbbaaa
$

aaa$ aaa
aaa$ aaabbb
aaa$ bbbaaa
\d*
[0-9]*
( )
12321

5323
\d+
[0-9]+

,
,
.
\w*
,
( )
_we
_1ee
Gfd4
\s
,

\t, \r
\n
"[^\"]*"
( )

"aaa"
""
"3213dsa"
?


abc?
ab
abc, b bc
|

aaa|bbb
aaa
bbb, aaabbb
()
. Cucumber

.
(\d+) 10 ,
10

(?: )
.
Cucumber
.
(\d+) (?:|) 3
, 3 ,
- .


, . Cucumber :

  1. , , ArrayList:

         , ,  

    @("^    (.*)$")
    public void (List arg) {
        // - 
    }

    , Delimiter:

              

    @("^    (.+)$")
    public void (@Delimiter("  ") List arg) {
        // - 
    }

  2. , , Cucumber ArrayList:

        
      |           |
      |  |
      |     |

    @("^   $")
    public void (List arg) {
        // - 
    }

  3. , , Cucumber , , :

        
      |           | true  |
      |  | false |
      |     | true  |

    public void (Map arg) {
        // - 
    }

  4. :

    • DataTable

          
        |           | true  | 5 |
        |  | false | 8 |
        |     | true  | 2 |

      @("^   $")
      public void (DataTable arg) {
          // - 
      }

      DataTable , . . :

      public  List> asMaps(Class keyType,Class valueType)

      . , :

          
        |       |  |   |
        |           | true     | 5                  |
        |  | false    | 8                  |
        |     | true     | 2                  |

      @("^   $")
      public void (DataTable arg) {
          List> table = arg.asMaps(String.class, String.class);
          System.out.println(table.get(0).get(""));
          System.out.println(table.get(1).get(""));
          System.out.println(table.get(2).get(""));
      }

      :





      public  List> asLists(Class itemType)

      :

          
        |           | true  | 5 |
        |  | false | 8 |
        |     | true  | 2 |

      @("^   $")
      public void (DataTable arg) {
          List> table = arg.asLists(String.class);
          System.out.print(table.get(0).get(0) + " ");
          System.out.print(table.get(0).get(1) + " ");
          System.out.println(table.get(0).get(2) + " ");
      
          System.out.print(table.get(1).get(0) + " ");
          System.out.print(table.get(1).get(1) + " ");
          System.out.println(table.get(1).get(2) + " ");
      }

      :

      true 5
      false 8


      public List> cells(int firstRow)

      , , , , List. :

          
        |           | true  | 5 |
        |  | false | 8 |
        |     | true  | 2 |

      @("^   $")
      public void (DataTable arg) {
          List> table = arg.cells(1);
          System.out.print(table.get(0).get(0) + " ");
          System.out.print(table.get(0).get(1) + " ");
          System.out.println(table.get(0).get(2) + " ");
      
          System.out.print(table.get(1).get(0) + " ");
          System.out.print(table.get(1).get(1) + " ");
          System.out.println(table.get(1).get(2) + " ");
      }

      :

      false 8
      true 2


    • Class Cucumber , . .

      Menu:

      public class Menu {
          private String title;
          private boolean isAvailable;
          private int subMenuCount;
      
          public String getTitle() {
              return title;
          }
      
          public boolean getAvailable() {
              return isAvailable;
          }
      
          public int getSubMenuCount() {
              return subMenuCount;
          }
      }

      :

          
        | title         | isAvailable | subMenuCount |
        |           | true        | 5            |
        |  | false       | 8            |
        |     | true        | 2            |

      :

      @("^   $")
      public void (List arg) {
          for (int i = 0; i < arg.size(); i++) {
              System.out.print(arg.get(i).getTitle() + " ");
              System.out.print(Boolean.toString(arg.get(i).getAvailable()) + " ");
              System.out.println(Integer.toString(arg.get(i).getSubMenuCount()));
          }
      }

      :

      true 5
      false 8
      true 2


      . , . - , .

      :

          
        | title        |  |  |   |
        | isAvailable  | true | false         | true        |
        | subMenuCount | 5    | 8             | 2           |

      @Transpose.

      @("^   $")
      public void (@Transpose List arg) {
          // - 
      }

      Cucumber, , , , , .



  5. , :

        
    """
           .
         
     .
    """

    String:

    @("^   $")
    public void (String expectedText) {
        // - 
    }

Date


, . , ConversionException. Date Calendar. :

    04.05.2017

@("^   (.+)$")
public void  (Date arg) {
    // - 
}

, Cucumber 04.05.2017 Date Thu May 04 00:00:00 EET 2017.

:

    04-05-2017

@("^   (.+)$")
public void  (Date arg) {
    // - 
}

, Cucumber :

cucumber.deps.com.thoughtworks.xstream.converters.ConversionException: Couldn't convert "04-05-2017" to an instance of: [class java.util.Date]

, ?

, Cucumber . , , Format:

    04-05-2017

@("^   (.+)$")
public void  (@Format("dd-MM-yyyy") Date arg) {
    // - 
}


, , :

# language: ru
@withdrawal
:    

  @success
   :     
         <> 
         <> 
         <> 

    :
      |  |  |  |
      | 10000      | 1     | 9999     |
      | 9999       | 9999  | 0        |

, , <>, . . .


Cucumber (hooks) , . Before After. , . :

import cucumber.api.java.After;
import cucumber.api.java.Before;

public class Hooks {
    @Before
    public void prepareData() {
        // 
    }

    @After
    public void clearData() {
        // 
    }
}

c Before , After .


, . order. order 10000, , :

@Before(order = 10)
public void connectToServer() {
    //  
}

@Before(order = 20)
public void prepareData() {
    // 
}

connectToServer(), prepareData().


value , . ~ . :

@Before(value = "@correct", order = 30)
public void connectToServer() {
    // -
}

@Before(value = "~@fail", order = 20)
public void prepareData() {
    // -
}

connectToServer correct, prepareData fail.

Scenario class


- Scenario, , :

@After
public void getScenarioInfo(Scenario scenario) {
    System.out.println(scenario.getId());
    System.out.println(scenario.getName());
    System.out.println(scenario.getStatus());
    System.out.println(scenario.isFailed());
    System.out.println(scenario.getSourceTagNames());
}

:

# language: ru
@all
:   
       PIN-  
          PIN-
        PIN-

  :
          
           PIN-

  @correct
  :  
        PIN-
             

:

--;-

passed
false
[@correct, @all]



Cucumber , . , Selenium -, Yandex.Allure , .
.
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332754/

:  

: [1] []
 

:
: 

: ( )

:

  URL