1. 程式人生 > >web自動化測試框架搭建( Java+Cucumber+Gradle) _Mac_3

web自動化測試框架搭建( Java+Cucumber+Gradle) _Mac_3

1. 建立一個Gradle project

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


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專案必須按照一定的格式構建請按照下圖建立目錄

示例:註冊一個百度賬號    

  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 "<userName>"

      When I want to set my register mobile number as "<mobileNumber>"

      Then Set a register pass word as "<passWord>"

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 {

      }


  5). 驗證

     Mac終端

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

-->gradle build

-->gradle baidu

6). Report


5. 總結

1). 在驗證scenario時,要修改build.gradle 裡面的task下的feature路徑

     2). 修改RunCukesTest中的tag