窗口处理——隐式、显式、流畅的等待都不起作用。只有 Thread.Sleep 在打开新窗口时才起作用

问题描述 投票:0回答:1

当弹出警报打开一个新窗口时,执行将继续,不会等待新窗口完全加载。具有讽刺意味的是,代码执行通过了测试,但它不应该通过测试,因为许多 Web 元素仍然有待执行。我很困惑。我尝试了隐式等待、显式等待、流畅等待……唯一有效的方法是 Thread.sleep,这不是一种有效的方法,因为它应该是动态等待,而 thread.sleep 会减慢这里的 selenium 测试。我相信测试会通过,因为其余代码位于 while 语句中。我自己学习编码已经快三年了,没有学位。请不要灰心,因为在我 40 多岁的时候,我发现了一些让我彻夜难眠的东西,但我仍然没有感觉到。

有人提到“”“”“在打开弹出窗口之前获取主窗口的句柄并保存它。

String Parent=driver.getWindowHandle(); “”“”对我不起作用。

---线程,睡眠是有效的...(不是有效的方法)...

--driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);..... 不起作用

--在许多预期条件下的显式等待不起作用...... //代码....BaseClass(使用testNG)

      System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver");
    
    driver = new ChromeDriver();
    
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://www.usda.gov/nutrition-security");


      
            package testNGpckg;


import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;



public class WindowHandles extends BaseClass {

    
    
    
    @Test
    public void handleWindow() throws InterruptedException {
         
        
        
        
       // System.out.println(driver.getTitle()); 
        
        String parent = driver.getWindowHandle(); // getting parent window as a string...

        Set <String> setOfWindows = driver.getWindowHandles(); // getting all other windows
        Iterator <String> iterating = setOfWindows.iterator();//Now iterate using iterator
        
        driver.findElement(By.xpath("/html/body/div/footer/div[2]/div[2]/div/div/div/div/a")).click();
        
    
        driver.switchTo().alert().accept();// alert handling here
        //Thread.sleep(4000); //this need to be replaced with implicit wait i think
    
        
        
        //The new window needs to be opened before the code below should run
        
        
        
    while (iterating.hasNext() ) {
    String child = iterating.next();
            
                if (!parent.equalsIgnoreCase(child)) {
             driver.switchTo().window(child); //switching to child window
        
        System.out.println(driver.getTitle()+ " (This is the Title of child window)");
                
            driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/div/div[2]/div[2]/fieldset/div[3]/label")).click();
            WebElement email =  driver.findElement(By.xpath("//*[@id=\"inputProp0\"]"));
            email.sendKeys("[email protected]");
            driver.findElement(By.xpath("//*[@id=\"update-profile-submit-btn\"]")).click();
            
            System.out.println("\n" + driver.findElement(By.xpath("//*[@id=\"optinSuccess\"]")).getText());
        
        
         // switching back to main window
     
             
        System.out.println(" \n LETS TRY GOING BACK TO MAIN WINDOW AND GET TITLE AGAIN. \n "); 
            driver.switchTo().window(parent);
            System.out.println(driver.getTitle() +" (We are back to main window and this is the Title of main window)");
            
            
        }
      }
 
    }

 }
selenium-webdriver testng window-handles
1个回答
0
投票

终于在其他一些堆栈溢出帖子中找到了答案...声明 WebDriverWait,然后等待两个窗口打开,然后才能执行。

 WebDriverWait wait = new WebDriverWait(driver,30);
       wait.until(ExpectedConditions.numberOfWindowsToBe(2));//here 2 represents the current window and the new window to be opened

© www.soinside.com 2019 - 2024. All rights reserved.