0

Serenity 是基于 selenium 的 BDD。我正在使用 3 个窗口处理程序。我的要求是这样的 -

  1. 打开窗口 1

  2. 单击窗口 1 上将打开窗口 2 的元素

3.单击窗口 2 上将打开窗口 3 的元素

  1. 关闭所有窗口

所有窗口处理程序都可以正常输入,但我仍然无法在窗口之间切换

4

2 回答 2

0
//In first window - Do something to activate second window
//Actions ..... here

//Second window opens
//Following code handles second window
ArrayList<String> newTab = new ArrayList (getDriver().getWindowHandles());
getDriver().switchTo().window(newTab.get(1));

//In second window - do some actions here
ArrayList<String> newTabs = new ArrayList<String> getDriver().getWindowHandles());
getDriver().switchTo().window((newTabs.get(2)));

//In Third Window        
//Do some actions here

//Close Third Window
getDriver().close(); //Disable if the action in third window closes third window like Cancel/OK button
//Switch back to second window
getDriver().switchTo().window(newTab.get(1));
//Close Second Window
getDriver().close();
//ghet back to initial (First) window
getDriver().switchTo().window(newTab.get(0));
于 2016-07-25T05:26:27.290 回答
0

这对我来说在处理最多 2 个窗口时工作得很好,但对于 3 个窗口则不行 -

public class MultipleWindowsHandle {


     WebDriver driver;  
     @Before  
     public void setup() throws Exception {  
     driver=new FirefoxDriver();  
     String URL="http://www.seleniummaster.com";   
     driver.get(URL);  
     driver.manage().window().maximize();  
     }  
     @Test  
     public void test() throws Exception {   
     // Opening site  
     driver.findElement(By.xpath("//img[@alt='SeleniumMasterLogo']")).click();  
     // Storing parent window reference into a String Variable  
     String Parent_Window = driver.getWindowHandle();    
      // Switching from parent window to child window   
     for (String Child_Window : driver.getWindowHandles())  
     {  
     driver.switchTo().window(Child_Window);  
     // Performing actions on child window  
     driver.findElement(By.id("dropdown_txt")).click();  
     List  dropdownitems=driver.findElements(By.xpath("//div[@id='DropDownitems']//div"));  
     int dropdownitems_Size=dropdownitems.size();  
     System.out.println("Dropdown item size is:"+dropdownitems_Size);  
     ((WebElement) dropdownitems.get(1)).click();  
     driver.findElement(By.xpath("//*[@id='anotherItemDiv']")).click();  
     }  
     //Switching back to Parent Window  
     driver.switchTo().window(Parent_Window);  
     //Performing some actions on Parent Window  
     driver.findElement(By.className("btn_style")).click();  
     }  
      @After  
      public void close() {  
      driver.quit();  
      }   
     }  
于 2016-02-04T03:46:57.087 回答