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

    

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值