Selenium java - 从pageObjects公共函数传递布尔参数

时间:2015-04-06 03:43:44

标签: java selenium parameters selenium-webdriver testng

在我的硒测试案例中,我有许多要验证的项目(例如toCheck_isUserLogintoCheck_isGenderChkbxExists等)。

我将此检查分组到pageObjects常用函数中以实现可重用的目的。 除了编写每个检查项Pass/Fail之外,我在Excel中有一个列来指定整体测试结果(通过/失败)

假设我的一个测试用例gender checkbox未在页面上找到,它应该将测试结果fail写入列'COL_IS_GENDER_CHKBX_EXISTS'

此外,它应该将Testfail=true传递给主脚本,因此在@AfterMethod时,测试用例的整体测试结果将为fail,因为其中一个验证项目失败。

将代码移到pageObject后,我无法将Testfail值传递给主测试脚本,是否有人可以帮我查看什么是错误的 用我的代码,谢谢。

// Selenium Main Test Script

    public class MyTestCase_01 extends SuiteRBase{
    Read_XLS FilePath_TestResult = null;
    static boolean TestCasePass = true; 
    static boolean Testskip = false;
    static boolean Testfail = false;
    static int DataSet = -1;
    public WebDriver driver;

    @BeforeTest
    public void checkCaseToRun() throws IOException{

        init(); 
        FilePath_TestResult = MyTestCase_01_TestResult;
        TestCaseName = this.getClass().getSimpleName(); 
        // code..   
    }

    @Test(dataProvider="MyTestCase_01Data")
    public void MyTestCase_01Test(String ColTestCaseName, String ColUsername, String ColPassword, String ColGender) throws Exception{

        // code...      
        try{

            Login_Page.toCheck_isUserLogin(FilePath_TestResult, TestCaseName, DataSet+1, Testfail);  

            // This was my code before move it into pageObjects Login_Page.java
        /*  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;
            }   */
            Register_Page.toCheck_isGenderChkbxExists(FilePath_TestResult, TestCaseName, DataSet+1, Testfail);          

        }catch(Exception e){
            Testfail = true;
            throw (e);
        }

        if(Testfail){
            // At last, test data assertion failure will be reported In testNG reports and It will mark your test data, test case and test suite as fail.
            s_assert.assertAll();       
        }
    }

    @AfterMethod
    public void reporterDataResults() throws Exception{     
        if(Testskip){
            Add_Log.info(TestCaseName+" : Reporting test data set line "+(DataSet+1)+" as SKIP In excel.");
            // If found Testskip = true, Result will be reported as SKIP against data set line In excel sheet.
            SuiteUtility.WriteResultUtility(FilePath, TestCaseName, "Pass/Fail/Skip", DataSet+1, Constant.KEYWORD_SKIP);
        }
        else if(Testfail){
            Add_Log.info(TestCaseName+" : Reporting test data set line "+(DataSet+1)+" as FAIL In excel.");
            // To make object reference null after reporting In report.
            s_assert = null;
            // Set TestCasePass = false to report test case as fail In excel sheet.
            TestCasePass=false; 
            // If found Testfail = true, Result will be reported as FAIL against data set line In excel sheet.
            SuiteUtility.WriteResultUtility(FilePath, TestCaseName, "Pass/Fail/Skip", DataSet+1, Constant.KEYWORD_FAIL);
        }else{
            Add_Log.info(TestCaseName+" : Reporting test data set line "+(DataSet+1)+" as PASS In excel.");
            // If found Testskip = false and Testfail = false, Result will be reported as PASS against data set line In excel sheet.
            SuiteUtility.WriteResultUtility(FilePath, TestCaseName, "Pass/Fail/Skip", DataSet+1, Constant.KEYWORD_PASS);            
        }
        // At last make both flags as false for next data set.
        Testskip=false;
        Testfail=false;
    }   
}

// pageObjects Login_Page.java

public class Login_Page extends BaseClass{

    private static WebElement element = null;
    static boolean Testfail = false;

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

    public static WebElement toCheck_isUserLogin(Read_XLS xls, String sheetName, int rowNum, Boolean Testfail) throws Exception{
        try{
            Boolean isUserLogin = driver.findElements(By.xpath(Object.getProperty("verify_isUserLogin"))).size()!= 0;               
            if (isUserLogin == true){
                SuiteUtility.WriteResultUtility(xls, sheetName, Constant.COL_IS_USER_LOGIN, rowNum, Constant.KEYWORD_PASS);
                Testfail = false;                   
            }else{
                SuiteUtility.WriteResultUtility(xls, sheetName, Constant.COL_IS_USER_LOGIN, rowNum, Constant.KEYWORD_FAIL);

                // Pass testfail true to main script so at @AfterMethod it will write overall test result 'fail'
                Testfail = true;
            }
        }catch(Exception e){
            throw(e);
        }
        return element;
    }

    public static WebElement toCheck_isGenderChkbxExists(Read_XLS xls, String sheetName, int rowNum, Boolean Testfail) throws Exception{
        try{
            Boolean isGenderChkbxExists = driver.findElements(By.xpath(Object.getProperty("verify_isGenderChkbxExists"))).size()!= 0;
            if (isGenderChkbxExists == true){
                SuiteUtility.WriteResultUtility(xls, sheetName, Constant.COL_IS_GENDER_CHKBX_EXISTS, rowNum, Constant.KEYWORD_PASS);
                    Testfail = false; 
            }else{
                // write into column 'COL_IS_GENDER_CHKBX_EXISTS' fail
                SuiteUtility.WriteResultUtility(xls, sheetName, Constant.COL_IS_GENDER_CHKBX_EXISTS, rowNum, Constant.KEYWORD_FAIL);

                // Pass testfail true to main script so at @AfterMethod it will write overall test result 'fail'
                Testfail=true;
            }
        }catch(Exception e){
            throw(e);
        }
        return element;
    }
}

请告诉我是否需要更多信息,谢谢。

1 个答案:

答案 0 :(得分:0)

通常在页面对象中,我们没有任何断言。您的页面对象可以修改为这样 -

public static List<WebElement> getUserLogin(Read_XLS xls, String sheetName, int rowNum, Boolean Testfail) throws Exception{
        try{
            return driver.findElements(By.xpath(Object.getProperty("verify_isUserLogin")));            

    }

在主测试类中做你的断言 -

   List<WebElement> elements=   Login_Page.getUserLogin(FilePath_TestResult, TestCaseName, DataSet+1, Testfail);  

if(elements.size())!=0{
Testfail =false;
}