2015-02-11 85 views
0

我做硒数据驱动的框架,在Java语言硒TestNG的爪哇 - 参数太多

用TestNG我pageObjectLogin_Page.java存储页面上可用的所有定位器。

然后我有appModulesLogin_Action.java作为重复登录流程的常用功能。

如您所注意到的,步骤ExecuteLoginAction()是相同的,但由于需要测试用例输入而具有不同数量的参数。如何在这种情况下优化代码?

正如我在测试脚本中,我会打电话给Login_Action.ExecuteLoginAction(many...parameters)

我怎样才能避免这种MyTestScript_001Test()长参数列表和Login_Action.ExecuteLoginAction()

pageObjects Login_Page.java

package pageObjects; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 

    public class Login_Page extends BaseClass { 

     private static WebElement element = null; 

     public Login_Page(WebDriver driver){ 
       super(driver); 
     } 

     public static WebElement txt_enterUsername() throws Exception{ 
      try{ 
       element = driver.findElement(By.name("username")); 
      }catch (Exception e){ 
       throw(e); 
       } 
      return element; 
     } 

     public static WebElement txt_enterPassword() throws Exception{ 
      try{ 
       element = driver.findElement(By.name("password")); 
      }catch (Exception e){ 
       throw(e); 
       } 
      return element; 
     } 

     public static WebElement btn_clickLoginBtn() throws Exception{ 
      try{ 
       element = driver.findElement(By.name("loginBtn")); 
      }catch (Exception e){    
       throw(e); 
       } 
      return element; 
     }           
    } 

appModules Login_Action.java

package appModules; 
import org.apache.log4j.Logger; 
import org.openqa.selenium.WebDriver; 
import org.testng.Reporter; 

import pageObjects.Login_Page; 

    public class Login_Action {  

     public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword, 
       String ColFirstName, String ColLastName, String ColAddress, String ColCountry, String ColGender) throws Exception{   

      // Click Login link 
      Home_Page.lnk_clickLoginBtn().click(); 

      // Enter text for Username  
      Login_Page.txt_enterUsername().sendKeys(ColUsername); 

      // Enter text for Password 
      Login_Page.txt_enterPassword().sendKeys(ColPassword); 

      // Click Login submit button 
      Login_Page.btn_clickLoginSubmitBtn().click(); 

     } 

     public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword, 
       String ColFirstName, String ColLastName, String ColAddress) throws Exception{   

      // Click Login link 
      Home_Page.lnk_clickLoginBtn().click(); 

      // Enter text for Username  
      Login_Page.txt_enterUsername().sendKeys(ColUsername); 

      // Enter text for Password 
      Login_Page.txt_enterPassword().sendKeys(ColPassword); 

      // Click Login submit button 
      Login_Page.btn_clickLoginSubmitBtn().click(); 

     } 

    } 

Main Test Script (MyTestScript_001)

@Test(dataProvider="MyTestScript_001Data") 
    public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword, 
       String ColFirstName, String ColLastName, String ColAddress) throws Exception{ 


      // Login to web application 
      Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword, 
       ColFirstName, ColLastName, ColAddress, ColCountry, ColGender);            

      // Enter First Name 
      UpdateProfile_Page.txtbx_enterFirstName().sendKeys(ColFirstName); 

      // Enter Last Name 
      UpdateProfile_Page.txtbx_enterLastName().sendKeys(ColLastName); 

      Search_Action.ExecuteSearchAction(driver, ColTestCaseName, ColUsername, ColPassword, 
       ColFirstName, ColLastName, ColAddress, ColCountry, ColGender); 

Main Test Script (MyTestScript_002)

Main Test Script (MyTestScript_002) 

@Test(dataProvider="MyTestScript_002Data") 
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword, 
      String ColFirstName, String ColLastName, String ColAddress) throws Exception{ 


     // Login to web application 
     Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword, 
      ColFirstName, ColLastName, ColAddress);                  

     // Enter text for Address 
     UpdateProfile_Page.txtbx_enterAddress().sendKeys(ColAddress); 

您的建议都非常感谢!

+0

如果executelogin动作只需要用户名和密码,你为什么在传递这么多的数据。只需传入用户名和密码。 – 2015-02-11 13:11:51

+0

@niharika_neo,我的测试不仅适用于登录部分,我只是最小化代码以显示在问题中。事实上,我的参数现在高达20 ++。谢谢 – christo 2015-02-11 14:31:08

+0

制作对象而不是传递字符串。例如。创建具有名称,密码,地址等的用户类 – 2015-02-12 06:16:04

回答

1

没有意义的,要找这么多的参数,我将发送对象:

public class UserInfo { 
    private String colTestCaseName; 
    private String coldUserName; 
    private String colPassword; 
    public String getColTestCaseName() { 
     return colTestCaseName; 
    } 
    public void setColTestCaseName(String colTestCaseName) { 
     this.colTestCaseName = colTestCaseName; 
    } 
    public String getColdUserName() { 
     return coldUserName; 
    } 
    public void setColdUserName(String coldUserName) { 
     this.coldUserName = coldUserName; 
    } 
    public String getColPassword() { 
     return ColPassword; 
    } 
    public void setColPassword(String colPassword) { 
     this.colPassword = colPassword; 
    } 
} 

调用方法:

UserInfo userInfo = new UserInfo(); 
//Add credentials 
userInfo.setColUsername("user1"); 
.... 
Login_Action.ExecuteLoginAction(driver, userInfo);