如何检查新窗口是否成功加载?

时间:2014-08-25 08:15:27

标签: selenium selenium-webdriver automated-tests

我必须访问特定div上的所有锚点。然后,我必须断言

  1. 链接在新窗口中打开并
  2. 还要确保链接没有损坏。
  3. 如何做到这一点?

2 个答案:

答案 0 :(得分:1)

使用findbyelements收集链接列表。将它们放入循环并选择一个链接。然后使用" getWindowHandle"切换到新窗口,其中可以实现断言"页面不存在"或者没有显示其他一些错误消息。这将确保链接是否被破坏

答案 1 :(得分:0)

我使用以下方法来验证链接是否正在打开一个新窗口并且没有被破坏。代码注释解释了代码。

    public boolean xAnchors()
{
    String parentHandle = driver.getWindowHandle(); // get the current window handle
    //      System.out.println(parentHandle);
    boolean isValidated = true;
    try{
        List<WebElement> anchors = wait.until(ExpectedConditions.visibilityOfAllElements(driver.findElements(By.xpath("//div[@class='container-with-shadow']//a")))); // This is an array of anchor elements
        try
        {
            for (WebElement anchor : anchors){                                 //Iterating with the anchor elements
                String anchorURL = anchor.getAttribute("href");
                anchor.click();
                String newWindow ="";
                for (String winHandle : driver.getWindowHandles()) {
//                      System.out.println(winHandle);
                    driver.switchTo().window(winHandle); // switch focus to the new window
                    newWindow = winHandle;               //Saving the new window handle
                }
                //code to do something on new window
                if(newWindow == parentHandle)           //Checking if new window pop is actually displayed to the user.
                {
                    isValidated = false;
                    break;
                }
                else
                {
                    boolean linkWorking = verifyLinkActive(anchorURL);      //Verifying if the link is Broken or not        
                    if(linkWorking)
                    {
                        System.out.println("The anchor opens a new window and the link is not broken");
                        isValidated = true;
                    }
                    else
                    {
                        System.out.println("The anchor either does not open a new window or the link is broken");
                        isValidated = false;
                    }
                }
                driver.close(); // close newly opened window when done with it
                driver.switchTo().window(parentHandle); // switch back to the original window               
            }
        }
        catch(Exception e)
        {
            isValidated = false;

        }
    }
    catch(Exception e)
    {
        System.out.println("No Anchors founds on this page.");
        isValidated = true;
    }
    System.out.println(isValidated);
    return isValidated; 
}


public boolean verifyLinkActive(String linkUrl){
    try {
        URL url = new URL(linkUrl);
        HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
        httpURLConnect.setConnectTimeout(3000);
        httpURLConnect.setRequestMethod("GET"); 
        httpURLConnect.connect();
        if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND){
            System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage()
                    + " - "+ HttpURLConnection.HTTP_NOT_FOUND);
        return false;
        }
        else
        {
            return true;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}