放置在页面对象下的TestNG断言不会反映在用于webdriver测试的TestNG报告中

时间:2015-02-10 15:50:33

标签: selenium selenium-webdriver testng pageobjects

我正在研究Java-selenium测试框架。

我们正在调用单个页面对象来测试方法,即:

verifyOrderSummary()

位于Page Object内的这些方法将具有单独的TestNG断言

现在,当我运行主调用方法/ TestNG测试时,执行assert的测试结果后,没有写入TestNG报告。

请告知。 我应该如何构建我的页面对象驱动的测试,以便 - 断言(软/硬)将反映在最终的TestNG报告中。

非常感谢

Hi, Below is one of the key test method( calling various page objects representing various pages):

 @Test(dataProvider="BillingDataExcel") 
    public void dataEntryMethod(String sNo, String sTestCaseName, String sTestDesc, String sAddMedia, String sSupplyVia, String sClock, String sAdvertiser, String sSubtitles, String sBrand, String sPriceModel, String sMarket) throws InterruptedException, IOException {
        try{        
        loginToA5();        
        navigateToDeliveryDashboard();
        softAssert = new SoftAssert();

        deliveryLandingPageDataVariant = new DeliveryLandingPageDataVariant(driver);        
            Thread.sleep(1000);
        deliveryLandingPageDataVariant.triggerCreateOrder();
            Thread.sleep(1000);
        deliveryLandingPageDataVariant.changeCountry(sMarket);
            Thread.sleep(1000);
        deliveryLandingPageDataVariant.addMedia(sAddMedia, sSupplyVia);

            //****XML Driven variant below****
            //deliveryLandingPageDataVariant.AddInformation();      
        deliveryLandingPageDataVariant.AddInformation(sClock,sAdvertiser,sSubtitles,sBrand);

            //****XML Driven variant below****
            //deliveryLandingPageDataVariant.selectBroadCastDest();
        deliveryLandingPageDataVariant.selectBroadCastDest(sNo);        
            myUtil.TakeSnapShot(driver, "AfterFilling-inData");
        deliveryLandingPageDataVariant.submitData();
            Thread.sleep(2000);
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        ViewOrderPage viewOrderPage = new ViewOrderPage(driver);    
        softAssert.assertTrue(viewOrderPage.verifyOrderSummary(sNo,sTestCaseName,myUtil.excelFeederRowCount++));                
        viewOrderPage.openOrderSummaryAndVerifyDesti(sNo);

        /*Below is a Temp hook, before we fix the @After annotation */

        driver.close();
        driver.quit();  
            Thread.sleep(1000);
        }catch(AssertionError e){
            System.out.println("Assertion error or other error -- ");
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }   
    }   

以下是Page Object的一个:

public class ViewOrderPage extends Abstractpage {
    XMLDataReader xmlDataReader = new XMLDataReader();

    @FindBy(css="#angularHolder > div > div > div.clearfix.pbxs.ng-scope > div > div:nth-child(2) > div > div > div:nth-child(2)")
    //@FindBy(xpath=".//*[@id='angularHolder']/div/div/div[1]/div/div[2]/div/div/div[2]")
    private WebElement QTYValue;

    @FindBy(xpath=".//*[@id='angularHolder']/div/div/div[1]/div/div[3]/div/div[1]/div[2]/span")
    private WebElement subTotalValue;

    @FindBy(xpath="//button[@data-role='viewReport']")

    private WebElement viewOrderSummaryButton;

    private final String WAIT_WHEEL_PATH =".//*[@id='angularHolder']/div/div/div[2]/div[2]";

    private int i;
    String STDCell;
    String EXPCell;
    String STACell; 
    private final String DELIVER_TO_STRING=".//*[@id='report']/div/div[2]/div[4]/table/tbody/tr";


    public ViewOrderPage(WebDriver driver){
        this.driver=driver;
        AjaxElementLocatorFactory aFactory= new AjaxElementLocatorFactory(driver, 20);
        PageFactory.initElements(aFactory, this);
        myWait=new WebDriverWait(driver, 120);
        myWait.until(ExpectedConditions.elementToBeClickable(viewOrderSummaryButton));
        //myWait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(WAIT_WHEEL_PATH));
    }

    @SuppressWarnings("finally")
    public boolean verifyOrderSummary(String sNo, String sTestCaseName, int feederRowCount) throws Exception{

        int subTotal=-1;
        int qty=-1;
        boolean valueMatched = false;

        System.out.println("\n=========================Verify Order Summary Section=================================");     
        System.out.println("**Quantity =" + getQTY());
        System.out.println("**subTotal ="+ getSubTotal());


    try {

        ExcelUtils.setExcelFile(System.getProperty("user.dir")+"/src/test/java/com/SAPAutomation/testData/BillingData.xlsx","TestResults");         
        ExcelUtils.setCellData(sNo, feederRowCount, 0);
        ExcelUtils.setCellData(sTestCaseName, feederRowCount, 1);
        ExcelUtils.setCellData(getQTY(), feederRowCount, 2);
        ExcelUtils.setCellData(getSubTotal(), feederRowCount, 3);       

        System.out.println("Test Results Read: "+ (String.valueOf(ExcelUtils.getCellData(feederRowCount, 4)).toLowerCase()));

        if(getSubTotal().toLowerCase().equals("£"+String.valueOf(ExcelUtils.getCellData(feederRowCount, 4)).toLowerCase()+".00")){
            valueMatched=true;
            System.out.println("\n**Subtotle of "+feederRowCount+" Matched**");
            ExcelUtils.setCellData("PASS", feederRowCount, 5);  
        }else{
            ExcelUtils.setCellData("FAIL", feederRowCount, 5);
            System.out.println("\n**Subtotle of "+feederRowCount+" didn't match**");
            valueMatched=false;
        }//end else 


//      try {
//          Assert.assertEquals(getSubTotal(), String.valueOf(ExcelUtils.getCellData(feederRowCount, 4)), "Quantity Matched");
//      } catch (Exception e) {
//          e.getMessage();
//          
//          Reporter.log("Assertion error in Verify Order summary:"+ e.getMessage());
//      }



    } catch (Exception e1) {
        e1.printStackTrace();
    }finally{
        myUtil.takeFieldSnapshot(driver, driver.findElement(By.xpath(".//*[@id='angularHolder']/div/div/div[1]/div")), "OrderSummaryHighlight");    
        return valueMatched;
    }
}


