selenium从pageObjects公共函数传递参数到@Test

时间:2015-02-04 08:27:50

标签: java selenium selenium-webdriver apache-poi parameter-passing

我正在使用Selenium Webdriver开发自动化框架。

对于每个测试用例,我都有很多要验证的项目(例如,用户登录到Web应用程序,页面上是否存在性别复选框)

然后它将使用Excel WriteResultUtility根据预定义的列名称在Excel中写入结果。

我有20个测试用例,我不想每次都在每个测试用例中编写代码。 (最小化代码以便将来易于维护)

// Selenium主测试脚本

public class MyTestCase_01 extends SuiteBase{
Read_XLS FilePath = null;
Read_XLS FilePath_TestResult = null; //Location to store Output File
String TestCaseName = null; 
static int DataSet=-1;
public WebDriver driver;            

@BeforeTest
public void checkCaseToRun() throws IOException{        

    FilePath_TestResult = MyTestCase_0001_TestResult; //Each test case will have own Output file
    TestCaseName = this.getClass().getSimpleName(); 
}

@Test(dataProvider="MyTestCase_0001Data")
public void MyTestCase_0001Test(String ColTestCaseName, String ColUsername, String ColPassword) throws Exception{

    DataSet++;

    // add arguments to match  check_isUserLoggedIn(Read_XLS, String, String, int, String)
    Login_Page.toCheck_isUserLogin(FilePath_TestResult, TestCaseName);  
    Register_Page.toCheck_isGenderChkbxExists(FilePath_TestResult, TestCaseName);

// pageObjects Login_Page.java(我想将它作为常用函数使用)(但我对FilePath_TestResultTestCaseName变量有问题,因为它对每个测试用例都是不同的值)

public class Login_Page extends BaseClass{

    private static WebElement element = null;
    public static Read_XLS FilePath_TestResult = null;
    public static String TestCaseName = null;   
    static boolean Testfail = false;
    static int DataSet = -1;

    public Login_Page(WebDriver driver){
            super(driver);
    }
    public static WebElement toCheck_isUserLogin(Read_XLS xls, String sheetName, String ColName, int rowNum, String Result) throws Exception{
        try{
            Boolean isUserLogin = driver.findElements(By.xpath(Object.getProperty("verify_isUserLogin"))).size()!= 0;               
            if (isUserLogin == true){
                SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_USER_LOGIN, 
                        DataSet+1, Constant.KEYWORD_PASS);
                Testfail = false;                   
            }else{
                SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_USER_LOGIN, 
                        DataSet+1, Constant.KEYWORD_FAIL);
                Testfail = true;
            }
        }catch(Exception e){
            throw(e);
        }
        return element;
    }
    public static WebElement toCheck_isGenderChkbxExists(Read_XLS xls, String sheetName, String ColName, int rowNum, String Result) throws Exception{
        try{
            Boolean isGenderChkbxExists = driver.findElements(By.xpath(Object.getProperty("verify_isGenderChkbxExists"))).size()!= 0;
            if (isGenderChkbxExists == true){
                SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_GENDER_CHKBX_EXISTS,
                    DataSet+1, Constant.KEYWORD_PASS);
                    Testfail = false; 
            }else{
                SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_GENDER_CHKBX_EXISTS,
                    DataSet+1, Constant.KEYWORD_FAIL);
                Testfail=true;
            }
        }catch(Exception e){
            throw(e);
        }
        return element;
    }

// SuiteUtility.java

public static boolean WriteResultUtility(Read_XLS xls, String sheetName, String ColName, int rowNum, String Result){            
    return xls.writeResult(sheetName, ColName, rowNum, Result);         
}

// SuiteBase.java(初始化测试数据文件)

public class SuiteBase {    

public static Read_XLS MyTestCase_01_TestResult = null;
public static Read_XLS MyTestCase_02_TestResult = null;
public static Read_XLS MyTestCase_03_TestResult = null;
public static WebDriver driver = null;


//Initialize Test Data file.
public void init() throws IOException{

    // Test Result location
    MyTestCase_01_TestResult = new Read_XLS(System.getProperty("user.dir")+"\\TestOutput\\MyTestCase_01.xls");
    MyTestCase_02_TestResult = new Read_XLS(System.getProperty("user.dir")+"\\TestOutput\\MyTestCase_02.xls");
    MyTestCase_03_TestResult = new Read_XLS(System.getProperty("user.dir")+"\\TestOutput\\MyTestCase_03.xls");

我的目标是将其作为常用功能,以便可以重复使用并最小化代码。

但是,FilePath_TestResult(请参阅SuiteBase.java)是每个测试用例的不同变量。

(我在java.lang.NullPointerException获得SuiteUtility.WriteResultUtility) 我是否采用正确的方式宣布public static Read_XLS FilePath_TestResult = null;部分? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

为什么不将参数传递给以下方法

Login_Page.toCheck_isUserLogin(String...args);   
Register_Page.toCheck_isGenderChkbxExists(String...args);

并控制您要测试的参数? 我还建议您查看@AfterMethod,在那里您可以将上一个测试的结果写入您想要的列。 如果您需要进一步的帮助,请告诉我。