web自动化测试框架搭建( Java+Cucumber+Gradle) _Mac_3

1. 创建一个Gradle project

file-->new-->Other-->Gradle Project

web自动化测试框架搭建( Java+Cucumber+Gradle) _Mac_3_第1张图片

2. 修改build.gradle

      // plugin

  apply plugin: 'java'

  apply plugin: 'eclipse'

  apply plugin: 'idea'

      // repositories

           repositories { 

             mavenCentral()

            }

      // configurations

           configurations {

            cucumberRuntime {

              extendsFrom testRuntime

               }

            }  

     // dependencies

          dependencies {

               compile 'org.springframework:spring-context:4.2.1.RELEASE'

             compile 'log4j:log4j:1.2.17'

             compile 'junit:junit:4.12'

             compile 'org.apache.commons:commons-lang3:3.0'

             compile 'org.seleniumhq.selenium:selenium-java:3.4.0'

             compile 'info.cukes:cucumber-core:1.1.2'

             compile 'info.cukes:cucumber-java:1.1.2'

             compile 'info.cukes:cucumber-junit:1.1.2'

            }

      // create gradle task,verify env is ok

           task helloWorld << {

             println("hello world")   

            }

3. 验证环境

     1). cd /Users/mobiletest-26/eclipse-workspace/FirstGradle

        2). gradle build

        3). gradle helloWorld

     如果可以在命令行中看到输出 hello world,那么搭建的环境是ok的

4. Gradle java项目必须按照一定的格式构建请按照下图创建目录

     示例:注册一个百度账号
    web自动化测试框架搭建( Java+Cucumber+Gradle) _Mac_3_第2张图片

      1). 在build.gradle中新建一个gradle task

         task baidu() {

                 dependsOn assemble, compileTestJava

                 doLast {

                      javaexec {

                      main = "cucumber.api.cli.Main"

                      classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output            

                      args = ['-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/java/resources/cucumber/baiduAccount.feature']   

                       }

                   }

              }   

      2). 在resources/cucumber下新建一个feature文件

        

         Feature: Register a baiDu user 

             @Test

             Scenario Outline: Register two new user in baidu  

        Given the page is open "https://passport.baidu.com/v2/?reg&tt=1521992892277&overseas=undefined&gid=B2CD3CB-E56D-4A02-A21B-D1ED22B752A6&tpl=mn&u=https%3A%2F%2Fwww.baidu.com%2F" 

      And I want to set my login in account as ""

      When I want to set my register mobile number as ""

      Then Set a register pass word as ""

     Examples: 

         | userName       | passWord | mobileNumber|

 | Alice1234        | 1q2w3e4r |18909204520  |

 | Alice12345      | 1q2w3e4r |18909204521  |

 | Alice123456    | 1q2w3e4r |18909204522  |

      3). 在/src/test/java下新建一个BasicStepDefs.java

          package gradle.cucumber;


          import cucumber.api.java.After;

          import cucumber.api.java.Before;

          import cucumber.api.java.en.Given;

          import cucumber.api.java.en.Then;

          import cucumber.api.java.en.When;

          import org.openqa.selenium.By;

          import org.openqa.selenium.WebDriver;

          import org.openqa.selenium.JavascriptExecutor;

          import org.openqa.selenium.WebElement;

          import org.openqa.selenium.chrome.ChromeDriver;

          import org.openqa.selenium.interactions.Action; 

          import org.openqa.selenium.interactions.Actions;


      public class BasicStepDefs { 

 WebDriver driver = null;

 public void initializeDriver() {

driver = new ChromeDriver();

// take chrome to front window.

String parent_window = driver.getWindowHandle();

driver.switchTo().window(parent_window);

 }

/*

* Register a baiDu account feature steps

*/

@Given("^the page is open \"([^\"]*)\"$")

public void the_page_is_open(String page) throws Throwable {

initializeDriver();

driver.get(page);

}

@Given("^I want to set my login in account as \"([^\"]*)\"$")

public void i_want_to_set_my_login_in_account_as(String arg1) throws Throwable {

WebElement name = driver.findElement(By.name("userName"));

name.sendKeys(arg1);

}

@When("^I want to set my register mobile number as \"([^\"]*)\"$")

public void i_want_to_set_my_register_mobile_number_as(String mobileNumber) throws Throwable {

WebElement mobile;

mobile = driver.findElement(By.cssSelector("#TANGRAM__PSP_3__phone"));

mobile.sendKeys(mobileNumber);

}

@Then("^Set a register pass word as \"([^\"]*)\"$")

public void set_a_register_pass_word_as(String passWord) throws Throwable {

WebElement pw = driver.findElement(By.id("TANGRAM__PSP_3__password"));

pw.sendKeys(passWord);

}

@After

public void cleanUp() {

driver.close();

             }

        }

4). 在/src/test/java下新建一个RunCukesTest.java

      package gradle.cucumber;
      import cucumber.api.CucumberOptions;
      import cucumber.api.junit.Cucumber;
      import org.junit.runner.RunWith;

     @RunWith(Cucumber.class)
     @CucumberOptions(tags = {"@Test"})
     public class RunCukesTest {

      }

web自动化测试框架搭建( Java+Cucumber+Gradle) _Mac_3_第3张图片

  5). 验证

     Mac终端

     -->cd /Users/mobiletest-26/eclipse-workspace/FirstGradle

      -->gradle build

      -->gradle baidu

6). Report

web自动化测试框架搭建( Java+Cucumber+Gradle) _Mac_3_第4张图片

5. 总结

    1). 在验证scenario时,要修改build.gradle 里面的task下的feature路径

     2). 修改RunCukesTest中的tag

    

你可能感兴趣的:(web,自动化测试)