    @SuppressWarnings("finally")
    private String getQTY(){

        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        String QTY = "0";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        try{
            return QTY=QTYValue.getText();
        }catch(Exception e){
            e.getStackTrace();
            return QTY;         
        }

    }

    private String getSubTotal(){
        String subTotal=""; 
        try{
            return subTotal = subTotalValue.getText();
        }catch (Exception e){           
            e.getStackTrace();          
            return subTotal;
        }


    }


        public void openOrderSummaryAndVerifyDesti(String sNo) throws Exception{

            String mainWinHande=driver.getWindowHandle();

            viewOrderSummaryButton.click(); 
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            Set<String> handles = driver.getWindowHandles();

            for(String handle : handles)
            {
                if(!mainWinHande.equals(handle))
                {

                    driver.switchTo().window(handle);
                    String attriAndDestinations[][]=ExcelUtils.getTableArray(System.getProperty("user.dir")+"/src/test/java/com/SAPAutomation/testData/BillingData.xlsx", sNo, myUtil.GLOBAL_MAX_COLUMN_COUNT);                 
                    ExcelUtils.setExcelFile(System.getProperty("user.dir")+"/src/test/java/com/SAPAutomation/testData/BillingData.xlsx",sNo);
                    String destinationCell="";
                    STDCell="--";
                    EXPCell="--";
                    STACell="--";

                    int broadcasterTableSize= driver.findElements(By.xpath(DELIVER_TO_STRING)).size();

                     for(i=1;i</*broadcasterTableSize*/attriAndDestinations.length+1;i++){
                            int xPathDIVCounter=i+2;
                            //Get Destination
                            destinationCell=driver.findElement(By.xpath(DELIVER_TO_STRING+"["+xPathDIVCounter+"]/td[2]")).getText();
                            ExcelUtils.setCellData(destinationCell, i, 3);
                                System.out.println("\n**DEBUG: Destination Cell value ="+destinationCell);
                            //Get Attribute
                            //--STD
                            STDCell=driver.findElement(By.xpath(DELIVER_TO_STRING+"["+xPathDIVCounter+"]/th[1]")).getText();
                            ExcelUtils.setCellData(STDCell, i, 4);
                                System.out.println("\n**DEBUG: STD Cell value ="+STDCell);
                            //Get Attribute
                            //--EXP
                            EXPCell=driver.findElement(By.xpath(DELIVER_TO_STRING+"["+xPathDIVCounter+"]/th[2]")).getText();
                            ExcelUtils.setCellData(EXPCell, i, 5);
                                System.out.println("\n**DEBUG: EXP Cell value ="+EXPCell);
                            //Get Attribute
                            //--STA ** SPECIAL CASE** TO DO
//                          EXPCell=driver.findElement(By.xpath(DELIVER_TO_STRING+"["+xPathCounter+"]/th[3]")).getText();
//                          ExcelUtils.setCellData(STDCell, i, 6);

                        }//end for
                    // tallyTableRowsWithNumberOfDestinations(attriAndDestinations.length);
                    }//end if
            }//end for


            driver.close(); // close the poped-up report window
            driver.switchTo().window(mainWinHande);
        }

        private void tallyTableRowsWithNumberOfDestinations(int destinationSize){
            int rowCount= driver.findElements(By.xpath(DELIVER_TO_STRING)).size();
            System.out.println("\n** Table row Count: "+rowCount +"| destinations ArraySize: "+destinationSize);

        try {
            softAssert.assertEquals(rowCount-1, destinationSize+1, "Table rowcount didn't match with the Number of destinations selected");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

        public void concludeAsserts(){
            softAssert.assertAll();
        }


}

1 个答案:

答案 0 :(得分:0)

如评论中所述,您应该将测试逻辑与页面对象分开。页面对象应报告或更改页面的当前状态,由测试决定页面对象报告的内容是否正确。

更多地打破页面对象方法,以便每个人设置或检查页面上的一个项目,然后检查测试中的每个项目...

Assert.AreEqual(thePage.getOrderTotal(), expectedValue1);
Assert.AreEqual(thePage.getOrderItem(1), expectedValue2);
Assert.AreEqual(thePage.getOrderItem(2), expectedValue3);
Assert.AreEqual(thePage.getAnotherThing(), expectedValue4;
...

如果您发现自己在每次测试中反复做这些事情,您可能会发现将这些断言组合成测试类中的辅助方法很有用,我怀疑这可能是verifyOrderSummary()的预期目的