2015-04-06 32 views
0

在我的硒测试用例中,我有很多项目需要验证(例如,toCheck_isUserLogin,toCheck_isGenderChkbxExists等)。Selenium java - 从pageObjects传递布尔参数通用函数

我有这个检查成为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; 
    } 
} 

请让我知道,如果需要更多的信息,谢谢。

回答

0

通常在PageObjects中,我们没有任何断言。您PageObject可以修改成这样 -

​​

,做你的断言在主要测试类 -

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

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

嗨@Gosaka,感谢您的回复,科印'elements'点后,有没有'size'字填充,它只有'getSize'并且会得到msg'不兼容的操作数类型Dimension和int' – christo 2015-04-06 06:33:59

+0

我现在已经编辑了..基本上我想传达的是在你的PageClass中你返回driver.findElements By.xpath(Object.getProperty( “verify_isUserLogin”)))。这会给你列表和你的主要测试类,得到这个列表,你会得到你的大小。我没有给出确切的代码。对于那个很抱歉。 – Gosaka 2015-04-06 08:24:08

+0

嗨Gosaka,感谢您的帮助,但我无法产生预期的结果,因为if(elements.size())!= 0 {Testfail = false;}总是得到总体结果'Pass'。无论如何,我可以将'testfail'值从'pageObjects'传递给主脚本?谢谢。 – christo 2015-04-07 10:01:17