zoukankan      html  css  js  c++  java
  • Data Driven Testing

    Advantages of using data driven framework:

    • Re-usable code
    • Better test coverage
    • Less Maintenance

    E.g:  using invalid login scenario as an example. 

    1.  define a negative login method on LgoinPage.java

    //Find the locator of the error message

    By loginErrorLocator = By.id("flash-messages");

    //Define a negativeLogin method

    public void negativeLogin(String username, String password) {
    System.out.println("Entering username and password:");
    find(usernameLocator).sendKeys(username);
    find(passwordLocator).sendKeys(password);
    System.out.println("Click Logon button");
    find(submitLocator).click();


    //invoke the waitLoginError method to wait for error appear
    waitLoginError();

    }

    //define a method to wait erro msg appear, invoke the waitForVisibilityOf method defefined in baseObject.java
    public void waitLoginError() {
    waitForVisibilityOf(loginErrorLocator, 5);
    }

    //define get error msg method. invoke find method defined in baseObject.java

    public String getLoginError() {
    return find(loginErrorLocator).getText();
    }

    2. Define the data provider method on baseTest.java (this base test includes setup. teardown..)

    @DataProvider(name = "negativeLoginData")
    public static Object[][] negativeLoginData(){
    return new Object[][] {
    {"IncorrectUserName", "Password!", "Your username is invalid!"},
    {"username","incorrectPassword", "Your password is invalid!"}
    };
    }

    3. Define test method to use the dataProvider

    @Test(dataProvider="negativeLoginData")
    public void netgativeLoginTest(String username, String password, String expectedErrorMessage ) {
    LoginPage lp = new LoginPage(driver);
    lp.open();
    lp.negativeLogin(username, password);

    String errorMessage = lp.getLoginError();
    //verification
    Assert.assertTrue(errorMessage.contains(expectedErrorMessage), "Actual and expected error messages are different. Expected:"+expectedErrorMessage+"Actual:"+errorMessage);
    }

    4. Update the testng.xml to include the negative test case

    e.g.

    <test name="NegativeLogin Test">
    <classes>
    <class name="com.test.FirstTest">
    <methods>
    <include name="netgativeLoginTest"></include>
    </methods>
    </class>
    </classes>
    </test>

  • 相关阅读:
    手脱UPX v0.89.6
    手脱ASPack v2.12
    为什么每次进入命令都要重新source /etc/profile 才能生效?
    解决maven update project 后项目jdk变成1.5
    关于dubbo服务的xml配置文件报错的问题
    dubbo实际应用中的完整的pom.xml
    部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener【转】
    web.xml配置文件中<async-supported>true</async-supported>报错
    eclipse在线安装maven插件
    centos安装eclise启动报错
  • 原文地址:https://www.cnblogs.com/amy2012/p/11624064.html
Copyright © 2011-2022 走看看