某些测试未在TestNG中报告

时间:2015-07-09 15:35:11

标签: java eclipse selenium annotations testng

我在Java中使用Eclipse中的TestNG和Selenium。我有一个方法test,它调用其他方法作为对它需要执行的操作的支持。我使用@Test将它们标记为测试的一部分。

@BeforeTest
public void beforeTest() {
    driver.manage().window().maximize();
}

@Test
public static void checkValidity(String[] array, WebDriver driver){
    String partialURL = "";
    int brokenLinks = 0;
    for (int x=1; x<array.length; x+=2){
        partialURL = anonUserSitemapExperience.getPartialURL(driver, array[x]);
        if (partialURL.isEmpty()){
            System.err.println("The link \""+array[x]+"\" intended for the "+array[x-1]+" page is either broken or an external site.");
            brokenLinks++;
            brokenLinksTot++;
        }
        else{
            int found = anonUserSitemapExperience.findMatch(array, array.length, partialURL);
            if (found<1){
                System.err.println("A match was not found for "+array[x-1]+".\n"+array[x]+"\n"+partialURL);
                brokenLinks++;
                brokenLinksTot++;
            }
        }
    }
    System.err.println("\n"+brokenLinks+" broken link(s) was/were found.\n");
}

@Test
public static void openDropDowns(WebDriver driver){
    List<WebElement> dropdownArrows = driver.findElements(By.className("dropdownToggler"));
    Iterator<WebElement> itr = dropdownArrows.iterator();
    while(itr.hasNext()){
        try{
            itr.next().click();
        }
        catch(ElementNotVisibleException e){
        }
    }
}

@Test
public static String[] createArray(List<WebElement> list){
    String[] linkArray = new String[list.size()*2];
    int counter = 0;
    for (int x=1; x<linkArray.length; x+=2){
        linkArray[x] = list.get(counter).getAttribute("href");
        try{
            linkArray[x] = linkArray[x].replaceAll("%C2%AE", "®");
            linkArray[x] = linkArray[x].replaceAll("%20", " ");
            linkArray[x] = linkArray[x].replaceAll("%27", "'");
            linkArray[x] = linkArray[x].replaceAll("%C3%A4", "ä");
            linkArray[x] = linkArray[x].replaceAll("%C3%B6", "ö");
            linkArray[x] = linkArray[x].replaceAll("%C3%BC", "ü");
            linkArray[x] = linkArray[x].replaceAll("%C3%84", "Ä");
            linkArray[x] = linkArray[x].replaceAll("%C3%96", "Ö");
            linkArray[x] = linkArray[x].replaceAll("%C3%9C", "Ü");
            linkArray[x] = linkArray[x].replaceAll("%E2%80%93", "–");
            linkArray[x] = linkArray[x].replaceAll("%E2%84%A2", "™");
            linkArray[x] = linkArray[x].replaceAll("%25", "%");
            counter++;
        }
        catch(Exception e){
        }
    }
    int counter2 = 0;
    for (int x=0; x<linkArray.length; x+=2){
        linkArray[x] = list.get(counter2).getText();
        counter2++;
    }
    return linkArray;
}

@Test
public void test() {
    driver.get(siteUS);
    List<WebElement> topNavLinks = driver.findElement(By.className("topNavigationMenu")).findElements(By.className("menuLink"));
    int numLinks = topNavLinks.size();
    String[] topNavTitlesAndLinks = new String[numLinks*2];
    topNavTitlesAndLinks = createArray(topNavLinks);
    System.out.println("Filled titles and links array.");
    for (int x=1; x<topNavTitlesAndLinks.length; x+=2){
        driver.get(topNavTitlesAndLinks[x]);
        openDropDowns(driver);
        try{
            List<WebElement> menu = driver.findElement(By.className("asideNavigationMenu")).findElements(By.className("itemLink"));
            String[] menuArray = new String[menu.size()*2];
            menuArray = createArray(menu);
            checkValidity(menuArray, driver);
        }
        catch (Exception e){
            if (topNavTitlesAndLinks[x-1].contains("Endodontics")){
                WebElement element = driver.findElement(By.linkText("Endodontics"));
                Actions action = new Actions(driver);
                action.moveToElement(element).perform();
                WebElement subElement = driver.findElement(By.partialLinkText("Access"));
                action.moveToElement(subElement);
                action.click();
                action.perform();
                openDropDowns(driver);
                List<WebElement> menu = driver.findElement(By.className("asideNavigationMenu")).findElements(By.className("itemLink"));
                String[] menuArray = new String[menu.size()*2];
                menuArray = createArray(menu);
                checkValidity(menuArray, driver);
            }
        }
    }
}

@AfterTest
public void afterTest() {
    driver.close();
    driver.quit();

    System.err.println("\nTotal broken links found: "+brokenLinksTot);

    long time2 = System.currentTimeMillis();
    double timeMin =((double)(time2-time1)/60000);
    int timeMinTrunc = (int)timeMin;
    double timeSec = (timeMin%1)*60;

    System.out.println("\n\nAll tests finished in "+timeMinTrunc+" minutes and "+timeSec+" seconds.");
}

问题是,当我运行它时,我没有从test调用的方法中获得传递失败。我的输出如下:

  

失败:测试

     

SKIPPED:checkValidity

     

SKIPPED:openDropDowns

那么如何才能使所有被调用的方法报告?

2 个答案:

答案 0 :(得分:0)

只有顶级@Test才会报告结果。 checkValidity,IMO,不是测试用例,它更像是一个验证,会导致测试通过或失败。请记住,@Test应该从头到尾表示一个测试用例,一个场景。我应该能够看到一个@Test并确切知道你想要完成什么。在您的示例中,您似乎正在尝试验证列表框中的条目。您的测试名称应该反映出来,并且编写它的方式,第一次失败,测试将失败。如果您需要在失败之前先验证每个条目,那么您需要适当地构建失败消息,而不是在所有迭代/点击完成之后抛出异常。

答案 1 :(得分:0)

  1. TestNG
  2. 会忽略非空方法
  3. 您不应该自己调用测试方法 - 这是TestNG的工作
  4. 重写您的测试类,以便使用@Test注释的方法反映您的方案并连续跟进。然后,您可以使用任何所需的帮助方法。
  5. E.g。

    @BeforeClass
    public void before() {
        //blah blah precondition goes here
    }
    
    @Test(priority=0)
    public void start() {
        // testy test 0 goes here
    }
    
    @Test(dependsOnMethods = "start")
    public void next() {
        Sysout(doSomething());
    }
    
    public String doSomething() {
        return "Helper method which I call in some test method";
    }
    

    <强> UPD

    如果要调用400次:

    @DataProvider(name = "400_links")
    public static String[][] getLinks() {
       // find your own way to create links here
       return new String[][] {{"http://foo"}, {"http://bar"}, ... , {"http://xyz"}};
    }
    
    @Test(dataProvider = "400_links")
    public void checkAllLinks(String link) {
       checkValidityOrWhatever(link);
    }
    
    public void checkValidityOrWhatever(String link) {
       //blah blah http ftw
    }
    

    如果您在getLinks()中提供400个链接,它将启动400次。每次迭代都将由Testng报告。失败的迭代不会阻止其他调用启动。