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

Selenium 如何检查新窗口是否已成功加载?,selenium,selenium-webdriver,automated-tests,Selenium,Selenium Webdriver,Automated Tests,我必须访问特定div上的所有锚。然后,我必须断言 链接将在新窗口中打开,然后 还要确保链接没有断开 如何做到这一点 使用findbyelements收集链接列表。将它们放入一个循环并选择一个链接。然后使用“getWindowHandle”切换到新窗口,在该窗口中可以实现断言“页面不存在”或未显示其他错误消息。这将确保链接是否已断开我使用以下方法验证链接是否正在打开新窗口且未断开。代码注释解释代码 public boolean xAnchors() { String parentH

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

  • 链接将在新窗口中打开,然后
  • 还要确保链接没有断开
    如何做到这一点

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

    我使用以下方法验证链接是否正在打开新窗口且未断开。代码注释解释代码

        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;
        }
    }
    
    public boolean xAnchors()
    {
    字符串parentHandle=driver.getWindowHandle();//获取当前窗口句柄
    //System.out.println(parentHandle);
    布尔值=真;
    试一试{
    List archors=wait.until(ExpectedConditions.visibilityOfAllElements(driver.findelelements(By.xpath(“//div[@class='container-with-shadow']//a”));//这是一个锚元素数组
    尝试
    {
    对于(WebElement锚定:锚定){//使用锚定元素进行迭代
    字符串anchorURL=anchor.getAttribute(“href”);
    anchor.click();
    字符串newWindow=“”;
    对于(字符串winHandle:driver.getWindowHandles()){
    //System.out.println(winHandle);
    driver.switchTo().window(winHandle);//将焦点切换到新窗口
    newWindow=winHandle;//保存新窗口句柄
    }
    //在新窗口上执行操作的代码
    if(newWindow==parentHandle)//检查是否向用户实际显示了新窗口弹出窗口。
    {
    isValidated=false;
    打破
    }
    其他的
    {
    boolean linkWorking=verifyLinkActive(anchorURL);//验证链接是否断开
    如果(链接工作)
    {
    System.out.println(“锚打开一个新窗口,链接没有断开”);
    isValidated=true;
    }
    其他的
    {
    System.out.println(“锚点未打开新窗口或链接已断开”);
    isValidated=false;
    }
    }
    driver.close();//完成后关闭新打开的窗口
    driver.switchTo().window(parentHandle);//切换回原始窗口
    }
    }
    捕获(例外e)
    {
    isValidated=false;
    }
    }
    捕获(例外e)
    {
    System.out.println(“本页未找到锚定”);
    isValidated=true;
    }
    System.out.println(isValidated);
    退货已确认;
    }
    公共布尔值verifyLinkActive(字符串链接URL){
    试一试{
    URL=新URL(链接URL);
    HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
    httpURLConnect.setConnectTimeout(3000);
    httpURLConnect.setRequestMethod(“GET”);
    httpURLConnect.connect();
    if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP\u未找到){
    System.out.println(linkUrl+“-”+httpURLConnect.getResponseMessage()
    +“-”+HttpURLConnection.HTTP\u未找到);
    返回false;
    }
    其他的
    {
    返回true;
    }
    }捕获(格式错误){
    e、 printStackTrace();
    返回false;
    }捕获(IOE异常){
    e、 printStackTrace();
    返回false;
    }
    }